示例#1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (restoreStateMethod == null)
            {
                restoreStateMethod   = typeof(UnityEventDrawer).GetMethod("RestoreState", instanceFlags);
                reorderableListField = typeof(UnityEventDrawer).GetField("m_ReorderableList", instanceFlags);

                persistentCallType = System.Type.GetType("UnityEngine.Events.PersistentCall,UnityEngine");
                if (persistentCallType != null)
                {
                    findMethodMethod        = typeof(UnityEventBase).GetMethod("FindMethod", instanceFlags, null, new System.Type[] { persistentCallType }, null);
                    persistentCallGroupType = System.Type.GetType("UnityEngine.Events.PersistentCallGroup,UnityEngine");
                    if (persistentCallGroupType != null)
                    {
                        getListenerMethod = persistentCallGroupType.GetMethod("GetListener", instanceFlags);
                    }
                    persistentCallsField = typeof(UnityEventBase).GetField("m_PersistentCalls", instanceFlags);
                    listenersArrayField  = typeof(UnityEventDrawer).GetField("m_ListenersArray", instanceFlags);

                    rebuildMethod = typeof(UnityEventBase).GetMethod("RebuildPersistentCallsIfNeeded", instanceFlags | BindingFlags.FlattenHierarchy);
                }
            }

            if (restoreStateMethod == null || reorderableListField == null || findMethodMethod == null ||
                getListenerMethod == null || persistentCallsField == null || listenersArrayField == null ||
                rebuildMethod == null)
            {
                base.OnGUI(position, property, label);
                return;
            }

            restoreStateMethod.Invoke(this, new object[] { property });

            var reorderableList = reorderableListField.GetValue(this) as UnityEditorInternal.ReorderableList;

            if (reorderableList == null)
            {
                base.OnGUI(position, property, label);
                return;
            }

            if (originalCallback == null || originalCallback.Target == null)
            {
                originalCallback = reorderableList.drawElementCallback;
            }
            reorderableList.drawElementCallback = DrawEventListener;

            //...

            listenersArray = listenersArrayField.GetValue(this) as SerializedProperty;
            if (listenersArray != null && !listenersArray.isArray)
            {
#if SI3_WARNINGS
                Debug.LogWarning(listenersArray);
#endif
                listenersArray = null;
            }

            object    targetObject = property.serializedObject.targetObject;
            FieldInfo targetField  = null;
            var       fieldNames   = property.propertyPath.Split(propertyPathSeparators, System.StringSplitOptions.RemoveEmptyEntries);
            for (var i = 0; targetObject != null && i < fieldNames.Length; ++i)
            {
                if (fieldNames[i] == "Array")
                {
                    targetField = null;

                    var array = targetObject as IList;
                    if (array == null)
                    {
                        break;
                    }
                    var index = int.Parse(fieldNames[i += 2]);
                    if (index >= array.Count)
                    {
                        break;
                    }
                    targetObject = array[index];
                }
                else
                {
                    targetField  = targetObject.GetType().GetField(fieldNames[i], instanceFlags);
                    targetObject = targetField.GetValue(targetObject);
                }
            }
            dummyEvent = targetObject as UnityEventBase;

            base.OnGUI(position, property, label);

            if (targetField != null)
            {
                if (System.Attribute.IsDefined(targetField, typeof(TooltipAttribute)))
                {
                    var attribute = (TooltipAttribute)System.Attribute.GetCustomAttribute(fieldInfo, typeof(TooltipAttribute));
                    if (attribute.tooltip != "")
                    {
                        position.height     = 18f;
                        tempContent.tooltip = attribute.tooltip;
                        GUI.Label(position, tempContent);
                    }
                }
            }
        }
        protected bool InitializeForProperty(SerializedProperty property)
        {
            if (restoreStateMethod == null)
            {
                restoreStateMethod   = typeof(UnityEventDrawer).GetMethod("RestoreState", instanceFlags);
                reorderableListField = typeof(UnityEventDrawer).GetField("m_ReorderableList", instanceFlags);

                dummyEventField = typeof(UnityEventDrawer).GetField("m_DummyEvent", instanceFlags);
                textField       = typeof(UnityEventDrawer).GetField("m_Text", instanceFlags);

                persistentCallType = System.Type.GetType("UnityEngine.Events.PersistentCall,UnityEngine");
                if (persistentCallType != null)
                {
                    findMethodMethod        = typeof(UnityEventBase).GetMethod("FindMethod", instanceFlags, null, new System.Type[] { persistentCallType }, null);
                    persistentCallGroupType = System.Type.GetType("UnityEngine.Events.PersistentCallGroup,UnityEngine");
                    if (persistentCallGroupType != null)
                    {
                        getListenerMethod = persistentCallGroupType.GetMethod("GetListener", instanceFlags);
                    }
                    persistentCallsField = typeof(UnityEventBase).GetField("m_PersistentCalls", instanceFlags);
                    listenersArrayField  = typeof(UnityEventDrawer).GetField("m_ListenersArray", instanceFlags);

                    rebuildMethod = typeof(UnityEventBase).GetMethod("RebuildPersistentCallsIfNeeded", instanceFlags | BindingFlags.FlattenHierarchy);
                }
            }

            if (restoreStateMethod == null || reorderableListField == null || findMethodMethod == null ||
                getListenerMethod == null || persistentCallsField == null || listenersArrayField == null ||
                rebuildMethod == null)
            {
                return(false);
            }

            expanded = property.isExpanded;

            restoreStateMethod.Invoke(this, new object[] { property });

            var reorderableList = reorderableListField.GetValue(this) as UnityEditorInternal.ReorderableList;

            if (reorderableList == null)
            {
                return(false);
            }

            if (originalCallback == null || originalCallback.Target == null)
            {
                originalCallback = reorderableList.drawElementCallback;
            }
            reorderableList.drawElementCallback = DrawEventListener;

            //...

            listenersArray = listenersArrayField.GetValue(this) as SerializedProperty;
            if (listenersArray != null && !listenersArray.isArray)
            {
#if SI3_WARNINGS
                Debug.LogWarning(listenersArray);
#endif
                listenersArray = null;
            }

            object targetObject = property.serializedObject.targetObject;
            targetField = null;
            var fieldNames = property.propertyPath.Split(propertyPathSeparators, System.StringSplitOptions.RemoveEmptyEntries);
            for (var i = 0; targetObject != null && i < fieldNames.Length; ++i)
            {
                if (fieldNames[i] == "Array")
                {
                    targetField = null;

                    var array = targetObject as IList;
                    if (array == null)
                    {
                        break;
                    }
                    var index = int.Parse(fieldNames[i += 2]);
                    if (index >= array.Count)
                    {
                        break;
                    }
                    targetObject = array[index];
                }
                else
                {
                    for (var type = targetObject.GetType(); type != typeof(object); type = type.BaseType)
                    {
                        targetField = type.GetField(fieldNames[i], instanceFlags | BindingFlags.DeclaredOnly);
                        if (targetField != null)
                        {
                            break;
                        }
                    }
                    if (targetField == null)
                    {
                        Debug.LogWarning("Could not find field #" + i + " in type " + targetObject.GetType().FullName + "\n" + string.Join(", ", fieldNames));
                        targetObject = null;
                        break;
                    }
                    targetObject = targetField.GetValue(targetObject);
                }
            }
            currentEvent = targetObject as UnityEventBase;

            return(true);
        }
        public static void DoReorderableList <TSetting>(TSetting setting, IList list, Type listType, ref UnityEditorInternal.ReorderableList mReorderableList, ref Vector2 mReorderableScrollPosition, ref Vector2 mSettingsScrollPosition, UnityEditorInternal.ReorderableList.ElementCallbackDelegate drawElementCallback = null, Action enableAction = null, Action disableAction = null) where TSetting : ScriptableObject
        {
            if (mReorderableList == null)
            {
                mReorderableList = new UnityEditorInternal.ReorderableList(list, listType, true, false, false, false);
                mReorderableList.headerHeight          = 0;
                mReorderableList.showDefaultBackground = false;
                mReorderableList.drawElementCallback   = drawElementCallback;
            }
            QuickGUILayout.Space();
            using (new QuickEditorGUILayout.HorizontalBlock())
            {
                QuickGUILayout.Space();
                if (GUILayout.Button(EditorGUIUtility.FindTexture("Toolbar Plus"), GUIStyle.none, GUILayout.Width(16)))
                {
                    list.Add(Activator.CreateInstance(listType));
                    mReorderableList.index = list.Count - 1;
                    mReorderableList.GrabKeyboardFocus();
                    mSettingsScrollPosition.y = float.MaxValue;
                }
                QuickGUILayout.Space();
                if (GUILayout.Button(EditorGUIUtility.FindTexture("Toolbar Minus"), GUIStyle.none, GUILayout.Width(16)))
                {
                    if (mReorderableList.index >= 0 && mReorderableList.index <= list.Count - 1)
                    {
                        Undo.RecordObject(setting, "Removed Import Preset");
                        list.RemoveAt(mReorderableList.index);
                        mReorderableList.index = Mathf.Max(0, mReorderableList.index - 1);
                        mReorderableList.GrabKeyboardFocus();
                    }
                }
                QuickGUILayout.FlexibleSpace();
                if (enableAction != null)
                {
                    QuickGUILayout.Button("Enable.All", EditorStyles.miniButtonRight, () =>
                    {
                        enableAction();
                    });
                }
                if (disableAction != null)
                {
                    QuickGUILayout.Button("Disable.All", EditorStyles.miniButtonLeft, () =>
                    {
                        disableAction();
                    });
                }
            }

            //QEditorGUIStaticAPI.Space();
            QuickGUILayout.DrawRect(EditorGUILayout.GetControlRect(false, 2), QuickEditorColors.WhiteCoffee);

            using (var scroll = new EditorGUILayout.ScrollViewScope(mReorderableScrollPosition))
            {
                mReorderableScrollPosition = scroll.scrollPosition;
                mReorderableList.DoLayoutList();
            }
        }