Ejemplo n.º 1
0
        /// <summary>
        /// Renames the objects supplied as a list of object and new name pairings.
        /// </summary>
        /// <param name="objectsAndNewNames">Objects with their new names.</param>
        /// <param name="ignoreUndo">If set to <c>true</c> ignore undo.</param>
        public void RenameObjects(List <ObjectNameDelta> objectsAndNewNames, bool ignoreUndo = false)
        {
            // Record all the objects to undo stack, note as of Unity 5.5.2 this does not record asset names,
            // so we have our own Undoer to handle assets.
            // Furthermore, our Spritesheet renaming isn't captured by the undo system so it must be manually undone.
            if (!ignoreUndo)
            {
                var gameObjectsToRename = new List <GameObject>();
                foreach (var objectAndName in objectsAndNewNames)
                {
                    if (!objectAndName.NamedObject.IsAsset())
                    {
                        gameObjectsToRename.Add((GameObject)objectAndName.NamedObject);
                    }
                }

                Undo.RecordObjects(gameObjectsToRename.ToArray(), "Bulk Rename");

                AssetRenameUndoer.RecordAssetRenames("Bulk Rename", objectsAndNewNames);
            }

            int totalNumSteps       = objectsAndNewNames.Count;
            int currentStep         = 0;
            var spritesheetRenamers = new List <SpritesheetRenamer>();

            for (int i = 0; i < objectsAndNewNames.Count; ++i, ++currentStep)
            {
                var infoString = string.Format("Renaming Object {0} of {1}", i, objectsAndNewNames.Count);
                EditorUtility.DisplayProgressBar("Renaming...", infoString, currentStep / (float)objectsAndNewNames.Count);

                var objectToRename = objectsAndNewNames[i].NamedObject;
                var newName        = objectsAndNewNames[i].NewName;
                if (objectToRename.IsAsset())
                {
                    if (objectToRename is Sprite)
                    {
                        this.MarkSpriteForRename((Sprite)objectToRename, newName, ref spritesheetRenamers);
                    }
                    else
                    {
                        this.RenameAsset(objectToRename, newName);
                    }
                }
                else
                {
                    this.RenameGameObject((GameObject)objectToRename, newName);
                }
            }

            for (int i = 0; i < spritesheetRenamers.Count; ++i, ++currentStep)
            {
                var infoString = string.Format("Renaming Spritesheet {0} of {1}", i, spritesheetRenamers.Count);
                EditorUtility.DisplayProgressBar("Renaming...", infoString, currentStep / (float)totalNumSteps);

                spritesheetRenamers[i].RenameSprites();
            }

            EditorUtility.ClearProgressBar();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Renames the objects supplied as a list of object and new name pairings.
        /// </summary>
        /// <returns>The number of successfully renamed objects.</returns>
        /// <param name="objectsAndNewNames">Objects with their new names.</param>
        /// <param name="ignoreUndo">If set to <c>true</c> ignore undo.</param>
        public static int ApplyNameDeltas(List <ObjectNameDelta> objectsAndNewNames, bool ignoreUndo = false)
        {
            List <ObjectNameDelta> assetsToRename;
            List <ObjectNameDelta> spritesToRename;
            List <ObjectNameDelta> gameObjectsToRename;

            SplitObjectsIntoCategories(objectsAndNewNames, out gameObjectsToRename, out assetsToRename, out spritesToRename);

            // Record all the objects to undo stack, note as of Unity 5.5.2 this does not record asset names,
            // so we have our own Undoer to handle assets.
            // Furthermore, our Spritesheet renaming isn't captured by the undo system so it must be manually undone.
            if (!ignoreUndo)
            {
                var gameObjectsToRenameAsGameObjects = new List <GameObject>();
                foreach (var gameObjectToRename in gameObjectsToRename)
                {
                    gameObjectsToRenameAsGameObjects.Add((GameObject)gameObjectToRename.NamedObject);
                }

                Undo.RecordObjects(gameObjectsToRenameAsGameObjects.ToArray(), LocalizationManager.Instance.GetTranslation("bulkRename"));

                AssetRenameUndoer.RecordAssetRenames(LocalizationManager.Instance.GetTranslation("bulkRename"), objectsAndNewNames);
            }

            // Rename the objects and show a progress bar
            int numFailedRenames = 0;
            int totalNumSteps    = spritesToRename.Count + assetsToRename.Count + gameObjectsToRename.Count;
            int progressBarStep  = 0;
            var deferredRenames  = new List <ObjectNameDelta>();

            foreach (var assetToRename in assetsToRename)
            {
                UpdateProgressBar(progressBarStep++, totalNumSteps);
                var newName = string.Empty;
                if (RenamedAssetWillShareNameWithAnotherAsset(assetToRename, objectsAndNewNames))
                {
                    // Decrement progress bar count because we'll increment it later when we do the deferred objects.
                    --progressBarStep;
                    deferredRenames.Add(assetToRename);
                    newName = assetToRename.NamedObject.GetInstanceID().ToString();
                }
                else
                {
                    newName = assetToRename.NewName;
                }

                RenameAsset(assetToRename.NamedObject, newName);
            }

            foreach (var gameObjectToRename in gameObjectsToRename)
            {
                UpdateProgressBar(progressBarStep++, totalNumSteps);
                RenameGameObject((GameObject)gameObjectToRename.NamedObject, gameObjectToRename.NewName);
            }

            foreach (var deferredRename in deferredRenames)
            {
                UpdateProgressBar(progressBarStep++, totalNumSteps);
                try
                {
                    RenameAsset(deferredRename.NamedObject, deferredRename.NewName);
                }
                catch (System.OperationCanceledException)
                {
                    numFailedRenames++;
                }
            }

            // Package all sprites into renamers so we can do the file IO
            // all at once.
            var spritesheetRenamers = new Dictionary <string, SpritesheetRenamer>();

            foreach (var spriteToRename in spritesToRename)
            {
                UpdateProgressBar(progressBarStep++, totalNumSteps);
                MarkSpriteForRename((Sprite)spriteToRename.NamedObject, spriteToRename.NewName, ref spritesheetRenamers);
            }

            // Rename the sprites in the spritesheets.
            foreach (var kvp in spritesheetRenamers)
            {
                UpdateProgressBar(progressBarStep++, totalNumSteps);
                kvp.Value.RenameSprites();
            }

            EditorUtility.ClearProgressBar();

            return(totalNumSteps - numFailedRenames);
        }