Example #1
0
        static void SetupObjectReference(SerializedProperty property, GenericMenu menu, Event evt)
        {
            var hasPasteObject = Clipboard.hasObject;
            var hasPasteGuid   = Clipboard.hasGuid;
            var hasPastePath   = !new GUID(AssetDatabase.AssetPathToGUID(Clipboard.stringValue)).Empty();
            var obj            = property.objectReferenceValue;

            var canCopy  = !property.hasMultipleDifferentValues && obj != null;
            var canPaste = GUI.enabled && (hasPasteGuid || hasPastePath || hasPasteObject);

            if (menu != null)
            {
                AddSeparator(menu);
                var hasAsset = canCopy && AssetDatabase.Contains(obj);
                if (canCopy)
                {
                    menu.AddItem(kCopyContent, false, o => Clipboard.objectValue = (UnityEngine.Object)o, obj);
                }
                else
                {
                    menu.AddDisabledItem(kCopyContent);
                }
                if (hasAsset)
                {
                    menu.AddItem(kCopyPathContent, false, o => Clipboard.stringValue = AssetDatabase.GetAssetPath((UnityEngine.Object)o).ToString(), obj);
                    menu.AddItem(kCopyGuidContent, false, o => Clipboard.guidValue   = new GUID(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath((UnityEngine.Object)o))), obj);
                }
                else
                {
                    menu.AddDisabledItem(kCopyPathContent);
                    menu.AddDisabledItem(kCopyGuidContent);
                }

                if (canPaste)
                {
                    menu.AddItem(kPasteContent, false, o => PasteObjectReference((SerializedProperty)o), property);
                }
                else
                {
                    menu.AddDisabledItem(kPasteContent);
                }
            }

            if (evt != null)
            {
                if (canCopy && evt.commandName == EventCommandNames.Copy)
                {
                    if (evt.type == EventType.ValidateCommand)
                    {
                        evt.Use();
                    }
                    if (evt.type == EventType.ExecuteCommand)
                    {
                        Clipboard.objectValue = obj;
                        evt.Use();
                    }
                }
                if (canPaste && evt.commandName == EventCommandNames.Paste)
                {
                    if (evt.type == EventType.ValidateCommand)
                    {
                        evt.Use();
                    }
                    if (evt.type == EventType.ExecuteCommand)
                    {
                        PasteObjectReference(property);
                        evt.Use();
                    }
                }
            }
        }
Example #2
0
 public static bool Contains(UnityEngine.Object obj)
 {
     return(AssetDatabase.Contains(obj.GetInstanceID()));
 }
Example #3
0
        private static bool OnSearchForReferencesValidate()
        {
            Object activeObject = Selection.activeObject;

            return(((activeObject != null) && AssetDatabase.Contains(activeObject)) && !Directory.Exists(AssetDatabase.GetAssetPath(activeObject)));
        }
        internal static void ShowAssetsPopupMenu <T>(Rect buttonRect, string typeName, SerializedProperty serializedProperty, string fileExtension, string defaultFieldName) where T : Object, new()
        {
            GenericMenu gm = new GenericMenu();

            int selectedInstanceID = serializedProperty.objectReferenceValue != null?serializedProperty.objectReferenceValue.GetInstanceID() : 0;


            bool foundDefaultAsset = false;
            var  type    = UnityEditor.UnityType.FindTypeByName(typeName);
            int  classID = type != null ? type.persistentTypeID : 0;

            BuiltinResource[] resourceList = null;

            // Check the assets for one that matches the default name.
            if (classID > 0)
            {
                resourceList = EditorGUIUtility.GetBuiltinResourceList(classID);
                foreach (var resource in resourceList)
                {
                    if (resource.m_Name == defaultFieldName)
                    {
                        gm.AddItem(new GUIContent(resource.m_Name), resource.m_InstanceID == selectedInstanceID, AssetPopupMenuCallback, new object[] { resource.m_InstanceID, serializedProperty });
                        resourceList      = resourceList.Where(x => x != resource).ToArray();
                        foundDefaultAsset = true;
                        break;
                    }
                }
            }

            // If no defalut asset was found, add defualt null value.
            if (!foundDefaultAsset)
            {
                gm.AddItem(new GUIContent(defaultFieldName), selectedInstanceID == 0, AssetPopupMenuCallback, new object[] { 0, serializedProperty });
            }

            // Add items from asset database
            foreach (var property in AssetDatabase.FindAllAssets(new SearchFilter()
            {
                classNames = new[] { typeName }
            }))
            {
                gm.AddItem(new GUIContent(property.name), property.instanceID == selectedInstanceID, AssetPopupMenuCallback, new object[] { property.instanceID, serializedProperty });
            }

            // Add builtin items, except for the already added default item.
            if (classID > 0 && resourceList != null)
            {
                foreach (var resource in resourceList)
                {
                    gm.AddItem(new GUIContent(resource.m_Name), resource.m_InstanceID == selectedInstanceID, AssetPopupMenuCallback, new object[] { resource.m_InstanceID, serializedProperty });
                }
            }

            var  target   = serializedProperty.serializedObject.targetObject;
            bool isPreset = target is Component ? ((int)(target as Component).gameObject.hideFlags == 93) : !AssetDatabase.Contains(target);

            // the preset object is destroyed with the inspector, and nothing new can be created that needs this link. Fix for case 1208437
            if (!isPreset)
            {
                // Create item
                gm.AddSeparator("");
                gm.AddItem(EditorGUIUtility.TrTextContent("Create New..."), false, delegate
                {
                    var newAsset = Activator.CreateInstance <T>();
                    var doCreate = ScriptableObject.CreateInstance <DoCreateNewAsset>();
                    doCreate.SetProperty(serializedProperty);
                    ProjectWindowUtil.StartNameEditingIfProjectWindowExists(newAsset.GetInstanceID(), doCreate, "New " + typeName + "." + fileExtension, AssetPreview.GetMiniThumbnail(newAsset), null);
                });
            }

            gm.DropDown(buttonRect);
        }
        static bool CreateEditableCopy_Validate(MenuCommand command)
        {
            var importer = (TrueTypeFontImporter)command.context;

            return(AssetDatabase.Contains(importer));
        }