Пример #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            UnityEngine.Event e = UnityEngine.Event.current;
            controlId = EditorGUIUtility.GetControlID(GetHashCode(), FocusType.Passive);
            switch (e.GetTypeForControl(controlId))
            {
            default:

                position = EditorGUI.IndentedRect(position);
                Rect pos2 = position;
                pos2.width -= 32;
                Rect pos3 = position;
                pos3.x    += pos2.width;
                pos3.width = 15;
                Rect pos4 = position;
                pos4.x    += pos2.width + pos3.width;
                pos4.width = 15;

                var    typeName = property.FindPropertyRelative("m_Name");
                string name     = (typeName != null && !string.IsNullOrEmpty(typeName.stringValue) ? typeName.stringValue : "None");

                GUI.Label(pos2, name);

                if (GUI.Button(pos3, "s"))
                {
                    TypePickerAttribute attr = attribute as TypePickerAttribute;
                    if (attr != null)
                    {
                        TypePicker.Show(controlId, attr.baseType);
                    }
                }

                if (GUI.Button(pos4, "x"))
                {
                    var assemblyQualifiedName = property.FindPropertyRelative("m_AssemblyQualifiedName");
                    var nameProperty          = property.FindPropertyRelative("m_Name");
                    var assemblyName          = property.FindPropertyRelative("m_AssemblyName");

                    assemblyQualifiedName.stringValue = "";
                    nameProperty.stringValue          = "";
                    assemblyName.stringValue          = "";

                    property.serializedObject.ApplyModifiedProperties();
                }

                if (e.commandName == "TypePickerSelectionChanged" || e.commandName == "TypePickerClosed")
                {
                    if (TypePicker.GetControlId() == controlId)
                    {
                        GUI.changed = true;
                        var selection = TypePicker.GetSelection();
                        if (selection.Length > 0 && selection[0] != null)
                        {
                            var assemblyQualifiedName = property.FindPropertyRelative("m_AssemblyQualifiedName");
                            var nameProperty          = property.FindPropertyRelative("m_Name");
                            var assemblyName          = property.FindPropertyRelative("m_AssemblyName");

                            assemblyQualifiedName.stringValue = selection[0].AssemblyQualifiedName;
                            nameProperty.stringValue          = selection[0].Name;
                            assemblyName.stringValue          = selection[0].FullName;

                            property.serializedObject.ApplyModifiedProperties();
                        }
                    }
                }
                break;
            }
        }
        public static void Show(int controlId, Type baseType)
        {
            PropertyInfo prop = Assembly.GetAssembly(typeof(EditorWindow)).GetType("UnityEditor.GUIView")
                                .GetProperty("current", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public);
            object o = prop.GetValue(null, BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty, null, null,
                                     null);

            TypePicker picker = ScriptableObject.CreateInstance <TypePicker>();

            picker.ShowAuxWindow();
            TypePicker.controlId = controlId;

            Rect position = picker.position;

            position.width  = EditorPrefs.GetFloat("ObjectSelectorWidth", 200f);
            position.height = EditorPrefs.GetFloat("ObjectSelectorHeight", 390f);
            position.x      = EditorPrefs.GetFloat("ObjectSelectorXPos", 100);
            position.y      = EditorPrefs.GetFloat("ObjectSelectorYPos", 100);
            picker.minSize  = new Vector2(200f, 335f);
            picker.maxSize  = new Vector2(10000f, 10000f);

            picker.position = position;

            picker.tree = new TypePickerTreeView(new TreeViewState(), baseType);

            picker.tree.OnItemDoubleClicked = id =>
            {
                selectedItems = new Type[1] {
                    picker.tree.GetItemById(id)
                };
                UnityEngine.Event e = EditorGUIUtility.CommandEvent("TypePickerClosed");
                if (o != null)
                {
                    MethodInfo m = o.GetType().GetMethod("SendEvent",
                                                         BindingFlags.Instance | BindingFlags.NonPublic |
                                                         BindingFlags.FlattenHierarchy);
                    m.Invoke(o, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { e }, null);
                }
                if (selectedItems[0] != null)
                {
                    picker.Close();
                }
            };

            picker.tree.OnSelectionChanged = selectedIds =>
            {
                selectedItems = new Type[selectedIds.Count];
                for (int i = 0; i < selectedIds.Count; i++)
                {
                    int selectedId = selectedIds[i];
                    selectedItems[i] = picker.tree.GetItemById(selectedId);
                }

                selectedItems = selectedItems.Where(x => x != null).ToArray();
                UnityEngine.Event e = EditorGUIUtility.CommandEvent("TypePickerSelectionChanged");
                if (o != null)
                {
                    MethodInfo m = o.GetType().GetMethod("SendEvent",
                                                         BindingFlags.Instance | BindingFlags.NonPublic |
                                                         BindingFlags.FlattenHierarchy);
                    m.Invoke(o, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { e }, null);
                }
            };
        }