summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/MeshBaker/Editor/propertyDrawers/MB_PrefabPairPropertyDrawer.cs
blob: 3c1d9978d54f7ab472d36cba4158e49f96c39d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using DigitalOpus.MB.MBEditor;

namespace DigitalOpus.MB.MBEditor
{
    /// <summary>
    /// Draws rows for the PrafabPairs in the Switch Prefabs In Scene Window.
    /// </summary>
    [CustomPropertyDrawer(typeof(MB_ReplacePrefabsSettings.PrefabPair))]
    public class MB_PrefabPairPropertyDrawer : PropertyDrawer
    {
        private GUIStyle redFont = new GUIStyle();

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);
            position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
            int indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel = 0;

            float xx = position.x;
            float yy = position.y;
            float ww = 15f;
            float hh = position.height;

            Rect enabledRect = new Rect(xx, yy, ww, EditorGUIUtility.singleLineHeight);
            xx += ww;
            ww = 50;
            Rect srcLabel = new Rect(xx, yy, ww, EditorGUIUtility.singleLineHeight);
            xx += ww;
            ww = 200;
            Rect sourceRect = new Rect(xx, yy, ww, EditorGUIUtility.singleLineHeight);
            xx += ww;
            ww = 50;
            Rect targetLabel = new Rect(xx, yy, ww, EditorGUIUtility.singleLineHeight);
            xx += ww;
            ww = 200;
            Rect targetRect = new Rect(xx, yy, ww, EditorGUIUtility.singleLineHeight);

            EditorGUI.PropertyField(enabledRect, property.FindPropertyRelative("enabled"), GUIContent.none);
            EditorGUI.LabelField(srcLabel, "Source:");
            EditorGUI.PropertyField(sourceRect, property.FindPropertyRelative("srcPrefab"), GUIContent.none);
            EditorGUI.LabelField(targetLabel, "Target:");
            EditorGUI.PropertyField(targetRect, property.FindPropertyRelative("targPrefab"), GUIContent.none);

            SerializedProperty errorsProp = property.serializedObject.FindProperty(property.propertyPath + ".objsWithErrors");
            if (errorsProp.arraySize > 0)
            {
                redFont.normal.textColor = Color.red;
                for (int i = 0; i < errorsProp.arraySize; i++)
                {
                    xx = position.x;
                    yy = position.y + (1 + i) * EditorGUIUtility.singleLineHeight;
                    ww = 100;
                    SerializedProperty errProp = errorsProp.GetArrayElementAtIndex(i);
                    GameObject obj = (GameObject)errProp.FindPropertyRelative("errorObj").objectReferenceValue;
                    string errStr = errProp.FindPropertyRelative("error").stringValue;
                    Rect buttonRect = new Rect(xx, yy, ww, EditorGUIUtility.singleLineHeight);
                    if (GUI.Button(buttonRect, "Select"))
                    {
                        if (obj != null) Selection.activeGameObject = obj;
                    }
                    xx += ww;
                    ww = 500;
                    EditorGUI.LabelField(new Rect(xx, yy, ww, EditorGUIUtility.singleLineHeight), errStr, redFont);
                }
            }

            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            SerializedProperty errorsProp = property.serializedObject.FindProperty(property.propertyPath + ".objsWithErrors");
            return base.GetPropertyHeight(property, label) + errorsProp.arraySize * EditorGUIUtility.singleLineHeight;
        }
    }
}