Esempio n. 1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var attr = (SearchAssetsByTypePopupAttribute)this.attribute;

            var target = (string.IsNullOrEmpty(attr.innerField) == true ? property : property.FindPropertyRelative(attr.innerField));

            EditorGUI.LabelField(position, label);
            position.x     += EditorGUIUtility.labelWidth;
            position.width -= EditorGUIUtility.labelWidth;
            if (GUILayoutExt.DrawDropdown(position, new GUIContent(target.objectReferenceValue != null ? EditorHelpers.StringToCaption(target.objectReferenceValue.name) : attr.noneOption), FocusType.Passive, target.objectReferenceValue) == true)
            {
                var rect   = position;
                var vector = GUIUtility.GUIToScreenPoint(new Vector2(rect.x, rect.y));
                rect.x = vector.x;
                rect.y = vector.y;

                var popup = new Popup()
                {
                    title = attr.menuName, autoClose = true, screenRect = new Rect(rect.x, rect.y + rect.height, rect.width, 200f)
                };
                var objects = AssetDatabase.FindAssets("t:" + (attr.filterType != null ? attr.filterType.Name : "Object"), attr.filterDir == null ? null : new [] { attr.filterDir });
                if (string.IsNullOrEmpty(attr.noneOption) == false)
                {
                    popup.Item(attr.noneOption, null, searchable: false, action: (item) => {
                        property.serializedObject.Update();
                        target.objectReferenceValue = null;
                        property.serializedObject.ApplyModifiedProperties();
                    }, order: -1);
                }

                for (int i = 0; i < objects.Length; ++i)
                {
                    var path  = AssetDatabase.GUIDToAssetPath(objects[i]);
                    var asset = AssetDatabase.LoadAssetAtPath <Object>(path);
                    popup.Item(EditorHelpers.StringToCaption(asset.name), () => {
                        property.serializedObject.Update();
                        target.objectReferenceValue = asset;
                        property.serializedObject.ApplyModifiedProperties();
                    });
                }
                popup.Show();
            }
        }
Esempio n. 2
0
        public static void DrawGUI(Rect position, GUIContent label, SearchComponentsByTypePopupAttribute attr, SerializedProperty property)
        {
            var searchType = attr.baseType;

            if (attr.allowClassOverrides == true && property.serializedObject.targetObject is ISearchComponentByTypeEditor searchComponentByTypeEditor)
            {
                searchType = searchComponentByTypeEditor.GetSearchType();
            }

            IList searchArray = null;
            var   singleOnly  = false;

            if (attr.singleOnly == true && property.serializedObject.targetObject is ISearchComponentByTypeSingleEditor searchComponentByTypeSingleEditor)
            {
                searchArray = searchComponentByTypeSingleEditor.GetSearchTypeArray();
                singleOnly  = true;
            }

            var target      = (string.IsNullOrEmpty(attr.innerField) == true ? property : property.FindPropertyRelative(attr.innerField));
            var displayName = string.Empty;

            if (target.objectReferenceValue != null)
            {
                var compDisplayAttrs = target.objectReferenceValue.GetType().GetCustomAttributes(typeof(UnityEngine.UI.Windows.ComponentModuleDisplayNameAttribute), true);
                if (compDisplayAttrs.Length > 0)
                {
                    var compDisplayAttr = (UnityEngine.UI.Windows.ComponentModuleDisplayNameAttribute)compDisplayAttrs[0];
                    displayName = compDisplayAttr.name;
                }
                else
                {
                    displayName = EditorHelpers.StringToCaption(target.objectReferenceValue.GetType().Name);
                }
            }

            EditorGUI.LabelField(position, label);
            position.x     += EditorGUIUtility.labelWidth;
            position.width -= EditorGUIUtility.labelWidth;
            if (GUILayoutExt.DrawDropdown(position, new GUIContent(target.objectReferenceValue != null ? displayName : attr.noneOption), FocusType.Passive, target.objectReferenceValue) == true)
            {
                var rect   = position;
                var vector = GUIUtility.GUIToScreenPoint(new Vector2(rect.x, rect.y));
                rect.x = vector.x;
                rect.y = vector.y;

                var popup = new Popup()
                {
                    title = attr.menuName, autoClose = true, screenRect = new Rect(rect.x, rect.y + rect.height, rect.width, 200f)
                };
                if (string.IsNullOrEmpty(attr.noneOption) == false)
                {
                    popup.Item(attr.noneOption, null, searchable: false, action: (item) => {
                        property.serializedObject.Update();
                        if (property.objectReferenceValue != null)
                        {
                            Object.DestroyImmediate(property.objectReferenceValue, true);
                            property.objectReferenceValue = null;
                        }

                        property.serializedObject.ApplyModifiedProperties();
                    }, order: -1);
                }

                var allTypes = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).ToArray();//searchType.Assembly.GetTypes();
                foreach (var type in allTypes)
                {
                    if (type.IsSubclassOf(searchType) == true)
                    {
                        var itemType = type;
                        if (singleOnly == true)
                        {
                            var found = false;
                            foreach (var item in searchArray)
                            {
                                if (item == null)
                                {
                                    continue;
                                }
                                if (item.GetType().IsSubclassOf(itemType) == true)
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (found == true)
                            {
                                continue;
                            }
                        }

                        var compDisplayAttrs = type.GetCustomAttributes(typeof(UnityEngine.UI.Windows.ComponentModuleDisplayNameAttribute), true);
                        if (compDisplayAttrs.Length > 0)
                        {
                            var compDisplayAttr = (UnityEngine.UI.Windows.ComponentModuleDisplayNameAttribute)compDisplayAttrs[0];
                            displayName = compDisplayAttr.name;
                        }
                        else
                        {
                            displayName = EditorHelpers.StringToCaption(type.Name);
                        }

                        popup.Item(displayName, () => {
                            property.serializedObject.Update();
                            var go = (property.serializedObject.targetObject as Component).gameObject;
                            if (property.objectReferenceValue != null)
                            {
                                Object.DestroyImmediate(property.objectReferenceValue, true);
                                property.objectReferenceValue = null;
                            }
                            property.objectReferenceValue = go.AddComponent(itemType);
                            property.serializedObject.ApplyModifiedProperties();
                        });
                    }
                }

                popup.Show();
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            //EditorGUI.PropertyField(position, property, label);

            var attr = this.attr;
            var go   = (property.serializedObject.targetObject as Component).gameObject;

            if (go == null)
            {
                return;
            }

            var pth = AssetDatabase.GetAssetPath(go);

            if (string.IsNullOrEmpty(pth) == true)
            {
                return;
            }

            var assetPath     = System.IO.Path.GetDirectoryName(pth);
            var screensMarker = "Screens";

            if (assetPath.EndsWith(screensMarker) == true)
            {
                assetPath  = assetPath.Remove(assetPath.Length - screensMarker.Length);
                assetPath += "Layouts";
            }
            attr.filterDir = assetPath;

            var target       = property;
            var so           = property.serializedObject;
            var targetObject = so.targetObject;
            var targetPath   = target.propertyPath;

            EditorGUI.LabelField(position, label);
            position.x     += EditorGUIUtility.labelWidth;
            position.width -= EditorGUIUtility.labelWidth;

            if (GUILayoutExt.DrawDropdown(position, new GUIContent(target.objectReferenceValue != null ? EditorHelpers.StringToCaption(target.objectReferenceValue.name) : attr.noneOption), FocusType.Passive, target.objectReferenceValue) == true)
            {
                var rect   = position;
                var vector = GUIUtility.GUIToScreenPoint(new Vector2(rect.x, rect.y));
                rect.x = vector.x;
                rect.y = vector.y;

                var popup = new Popup()
                {
                    title = attr.menuName, autoClose = true, screenRect = new Rect(rect.x, rect.y + rect.height, rect.width, 200f)
                };
                var objects = AssetDatabase.FindAssets("t:" + (attr.filterType != null ? attr.filterType : "Object"), attr.filterDir == null ? null : new [] { attr.filterDir });

                var allObjects = Resources.LoadAll <GameObject>("").Where(x => x.GetComponent <WindowLayout>() != null && x.GetComponent <TemplateMarker>() != null).ToArray();
                for (int i = 0; i < allObjects.Length; ++i)
                {
                    var idx = i;
                    popup.Item("Clone Template/" + allObjects[i].name, null, searchable: true, action: (item) => {
                        var p       = AssetDatabase.GetAssetPath(allObjects[idx]);
                        var newPath = AssetDatabase.GenerateUniqueAssetPath(assetPath + "/" + allObjects[idx].name + ".prefab");
                        AssetDatabase.CopyAsset(p, newPath);
                        AssetDatabase.ImportAsset(newPath, ImportAssetOptions.ForceUpdate);
                        AssetDatabase.ForceReserializeAssets(new [] { newPath }, ForceReserializeAssetsOptions.ReserializeAssetsAndMetadata);
                        var newGo = AssetDatabase.LoadAssetAtPath <GameObject>(newPath);
                        Object.DestroyImmediate(newGo.GetComponent <TemplateMarker>(), true);
                        AssetDatabase.ForceReserializeAssets(new [] { newPath }, ForceReserializeAssetsOptions.ReserializeAssetsAndMetadata);

                        so = new SerializedObject(targetObject);
                        so.Update();
                        so.FindProperty(targetPath).objectReferenceValue = newGo.GetComponent <WindowLayout>();
                        so.ApplyModifiedProperties();
                    }, order: -1);
                }

                if (string.IsNullOrEmpty(attr.noneOption) == false)
                {
                    popup.Item(attr.noneOption, null, searchable: false, action: (item) => {
                        so = new SerializedObject(targetObject);
                        so.Update();
                        so.FindProperty(targetPath).objectReferenceValue = null;
                        so.ApplyModifiedProperties();
                    }, order: -1);
                }

                for (int i = 0; i < objects.Length; ++i)
                {
                    var path  = AssetDatabase.GUIDToAssetPath(objects[i]);
                    var asset = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                    if (asset.GetComponent <WindowLayout>() == null)
                    {
                        continue;
                    }

                    popup.Item(EditorHelpers.StringToCaption(asset.name), () => {
                        so = new SerializedObject(targetObject);
                        so.Update();
                        so.FindProperty(targetPath).objectReferenceValue = asset.GetComponent <WindowLayout>();
                        so.ApplyModifiedProperties();
                    });
                }
                popup.Show();
            }
        }