コード例 #1
0
        public static GenericMenu FillPropertyContextMenu(RuntimeSerializedProperty property, RuntimeSerializedProperty linkedProperty = null)
        {
            GenericMenu popupMenu = new GenericMenu();
            RuntimeSerializedProperty propertyWithPath = property.RuntimeSerializedObject.FindProperty(property.PropertyPath);

            RuntimeScriptAttributeUtility.GetHandler(property, null).AddMenuItems(property, popupMenu);

            if (property.RuntimeSerializedObject.SerializedObject.targetObjects.Length == 1 && property.IsInstantiatedPrefab)
            {
                popupMenu.AddItem(EditorGUIUtility.TrTextContent("Revert Value to Prefab"), false, RuntimeTargetChoiceHandler.RevertPrefabPropertyOverride, propertyWithPath);
            }

            // If property is an element in an array, show duplicate and delete menu options
            if (property.Depth > 0 && !property.PropertyPath.EndsWith("Size"))
            {
                var length     = property.PropertyPath.LastIndexOf(".");
                var parentPath = property.PropertyPath.Substring(0, length);
                var prop       = property.FindProperty(parentPath);

                if (prop != null && prop.IsArray)
                {
                    if (popupMenu.GetItemCount() > 0)
                    {
                        popupMenu.AddSeparator(EmptyStr);
                    }
                    popupMenu.AddItem(EditorGUIUtility.TrTextContent("Duplicate Array Element"), false, (object a) =>
                    {
                        RuntimeTargetChoiceHandler.DuplicateArrayElement(a);
                        EditorGUIUtility.editingTextField = false;
                    }, propertyWithPath);
                    popupMenu.AddItem(EditorGUIUtility.TrTextContent("Delete Array Element"), false, (object a) =>
                    {
                        RuntimeTargetChoiceHandler.DeleteArrayElement(a);
                        EditorGUIUtility.editingTextField = false;
                    }, propertyWithPath);
                }
            }

            // If shift is held down, show debug menu options
            if (Event.current.shift)
            {
                if (popupMenu.GetItemCount() > 0)
                {
                    popupMenu.AddSeparator(EmptyStr);
                }
                popupMenu.AddItem(EditorGUIUtility.TrTextContent("Print Property Path"), false, e => Debug.Log(((SerializedProperty)e).propertyPath), propertyWithPath);
            }
            else
            {
                if (popupMenu.GetItemCount() > 0)
                {
                    popupMenu.AddSeparator(EmptyStr);
                }
                popupMenu.AddItem(EditorGUIUtility.TrTextContent("Copy Component"), false, (object a) =>
                {
                    RuntimeTargetChoiceHandler.CopyComponent(a);
                }, propertyWithPath);
                if (RuntimeTargetChoiceHandler.CanPasteAsNew(propertyWithPath))
                {
                    popupMenu.AddItem(EditorGUIUtility.TrTextContent("Paste Component As New"), false, (object a) =>
                    {
                        RuntimeTargetChoiceHandler.PasteComponentAsNew(a);
                    }, propertyWithPath);
                }
                else
                {
                    popupMenu.AddDisabledItem(EditorGUIUtility.TrTextContent("Paste Component As New"));
                }
                if (RuntimeTargetChoiceHandler.CanPaste(propertyWithPath))
                {
                    popupMenu.AddItem(EditorGUIUtility.TrTextContent("Paste Component Values"), false, (object a) =>
                    {
                        RuntimeTargetChoiceHandler.PasteComponentValues(a);
                    }, propertyWithPath);
                }
                else
                {
                    popupMenu.AddDisabledItem(EditorGUIUtility.TrTextContent("Paste Component Values"));
                }
            }

            if (property.RuntimeSerializedObject.Target != null)
            {
                if (popupMenu.GetItemCount() > 0)
                {
                    popupMenu.AddSeparator(EmptyStr);
                }

                var obj     = propertyWithPath.RuntimeSerializedObject.Target;
                var methods = obj.GetType().GetAllInstanceMethods();

                contextMenuIndex.Clear();
                contextMenuResultIndex.Clear();
                easyContextMenuResultIndex.Clear();

                foreach (var method in methods)
                {
                    foreach (ContextMenu contextMenu in method.GetCustomAttributes(typeof(ContextMenu), false))
                    {
                        contextMenuIndex.Add(contextMenu, method);
                    }
                }
                var orderedContextMenu = contextMenuIndex.Select(kvp => kvp.Key).OrderByDescending(menu => menu.validate).ThenBy(menu => menu.priority).ToArray();
                foreach (var menu in orderedContextMenu)
                {
                    if (menu.validate)
                    {
                        var result = (bool)contextMenuIndex[menu].Invoke(obj, null);
                        if (contextMenuResultIndex.ContainsKey(menu.menuItem))
                        {
                            if (contextMenuResultIndex[menu.menuItem] && !result)
                            {
                                contextMenuResultIndex[menu.menuItem] = result;
                            }
                        }
                        else
                        {
                            contextMenuResultIndex.Add(menu.menuItem, result);
                        }
                    }
                    else
                    {
                        if (contextMenuResultIndex.ContainsKey(menu.menuItem) && !contextMenuResultIndex[menu.menuItem])
                        {
                            popupMenu.AddDisabledItem(EditorGUIUtility.TrTextContent(menu.menuItem));
                        }
                        else
                        {
                            popupMenu.AddItem(EditorGUIUtility.TrTextContent(menu.menuItem), false, (object o) =>
                            {
                                contextMenuIndex[menu].Invoke(o, null);
                            }, obj);
                        }
                    }
                }
            }
            return(popupMenu);
        }
コード例 #2
0
        public RuntimeSerializedProperty FindProperty(string propertyPath)
        {
            RuntimeSerializedProperty property = GetIterator();

            return(property.FindProperty(propertyPath));
        }