Beispiel #1
0
        public static void RenderEventSettings(SerializedProperty eventItem, int index, InteractableTypesContainer options, InspectorUIUtility.MultiListButtonEvent changeEvent, InspectorUIUtility.ListButtonEvent removeEvent)
        {
            EditorGUILayout.BeginVertical("Box");
            SerializedProperty uEvent                = eventItem.FindPropertyRelative("Event");
            SerializedProperty eventName             = eventItem.FindPropertyRelative("Name");
            SerializedProperty className             = eventItem.FindPropertyRelative("ClassName");
            SerializedProperty assemblyQualifiedName = eventItem.FindPropertyRelative("AssemblyQualifiedName");
            SerializedProperty hideEvents            = eventItem.FindPropertyRelative("HideUnityEvents");

            // show event dropdown
            int id = InspectorUIUtility.ReverseLookup(className.stringValue, options.ClassNames);

            Rect       position    = EditorGUILayout.GetControlRect();
            GUIContent selectLable = new GUIContent("Select Event Type", "Select the event type from the list");

            EditorGUI.BeginProperty(position, selectLable, className);
            {
                int newId = EditorGUI.Popup(position, selectLable.text, id, options.ClassNames);

                if (id != newId || String.IsNullOrEmpty(className.stringValue))
                {
                    className.stringValue             = options.ClassNames[newId];
                    assemblyQualifiedName.stringValue = options.AssemblyQualifiedNames[newId];

                    changeEvent(new int[] { index, newId }, eventItem);
                }
            }
            EditorGUI.EndProperty();

            if (!hideEvents.boolValue)
            {
                EditorGUILayout.PropertyField(uEvent, new GUIContent(eventName.stringValue));
            }

            // show event properties
            EditorGUI.indentLevel = indentOnSectionStart + 1;
            SerializedProperty eventSettings = eventItem.FindPropertyRelative("Settings");

            for (int j = 0; j < eventSettings.arraySize; j++)
            {
                SerializedProperty propertyField = eventSettings.GetArrayElementAtIndex(j);
                bool isEvent = InspectorFieldsUtility.IsPropertyType(propertyField, InspectorField.FieldTypes.Event);

                if (!hideEvents.boolValue || !isEvent)
                {
                    InspectorFieldsUtility.DisplayPropertyField(eventSettings.GetArrayElementAtIndex(j));
                }
            }
            EditorGUI.indentLevel = indentOnSectionStart;

            EditorGUILayout.Space();

            if (removeEvent != null)
            {
                InspectorUIUtility.FlexButton(new GUIContent("Remove Event"), index, removeEvent);
            }

            EditorGUILayout.EndVertical();
        }
        /// <summary>
        /// Update the given InteractableEvent to the new type (which extends ReceiverBase)
        /// </summary>
        /// <param name="newType">new receiverbase subclass type to target</param>
        /// <param name="eventItem">InteractableEvent to target and update</param>
        private static void EventChanged(Type newType, SerializedProperty eventItem)
        {
            SerializedProperty className             = eventItem.FindPropertyRelative("ClassName");
            SerializedProperty assemblyQualifiedName = eventItem.FindPropertyRelative("AssemblyQualifiedName");

            className.stringValue             = newType.Name;
            assemblyQualifiedName.stringValue = newType.AssemblyQualifiedName;

            SerializedProperty settings = eventItem.FindPropertyRelative("Settings");

            ReceiverBase defaultReceiver = (ReceiverBase)Activator.CreateInstance(newType, new UnityEvent());

            InspectorFieldsUtility.ClearSettingsList(settings, InspectorFieldsUtility.GetInspectorFields(defaultReceiver));
        }
        protected virtual void ChangeEvent(int[] indexArray, SerializedProperty prop = null)
        {
            SerializedProperty className  = prop.FindPropertyRelative("ClassName");
            SerializedProperty name       = prop.FindPropertyRelative("Name");
            SerializedProperty settings   = prop.FindPropertyRelative("Settings");
            SerializedProperty hideEvents = prop.FindPropertyRelative("HideUnityEvents");

            if (!String.IsNullOrEmpty(className.stringValue))
            {
                InteractableEvent.ReceiverData data = eventList[indexArray[0]].AddReceiver(eventTypes[indexArray[1]]);
                name.stringValue     = data.Name;
                hideEvents.boolValue = data.HideUnityEvents;

                InspectorFieldsUtility.PropertySettingsList(settings, data.Fields);
            }
        }
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            // display the fields virtually as a custom inspector
            for (int i = 0; i < settings.arraySize; i++)
            {
                SerializedProperty prop = settings.GetArrayElementAtIndex(i);
                InspectorFieldsUtility.DisplayPropertyField(prop);
            }

            serializedObject.ApplyModifiedProperties();

            // to apply during runtime - only needed for MonoBehaviours
            InspectorGenericFields <InspectorFieldsExample> .LoadSettings(example, example.Settings);
        }
Beispiel #5
0
        /// <summary>
        /// Invoked when the event is changed.
        /// </summary>
        /// <param name="indexArray">
        /// A two-element sized index array where the first element is the index of the
        /// event in the event list, and the second is the new event handler class that
        /// was selected.
        /// </param>
        protected virtual void ChangeEvent(int[] indexArray, SerializedProperty prop = null)
        {
            SerializedProperty className             = prop.FindPropertyRelative("ClassName");
            SerializedProperty name                  = prop.FindPropertyRelative("Name");
            SerializedProperty assemblyQualifiedName = prop.FindPropertyRelative("AssemblyQualifiedName");
            SerializedProperty settings              = prop.FindPropertyRelative("Settings");
            SerializedProperty hideEvents            = prop.FindPropertyRelative("HideUnityEvents");

            if (!String.IsNullOrEmpty(className.stringValue))
            {
                InteractableEvent.ReceiverData data = eventList[indexArray[0]].AddReceiver(eventOptions.Types[indexArray[1]]);
                name.stringValue = data.Name;
                // Technically not necessary due to how this is set in RenderEventSettings, nevertheless included to
                // make sure that wherever we set Name/ClassName, we always set AssemblyQualifiedName as well.
                // Performance wise this is not a huge deal due to how this is only triggered on changes in the inspector
                // in the editor (i.e. dropdown selection has changed, which requires explicit user input).
                assemblyQualifiedName.stringValue = eventOptions.AssemblyQualifiedNames[indexArray[1]];
                hideEvents.boolValue = data.HideUnityEvents;

                InspectorFieldsUtility.PropertySettingsList(settings, data.Fields);
            }
        }
        /// <summary>
        /// Render event properties for the given event item. If item has been removed, returns true. False otherwise
        /// </summary>
        /// <param name="eventItem">serialized property of the event item to render properties from</param>
        /// <returns>If item has been removed, returns true. False otherwise</returns>
        public static bool RenderEvent(SerializedProperty eventItem, bool canRemove = true)
        {
            using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
            {
                SerializedProperty uEvent                = eventItem.FindPropertyRelative("Event");
                SerializedProperty eventName             = eventItem.FindPropertyRelative("Name");
                SerializedProperty className             = eventItem.FindPropertyRelative("ClassName");
                SerializedProperty assemblyQualifiedName = eventItem.FindPropertyRelative("AssemblyQualifiedName");
                Type receiverType;

                InspectorUIUtility.DrawHeader("Event Receiver Type");
                using (new EditorGUILayout.HorizontalScope())
                {
                    Rect position = EditorGUILayout.GetControlRect();
                    using (new EditorGUI.PropertyScope(position, SelectEventLabel, className))
                    {
                        var receiverTypes      = TypeCacheUtility.GetSubClasses <ReceiverBase>();
                        var recevierClassNames = receiverTypes.Select(t => t?.Name).ToArray();
                        int id    = Array.IndexOf(recevierClassNames, className.stringValue);
                        int newId = EditorGUI.Popup(position, id, recevierClassNames);
                        if (newId == -1)
                        {
                            newId = 0;
                        }

                        receiverType = receiverTypes[newId];

                        // Temporary workaround to fix bug shipped in GA where assemblyQualifiedName was never set
                        if (string.IsNullOrEmpty(assemblyQualifiedName.stringValue))
                        {
                            assemblyQualifiedName.stringValue = receiverType.AssemblyQualifiedName;
                        }

                        if (id != newId)
                        {
                            EventChanged(receiverType, eventItem);
                        }
                    }

                    if (canRemove)
                    {
                        if (InspectorUIUtility.FlexButton(new GUIContent("Remove Event")))
                        {
                            return(true);
                        }
                    }
                }

                EditorGUILayout.Space();
                InspectorUIUtility.DrawHeader("Event Properties");

                ReceiverBase receiver = (ReceiverBase)Activator.CreateInstance(receiverType, new UnityEvent());

                if (!receiver.HideUnityEvents)
                {
                    EditorGUILayout.PropertyField(uEvent, new GUIContent(receiver.Name));
                }

                SerializedProperty eventSettings = eventItem.FindPropertyRelative("Settings");

                // If fields for given receiver class type have been changed, update the related inspector field data
                var fieldList = InspectorFieldsUtility.GetInspectorFields(receiver);
                if (!InspectorFieldsUtility.AreFieldsSame(eventSettings, fieldList))
                {
                    InspectorFieldsUtility.UpdateSettingsList(eventSettings, fieldList);
                }

                for (int index = 0; index < eventSettings.arraySize; index++)
                {
                    SerializedProperty propertyField = eventSettings.GetArrayElementAtIndex(index);
                    bool isEvent = InspectorFieldsUtility.IsPropertyType(propertyField, InspectorField.FieldTypes.Event);

                    if (!receiver.HideUnityEvents || !isEvent)
                    {
                        InspectorFieldsUtility.DisplayPropertyField(eventSettings.GetArrayElementAtIndex(index));
                    }
                }
            }

            return(false);
        }
        /// <summary>
        /// Render event properties for the given event item. If item has been removed, returns true. False otherwise
        /// </summary>
        /// <param name="eventItem">serialized property of the event item to render properties from</param>
        /// <param name="index">index of event item in higher order list</param>
        /// <param name="options">Event type options</param>
        /// <param name="changeEvent">Function to call if event properties have changed</param>
        /// <param name="removeEvent">Function to call if event requested to be removed</param>
        /// <returns>If item has been removed, returns true. False otherwise</returns>
        public static bool RenderEventSettings(SerializedProperty eventItem, int index, InteractableTypesContainer options, InspectorUIUtility.MultiListButtonEvent changeEvent, InspectorUIUtility.ListButtonEvent removeEvent)
        {
            using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
            {
                SerializedProperty uEvent                = eventItem.FindPropertyRelative("Event");
                SerializedProperty eventName             = eventItem.FindPropertyRelative("Name");
                SerializedProperty className             = eventItem.FindPropertyRelative("ClassName");
                SerializedProperty assemblyQualifiedName = eventItem.FindPropertyRelative("AssemblyQualifiedName");
                SerializedProperty hideEvents            = eventItem.FindPropertyRelative("HideUnityEvents");

                // show event dropdown
                int id = InspectorUIUtility.ReverseLookup(className.stringValue, options.ClassNames);

                using (new EditorGUILayout.HorizontalScope())
                {
                    Rect position = EditorGUILayout.GetControlRect();
                    EditorGUI.BeginProperty(position, SelectEventLabel, className);
                    {
                        int newId = EditorGUI.Popup(position, id, options.ClassNames);

                        if (id != newId || String.IsNullOrEmpty(className.stringValue))
                        {
                            className.stringValue             = options.ClassNames[newId];
                            assemblyQualifiedName.stringValue = options.AssemblyQualifiedNames[newId];

                            changeEvent(new int[] { index, newId }, eventItem);
                        }
                    }
                    EditorGUI.EndProperty();

                    if (removeEvent != null)
                    {
                        if (InspectorUIUtility.FlexButton(new GUIContent("Remove Event"), index, removeEvent))
                        {
                            return(true);
                        }
                    }
                }
                EditorGUILayout.Space();

                if (!hideEvents.boolValue)
                {
                    EditorGUILayout.PropertyField(uEvent, new GUIContent(eventName.stringValue));
                }

                // show event properties
                SerializedProperty eventSettings = eventItem.FindPropertyRelative("Settings");
                for (int j = 0; j < eventSettings.arraySize; j++)
                {
                    SerializedProperty propertyField = eventSettings.GetArrayElementAtIndex(j);
                    bool isEvent = InspectorFieldsUtility.IsPropertyType(propertyField, InspectorField.FieldTypes.Event);

                    if (!hideEvents.boolValue || !isEvent)
                    {
                        InspectorFieldsUtility.DisplayPropertyField(eventSettings.GetArrayElementAtIndex(j));
                    }
                }
            }

            return(false);
        }