Beispiel #1
0
        static int[] PasteFoldersAfterCopy(string destination = null)
        {
            List <string> copiedPaths = new List <string>();
            bool          failed      = false;

            foreach (var item in assetClipboard)
            {
                string assetPath = AssetDatabase.GetAssetPath(ObjectIdentifier.ToObject(item));
                var    newPath   = AssetDatabase.GenerateUniqueAssetPath(GetValidPath(assetPath, destination));

                if (newPath.Length != 0)
                {
                    failed |= !AssetDatabase.CopyAsset(assetPath, newPath);
                }
                else
                {
                    failed = true;
                }

                if (!failed)
                {
                    copiedPaths.Add(newPath);
                }
            }

            AssetDatabase.Refresh();

            int[] copiedAssets = new int[copiedPaths.Count];
            for (int i = 0; i < copiedPaths.Count; i++)
            {
                copiedAssets[i] = AssetDatabase.LoadMainAssetAtPath(copiedPaths[i]).GetInstanceID();
            }

            return(copiedAssets);
        }
Beispiel #2
0
        static int[] PasteFoldersAfterCut(string destination = null)
        {
            List <string> pastedObjects = new List <string>();

            string[] assetPaths = new string[assetClipboard.Count];

            int counter = 0;

            foreach (var item in assetClipboard)
            {
                assetPaths[counter] = AssetDatabase.GetAssetPath(ObjectIdentifier.ToObject(item));
                counter++;
            }

            if (counter > 0)
            {
                Undo.RegisterAssetsMoveUndo(assetPaths);
            }

            counter = 0;
            foreach (var item in assetClipboard)
            {
                var assetPath = assetPaths[counter];

                var newPath = AssetDatabase.GenerateUniqueAssetPath(GetValidPath(assetPath, destination));
                if (String.IsNullOrEmpty(AssetDatabase.MoveAsset(assetPath, newPath)))
                {
                    pastedObjects.Add(newPath);
                    counter++;
                }
            }

            Reset();
            performedAction = PerformedAction.None;
            AssetDatabase.Refresh();

            int[] copiedAssets = new int[pastedObjects.Count];

            for (int i = 0; i < pastedObjects.Count; i++)
            {
                copiedAssets[i] = AssetDatabase.LoadMainAssetAtPath(pastedObjects[i]).GetInstanceID();
            }

            return(copiedAssets);
        }
Beispiel #3
0
        public Object ToObject()
        {
            var id = new ObjectIdentifier
            {
                guid = new GUID(guid),
                localIdentifierInFile = localId,
                fileType = type
            };

            if (!id.guid.Empty())
            {
                var o = ObjectIdentifier.ToObject(id);
                if (o != null)
                {
                    return(o);
                }
            }
            return(EditorUtility.InstanceIDToObject(instanceID));
        }
Beispiel #4
0
        static IEnumerable <Object> PasteCutAssets(string destination = null)
        {
            List <string> pastedObjects = new List <string>();

            string[] assetPaths = new string[assetClipboard.Count];

            int counter = 0;

            foreach (var item in assetClipboard)
            {
                assetPaths[counter] = AssetDatabase.GetAssetPath(ObjectIdentifier.ToObject(item));
                counter++;
            }

            if (counter > 0)
            {
                Undo.RegisterAssetsMoveUndo(assetPaths);
            }

            counter = 0;
            foreach (var item in assetClipboard)
            {
                var assetPath = assetPaths[counter];
                var validPath = GetValidPath(assetPath, destination);
                var obj       = AssetDatabase.LoadAssetAtPath(validPath, typeof(Object));
                var newPath   = assetPath != validPath && obj != null?AssetDatabase.GenerateUniqueAssetPath(validPath) : validPath;

                if (String.IsNullOrEmpty(AssetDatabase.ValidateMoveAsset(assetPath, newPath)))
                {
                    AssetDatabase.MoveAsset(assetPath, newPath);
                    pastedObjects.Add(newPath);
                }

                counter++;
            }

            Reset();
            AssetDatabase.Refresh();

            return(pastedObjects.Select(AssetDatabase.LoadMainAssetAtPath));
        }
Beispiel #5
0
        static IEnumerable <Object> PasteCopiedAssets(string destination = null)
        {
            Object        firstDuplicatedObjectToFail = null;
            List <string> pastedObjects = new List <string>();

            foreach (var item in assetClipboard)
            {
                var asset     = ObjectIdentifier.ToObject(item);
                var assetPath = AssetDatabase.GetAssetPath(asset);

                // if duplicating a sub-asset, then create a copy next to the main asset file
                if (asset != null && !String.IsNullOrEmpty(assetPath) && AssetDatabase.IsSubAsset(asset))
                {
                    if (asset is ISubAssetNotDuplicatable || asset is GameObject)
                    {
                        firstDuplicatedObjectToFail = firstDuplicatedObjectToFail ? firstDuplicatedObjectToFail : asset;
                        continue;
                    }

                    var extension = NativeFormatImporterUtility.GetExtensionForAsset(asset);

                    // We dot sanitize or block unclean the asset filename (asset.name)
                    // since the assertdb will do it for us and has a whole tailored logic for that.

                    // It feels wrong that the asset name (that can apparently contain any char)
                    // is conflated with the orthogonal notion of filename. From the user's POV
                    // it will force an asset dup but with mangled names if the original name contained
                    // "invalid chars" for filenames.
                    // Path.Combine is not used here to avoid blocking asset names that might
                    // contain chars not allowed in filenames.
                    if ((new HashSet <char>(Path.GetInvalidFileNameChars())).Intersect(asset.name).Count() != 0)
                    {
                        Debug.LogWarning(string.Format("Duplicated asset name '{0}' contains invalid characters. Those will be replaced in the duplicated asset name.", asset.name));
                    }

                    var newPath = AssetDatabase.GenerateUniqueAssetPath(
                        string.Format("{0}{1}{2}.{3}",
                                      Path.GetDirectoryName(assetPath),
                                      Path.DirectorySeparatorChar,
                                      asset.name,
                                      extension)
                        );

                    assetPath = GetValidPath(newPath, destination);
                    AssetDatabase.CreateAsset(Object.Instantiate(asset), assetPath);
                    pastedObjects.Add(assetPath);
                }
                // Otherwise duplicate the main asset file
                else if (EditorUtility.IsPersistent(asset))
                {
                    var newPath = AssetDatabase.GenerateUniqueAssetPath(GetValidPath(assetPath, destination));
                    if (newPath.Length > 0 && AssetDatabase.CopyAsset(assetPath, newPath))
                    {
                        pastedObjects.Add(newPath);
                    }
                }
            }

            AssetDatabase.Refresh();

            if (firstDuplicatedObjectToFail != null)
            {
                var errString = string.Format(
                    "Duplication error: One or more sub assets (with types of {0}) can not be duplicated directly, use the appropriate editor instead",
                    firstDuplicatedObjectToFail.GetType().Name
                    );

                Debug.LogError(errString, firstDuplicatedObjectToFail);
            }

            return(pastedObjects.Select(AssetDatabase.LoadMainAssetAtPath));
        }