Esempio n. 1
0
        public static void CopyComponent(object userData)
        {
            RuntimeSerializedProperty runtimeSerializedProperty = userData as RuntimeSerializedProperty;
            var type = runtimeSerializedProperty.RuntimeSerializedObject.Type;

            Data = RuntimeObject.ToJson(runtimeSerializedProperty.RuntimeSerializedObject.Target);
            if (Clipboard.ContainsKey(type))
            {
                Clipboard[type] = runtimeSerializedProperty.RuntimeSerializedObject;
            }
            else
            {
                Clipboard.Add(type, runtimeSerializedProperty.RuntimeSerializedObject);
            }
        }
Esempio n. 2
0
 public bool UpdateIfRequiredOrScript()
 {
     if (owner is RuntimeSerializedObject)
     {
         var parentProperty = ParentProperty as RuntimeSerializedProperty;
         parentProperty.StringValue = RuntimeObject.ToJson(Target);
         parentProperty.RuntimeSerializedObject.UpdateIfRequiredOrScript();
         Debug.Log("UpdateIfRequiredOrScript [InstanceID : " + parentProperty.RuntimeSerializedObject.GetInstanceID() + "]");
     }
     else if (owner is SerializedObject)
     {
         var parentProperty = ParentProperty as SerializedProperty;
         parentProperty.stringValue = RuntimeObject.ToJson(Target);
         EditorUtility.SetDirty(TargetObject);
         parentProperty.serializedObject.ApplyModifiedProperties();
         parentProperty.serializedObject.UpdateIfRequiredOrScript();
         Debug.Log("UpdateIfRequiredOrScript [InstanceID : " + parentProperty.serializedObject.GetHashCode() + "]");
     }
     HasModifiedProperties = false;
     return(true);
 }
        protected virtual void OnAddDropdown(Rect position, ReorderableList list, System.Type type)
        {
            GenericMenu popupMenu = new GenericMenu();
            Dictionary <ContextMenuAttribute, System.Type> dropdownTypes = null;

            if (!dropdownTypeDict.TryGetValue(type, out dropdownTypes))
            {
                dropdownTypes = new Dictionary <ContextMenuAttribute, System.Type>();

                foreach (var t in Types.Where(t => type.IsAssignableFrom(t)))
                {
                    if (t.HasAttribute <ContextMenuAttribute>())
                    {
                        dropdownTypes.Add(t.AllAttributes <ContextMenuAttribute>().FirstOrDefault(), t);
                    }
                    else
                    {
                        dropdownTypes.Add(new ContextMenuAttribute(t.Name, false, int.MaxValue), t);
                    }
                }

                dropdownTypes = dropdownTypes.OrderBy(i => i.Key.Priority).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            }

            if (dropdownTypes.Count > 0)
            {
                foreach (var kvp in dropdownTypes)
                {
                    var content = new GUIContent(kvp.Key.MenuItem);
                    var index   = list.serializedProperty.arraySize;
                    if (kvp.Value == typeof(GameObject) || kvp.Value.IsSubclassOf(typeof(Component)))
                    {
                        popupMenu.AddDisabledItem(content);
                    }
                    else if (kvp.Value.IsSubclassOf(typeof(ScriptableObject)))
                    {
                        popupMenu.AddItem(content, false, () =>
                        {
                            var scriptableObject = CreateInstance(kvp.Value);

                            Object assetObject = null;
                            if (target is ScriptableObject)
                            {
                                assetObject = target;
                            }
                            else
                            {
#if UNITY_2018_3_OR_NEWER
                                assetObject = PrefabUtility.GetCorrespondingObjectFromSource(target) ?? PrefabUtility.GetPrefabInstanceHandle(target);
#elif UNITY_2018_2
                                assetObject = PrefabUtility.GetCorrespondingObjectFromSource(target) ?? PrefabUtility.GetPrefabObject(target);
#else
                                assetObject = PrefabUtility.GetPrefabParent(target) ?? PrefabUtility.GetPrefabObject(target);
#endif
                            }
                            if (assetObject != null)
                            {
                                scriptableObject.name = kvp.Value.Name;
                                AssetDatabase.AddObjectToAsset(scriptableObject, assetObject);
                                list.serializedProperty.arraySize++;
                                list.index = index;
                                list.serializedProperty.GetArrayElementAtIndex(index).objectReferenceValue = scriptableObject;
                            }
                            else
                            {
#if UNITY_EDITOR
                                Debug.LogWarning("You can't add " + scriptableObject + " to a scene object!", target);
#endif
                            }
                            serializedObject.ApplyModifiedProperties();
                        });
                    }
                    else
                    {
                        popupMenu.AddItem(content, false, () =>
                        {
                            var obj = System.Activator.CreateInstance(kvp.Value);
                            list.serializedProperty.arraySize++;
                            list.index = index;
                            list.serializedProperty.GetArrayElementAtIndex(index).stringValue = RuntimeObject.ToJson(kvp.Value.ToString(), JsonUtility.ToJson(obj));
                            serializedObject.ApplyModifiedProperties();
                        });
                    }
                }
            }
            popupMenu.ShowAsContext();
        }
Esempio n. 4
0
        protected virtual void OnAddDropdown(Rect position, RuntimeReorderableList list, System.Type type)
        {
            GenericMenu popupMenu = new GenericMenu();
            Dictionary <ContextMenuAttribute, System.Type> dropdownTypes = null;

            if (!dropdownTypeDict.TryGetValue(type, out dropdownTypes))
            {
                dropdownTypes = new Dictionary <ContextMenuAttribute, System.Type>();

                foreach (var t in EasyEditor.Types.Where(t => type.IsAssignableFrom(t)))
                {
                    if (t.HasAttribute <ContextMenuAttribute>())
                    {
                        dropdownTypes.Add(t.AllAttributes <ContextMenuAttribute>().FirstOrDefault(), t);
                    }
                    else
                    {
                        dropdownTypes.Add(new ContextMenuAttribute(t.Name, false, int.MaxValue), t);
                    }
                }

                dropdownTypes = dropdownTypes.OrderBy(i => i.Key.Priority).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            }

            if (dropdownTypes.Count > 0)
            {
                foreach (var kvp in dropdownTypes)
                {
                    var content = new GUIContent(kvp.Key.MenuItem);
                    var index   = list.RuntimeSerializedProperty.ArraySize;
                    if (kvp.Value == typeof(GameObject) || kvp.Value.IsSubclassOf(typeof(Component)))
                    {
                        popupMenu.AddDisabledItem(content);
                    }
                    else if (kvp.Value.IsSubclassOf(typeof(ScriptableObject)))
                    {
                        // Reference type is not supported!
                    }
                    else
                    {
                        popupMenu.AddItem(content, false, () =>
                        {
                            var obj = System.Activator.CreateInstance(kvp.Value);
                            list.RuntimeSerializedProperty.ArraySize++;
                            list.index = index;
                            list.RuntimeSerializedProperty.GetArrayElementAtIndex(index).StringValue = RuntimeObject.ToJson(kvp.Value.ToString(), JsonUtility.ToJson(obj));
                            list.RuntimeSerializedProperty.RuntimeSerializedObject.ApplyModifiedProperties();
                        });
                    }
                }
            }
            popupMenu.ShowAsContext();
        }