コード例 #1
0
        public void RenameObjects_EnumeratedObjectsWithDependentChanges_Renames()
        {
            // Arrange
            var enumeratedObject0 = this.CreatePrefabFromGameObject(
                new GameObject("EnumeratedObject0"),
                string.Concat(TestFixturesDirectory, "EnumeratedObject0.prefab"));
            var enumeratedObject1 = this.CreatePrefabFromGameObject(
                new GameObject("EnumeratedObject1"),
                string.Concat(TestFixturesDirectory, "EnumeratedObject1.prefab"));
            var enumeratedObject2 = this.CreatePrefabFromGameObject(
                new GameObject("EnumeratedObject2"),
                string.Concat(TestFixturesDirectory, "EnumeratedObject2.prefab"));

            var enumeratedObjects = new List <Object>()
            {
                enumeratedObject0,
                enumeratedObject1,
                enumeratedObject2,
            };

            var removeCharactersOp     = new RemoveCharactersOperation();
            var removeCharacterOptions = new RemoveCharactersOperation.RenameOptions();

            removeCharacterOptions.CharactersToRemove = "\\d";
            removeCharacterOptions.CharactersAreRegex = true;
            removeCharacterOptions.IsCaseSensitive    = false;
            removeCharactersOp.SetOptions(removeCharacterOptions);

            var enumerateOp = new EnumerateOperation();

            enumerateOp.StartingCount = 1;

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(removeCharactersOp);
            renameSequence.Add(enumerateOp);

            // Act
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(enumeratedObjects, true);

            // Assert
            // Build two lists to compare against because Assert displays their differences nicely in its output.
            var expectedNames = new List <string>
            {
                "EnumeratedObject1",
                "EnumeratedObject2",
                "EnumeratedObject3"
            };

            var resultingNames = new List <string>();

            foreach (var obj in enumeratedObjects)
            {
                resultingNames.Add(obj.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #2
0
        public void RenameObjects_ChangeObjectToExistingObjectNameNotInRenameGroup_SkipsRename()
        {
            // Arrange
            var conflictingObject0 = this.CreatePrefabFromGameObject(
                new GameObject("ConflictingObject0"),
                string.Concat(TestFixturesDirectory, "ConflictingObject0.prefab"));

            this.CreatePrefabFromGameObject(
                new GameObject("ExistingObject"),
                string.Concat(TestFixturesDirectory, "ExistingObject.prefab"));

            var conflictingObjectsWithoutAllNamesChanging = new List <Object>();

            conflictingObjectsWithoutAllNamesChanging.Add(conflictingObject0);

            var replaceFirstNameOp = new ReplaceStringOperation();

            replaceFirstNameOp.SearchString      = "ConflictingObject0";
            replaceFirstNameOp.ReplacementString = "ExistingObject";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceFirstNameOp);

            // Act and Assert
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(conflictingObjectsWithoutAllNamesChanging);
            var expectedName = "ConflictingObject0";

            Assert.AreEqual(expectedName, conflictingObject0.name);
        }
コード例 #3
0
ファイル: BulkRenamer.cs プロジェクト: Slaimon91/Highroller
        /// <summary>
        /// Renames the specified Objects according to a supplied RenameOperationSequence.
        /// </summary>
        /// <returns>The number of successfully renamed objects.</returns>
        /// <param name="objectsToRename">Objects to rename.</param>
        /// <param name="ignoreUndo">If set to <c>true</c> ignore undo.</param>
        public int RenameObjects(List <UnityEngine.Object> objectsToRename, bool ignoreUndo = false)
        {
            var nameChanges = new List <ObjectNameDelta>();
            var previews    = this.GetBulkRenamePreview(objectsToRename);

            for (int i = 0; i < previews.NumObjects; ++i)
            {
                // Don't request a rename if the preview has warnings
                var renamePreview = previews.GetPreviewAtIndex(i);
                if (renamePreview.HasWarnings || previews.WillRenameCollideWithExistingAsset(renamePreview))
                {
                    continue;
                }

                var renameResult = renamePreview.RenameResultSequence;
                var newName      = renameResult.NewName;
                var originalName = renameResult.OriginalName;

                // Don't request a rename if the name isn't going to change.
                if (originalName.Equals(newName))
                {
                    continue;
                }

                nameChanges.Add(new ObjectNameDelta(objectsToRename[i], newName));
            }

            return(BulkRenamer.ApplyNameDeltas(nameChanges, ignoreUndo));
        }
コード例 #4
0
        public void RenameObjects_SpritesheetsWithPrefixedNumbers_Renames()
        {
            // Tests Issue #163: https://github.com/redbluegames/unity-mulligan-renamer/issues/163
            // Arrange
            var spriteSheetConfig = new SpriteSheetGenerationConfig(2, "NumberedSprites.png");

            spriteSheetConfig.NamePrefix           = "0_NumberedSprites";
            spriteSheetConfig.UseZeroBasedIndexing = true;
            var textureWithSprites     = this.SetupSpriteSheet(spriteSheetConfig);
            var replaceStringOperation = new ReplaceStringOperation();

            replaceStringOperation.SearchString      = "Numbered";
            replaceStringOperation.ReplacementString = string.Empty;

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceStringOperation);

            var path            = AssetDatabase.GetAssetPath(textureWithSprites);
            var allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);
            var allSprites      = new List <Object>();

            foreach (var asset in allAssetsAtPath)
            {
                if (asset is Sprite)
                {
                    allSprites.Add(asset);
                }
            }

            // Act
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(allSprites, true);

            // Assert
            var expectedNames = new List <string>
            {
                "0_Sprites0",
                "0_Sprites1",
                "0_Sprites2",
                "0_Sprites3",
            };

            var resultingNames = new List <string>();

            foreach (var sprite in allSprites)
            {
                resultingNames.Add(sprite.name);
            }

            // In order to not depend on how these sprites are Loaded, we check Contains instead of comparing
            // the lists directly
            Assert.That(resultingNames.Count, Is.EqualTo(expectedNames.Count));
            foreach (var name in resultingNames)
            {
                Assert.That(expectedNames.Contains(name), "Expected names did not contain name: " + name);
            }
        }
コード例 #5
0
        public void RenameObjects_SpritesheetWithNamesThatWillOverlap_Renames()
        {
            // Tests Issue #126: https://github.com/redbluegames/unity-mulligan-renamer/issues/126
            // Arrange
            var spriteSheetConfig = new SpriteSheetGenerationConfig(2, "Texture.png");

            spriteSheetConfig.NamePrefix           = "Texture_Sprite";
            spriteSheetConfig.UseZeroBasedIndexing = true;
            var textureWithSprites = this.SetupSpriteSheet(spriteSheetConfig);
            var removeNumbersOp    = new RemoveCharactersOperation();

            removeNumbersOp.SetOptionPreset(RemoveCharactersOperation.PresetID.Numbers);
            var enumerateOp = new EnumerateOperation();

            enumerateOp.StartingCount = 1;
            enumerateOp.Increment     = 1;

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(removeNumbersOp);
            renameSequence.Add(enumerateOp);

            var path            = AssetDatabase.GetAssetPath(textureWithSprites);
            var allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);
            var allSprites      = new List <Object>();

            foreach (var asset in allAssetsAtPath)
            {
                if (asset is Sprite)
                {
                    allSprites.Add(asset);
                }
            }

            // Act
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(allSprites, true);

            // Assert
            var expectedNames = new List <string>
            {
                "Texture_Sprite1",
                "Texture_Sprite2",
                "Texture_Sprite3",
                "Texture_Sprite4",
            };

            var resultingNames = new List <string>();

            foreach (var sprite in allSprites)
            {
                resultingNames.Add(sprite.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #6
0
        public void RenameObjects_SpritesheetsWithPrefixedNumbers_Renames()
        {
            // Tests Issue #163: https://github.com/redbluegames/unity-mulligan-renamer/issues/163
            // Arrange
            var spriteSheetConfig = new SpriteSheetGenerationConfig(2, "NumberedSprites.png");

            spriteSheetConfig.NamePrefix           = "0_NumberedSprites";
            spriteSheetConfig.UseZeroBasedIndexing = true;
            var textureWithSprites     = this.SetupSpriteSheet(spriteSheetConfig);
            var replaceStringOperation = new ReplaceStringOperation();

            replaceStringOperation.SearchString      = "Numbered";
            replaceStringOperation.ReplacementString = string.Empty;

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceStringOperation);

            var path            = AssetDatabase.GetAssetPath(textureWithSprites);
            var allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);
            var allSprites      = new List <Object>();

            foreach (var asset in allAssetsAtPath)
            {
                if (asset is Sprite)
                {
                    allSprites.Add(asset);
                }
            }

            // Act
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(allSprites, true);

            // Assert
            var expectedNames = new List <string>
            {
                "0_Sprites0",
                "0_Sprites1",
                "0_Sprites2",
                "0_Sprites3",
            };

            var resultingNames = new List <string>();

            foreach (var sprite in allSprites)
            {
                resultingNames.Add(sprite.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #7
0
        public void RenameObjects_Spritesheet_Renames()
        {
            // Arrange
            var spriteSheetConfig = new SpriteSheetGenerationConfig(2, "Texture.png");

            spriteSheetConfig.NamePrefix = "Texture_Sprite";
            var textureWithSprites = this.SetupSpriteSheet(spriteSheetConfig);
            var replaceNameOp      = new ReplaceNameOperation();

            replaceNameOp.NewName = "NewSprite";

            var enumerateOp = new EnumerateOperation();

            enumerateOp.StartingCount = 1;

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceNameOp);
            renameSequence.Add(enumerateOp);

            var path            = AssetDatabase.GetAssetPath(textureWithSprites);
            var allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);
            var allSprites      = new List <Object>();

            foreach (var asset in allAssetsAtPath)
            {
                if (asset is Sprite)
                {
                    allSprites.Add(asset);
                }
            }

            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(allSprites, true);

            var expectedNames = new List <string>
            {
                "NewSprite1",
                "NewSprite2",
                "NewSprite3",
                "NewSprite4"
            };

            var resultingNames = new List <string>();

            foreach (var sprite in allSprites)
            {
                resultingNames.Add(sprite.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #8
0
        /// <summary>
        /// Reverts the name changes on the affected assets.
        /// </summary>
        public void RevertNameChange()
        {
            var reversedNames = new List <ObjectNameDelta>();

            foreach (var renamedAsset in this.renamedAssets)
            {
                if (!DoesObjectStillExist(renamedAsset.NamedObject))
                {
                    continue;
                }

                reversedNames.Add(new ObjectNameDelta(renamedAsset.NamedObject, renamedAsset.OldName));
            }

            BulkRenamer.ApplyNameDeltas(reversedNames, true);
        }
コード例 #9
0
        /// <summary>
        /// Reapplies the name changes to the affected assets.
        /// </summary>
        public void ReapplyNameChange()
        {
            var validRenames = new List <ObjectNameDelta>();

            foreach (var renamedAsset in this.renamedAssets)
            {
                if (!DoesObjectStillExist(renamedAsset.NamedObject))
                {
                    continue;
                }

                validRenames.Add(renamedAsset);
            }

            BulkRenamer.ApplyNameDeltas(validRenames, true);
        }
コード例 #10
0
        public void RenameObjects_RenameObjectToExistingObjectNameButAtDifferentPath_Succeeds()
        {
            // Arrange
            var conflictingObject0 = this.CreatePrefabFromGameObject(
                new GameObject("ConflictingObject0"),
                string.Concat(TestFixturesDirectory, "ConflictingObject0.prefab"));
            var existingObject = this.CreatePrefabFromGameObject(
                new GameObject("ExistingObject"),
                string.Concat(TestFixturesDirectory, "SubDirectory/ExistingObject.prefab"));

            var replaceFirstNameOp = new ReplaceStringOperation();

            replaceFirstNameOp.SearchString      = "ConflictingObject0";
            replaceFirstNameOp.ReplacementString = "ExistingObject";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceFirstNameOp);

            var expectedNames = new List <string>
            {
                "ExistingObject",
                "ExistingObject"
            };

            var objectsToRename = new List <UnityEngine.Object>()
            {
                conflictingObject0,
                existingObject,
            };

            // Act and Assert
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(objectsToRename);

            var resultingNames = new List <string>();

            foreach (var obj in objectsToRename)
            {
                resultingNames.Add(obj.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #11
0
        public void RenameObjects_MultipleAssets_Renames()
        {
            // Arrange
            var multipleObject0 = this.CreatePrefabFromGameObject(
                new GameObject("Asset0"),
                string.Concat(TestFixturesDirectory, "Asset0.prefab"));
            var multipleObject1 = this.CreatePrefabFromGameObject(
                new GameObject("Asset1"),
                string.Concat(TestFixturesDirectory, "Asset1.prefab"));
            var multipleAssets = new List <Object>()
            {
                multipleObject0,
                multipleObject1
            };

            var replaceStringOp = new ReplaceStringOperation();

            replaceStringOp.SearchString      = "Asset";
            replaceStringOp.ReplacementString = "Thingy";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceStringOp);

            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(multipleAssets, true);

            var expectedNames = new List <string>
            {
                "Thingy0",
                "Thingy1"
            };

            var resultingNames = new List <string>();

            foreach (var obj in multipleAssets)
            {
                resultingNames.Add(obj.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #12
0
        public void RenameObjects_SpriteAndTexture_Renames()
        {
            // Tests Issue #139: https://github.com/redbluegames/unity-mulligan-renamer/issues/139
            // Arrange
            var spriteSheetConfig = new SpriteSheetGenerationConfig(1, "Texture.png");

            spriteSheetConfig.NamePrefix = "Texture_Sprite";
            var textureWithSprites = this.SetupSpriteSheet(spriteSheetConfig);
            var replaceNameOp      = new ReplaceStringOperation();

            replaceNameOp.SearchString      = "Texture";
            replaceNameOp.ReplacementString = "Cool";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceNameOp);

            var path            = AssetDatabase.GetAssetPath(textureWithSprites);
            var allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);

            // Act
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(new List <Object>(allAssetsAtPath), true);

            // Assert
            var resultingNames = new List <string>();

            foreach (var asset in allAssetsAtPath)
            {
                resultingNames.Add(asset.name);
            }

            var expectedNames = new List <string>
            {
                "Cool",
                "Cool_Sprite1",
            };

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #13
0
        public void RenameObjects_MultipleGameObjects_Renames()
        {
            var gameObject0 = new GameObject("GameObject0");
            var gameObject1 = new GameObject("GameObject1");

            var gameObjects = new List <Object>()
            {
                gameObject0,
                gameObject1
            };

            var replaceStringOp = new ReplaceStringOperation();

            replaceStringOp.SearchString      = "Object";
            replaceStringOp.ReplacementString = "Thingy";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceStringOp);

            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(gameObjects, true);

            var expectedNames = new List <string>
            {
                "GameThingy0",
                "GameThingy1"
            };

            var resultingNames = new List <string>();

            foreach (var obj in gameObjects)
            {
                resultingNames.Add(obj.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #14
0
        public void RenameObjects_SingleAsset_Renames()
        {
            var gameObjectAsset = this.CreatePrefabFromGameObject(
                new GameObject("Original"),
                string.Concat(TestFixturesDirectory, "Original.prefab"));
            var singleAsset = new List <Object>()
            {
                gameObjectAsset
            };

            var replaceNameOp = new ReplaceNameOperation();

            replaceNameOp.NewName = "NewName";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceNameOp);

            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(singleAsset, true);

            Assert.AreEqual("NewName", singleAsset[0].name);
        }
コード例 #15
0
        public void RenameObjects_SpritesheetWithTargetNameAsSubstringInMultipleSprites_Renames()
        {
            // Tests Issue #143: https://github.com/redbluegames/unity-mulligan-renamer/issues/143
            // Arrange
            var spriteSheetConfig = new SpriteSheetGenerationConfig(4, "Texture.png");

            spriteSheetConfig.NamePrefix = "Texture_Sprite";
            var textureWithSprites = this.SetupSpriteSheet(spriteSheetConfig);
            var replaceNameOp      = new ReplaceStringOperation();

            replaceNameOp.SearchString      = "Texture_Sprite1";
            replaceNameOp.ReplacementString = "CoolSprite";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceNameOp);

            var path            = AssetDatabase.GetAssetPath(textureWithSprites);
            var allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);
            var allSprites      = new List <Object>();

            foreach (var asset in allAssetsAtPath)
            {
                if (asset is Sprite)
                {
                    allSprites.Add(asset);
                }
            }

            // Act
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(new List <Object>()
            {
                allSprites[0]
            }, true);

            // Assert
            var expectedNames = new List <string>
            {
                "CoolSprite",
                "Texture_Sprite2",
                "Texture_Sprite3",
                "Texture_Sprite4",
                "Texture_Sprite5",
                "Texture_Sprite6",
                "Texture_Sprite7",
                "Texture_Sprite8",
                "Texture_Sprite9",
                "Texture_Sprite10",
                "Texture_Sprite11",
                "Texture_Sprite12",
                "Texture_Sprite13",
                "Texture_Sprite14",
                "Texture_Sprite15",
                "Texture_Sprite16",
            };

            var resultingNames = new List <string>();

            foreach (var sprite in allSprites)
            {
                resultingNames.Add(sprite.name);
            }

            Assert.AreEqual(expectedNames, resultingNames);
        }
コード例 #16
0
        public void RenameObjects_SpritesheetWithTargetNameAsSubstringInMultipleSprites_Renames()
        {
            // Tests Issue #143: https://github.com/redbluegames/unity-mulligan-renamer/issues/143
            // Arrange
            var spriteSheetConfig = new SpriteSheetGenerationConfig(4, "Texture.png");
            var targetSpriteName  = "Texture_Sprite1";

            spriteSheetConfig.NamePrefix = "Texture_Sprite";
            var textureWithSprites = this.SetupSpriteSheet(spriteSheetConfig);
            var replaceNameOp      = new ReplaceStringOperation();

            replaceNameOp.SearchString      = targetSpriteName;
            replaceNameOp.ReplacementString = "CoolSprite";

            var renameSequence = new RenameOperationSequence <IRenameOperation>();

            renameSequence.Add(replaceNameOp);

            var    path            = AssetDatabase.GetAssetPath(textureWithSprites);
            var    allAssetsAtPath = AssetDatabase.LoadAllAssetsAtPath(path);
            var    allSprites      = new List <Object>();
            Object targetSprite    = null;

            foreach (var asset in allAssetsAtPath)
            {
                if (asset is Sprite)
                {
                    allSprites.Add(asset);
                    if (asset.name == targetSpriteName)
                    {
                        targetSprite = asset;
                    }
                }
            }

            // Act
            var bulkRenamer = new BulkRenamer(renameSequence);

            bulkRenamer.RenameObjects(new List <Object>()
            {
                targetSprite
            }, true);

            // Assert
            var expectedNames = new List <string>
            {
                "CoolSprite",
                "Texture_Sprite2",
                "Texture_Sprite3",
                "Texture_Sprite4",
                "Texture_Sprite5",
                "Texture_Sprite6",
                "Texture_Sprite7",
                "Texture_Sprite8",
                "Texture_Sprite9",
                "Texture_Sprite10",
                "Texture_Sprite11",
                "Texture_Sprite12",
                "Texture_Sprite13",
                "Texture_Sprite14",
                "Texture_Sprite15",
                "Texture_Sprite16",
            };

            var resultingNames = new List <string>();

            foreach (var sprite in allSprites)
            {
                resultingNames.Add(sprite.name);
            }

            // In order to not depend on how these sprites are Loaded, we check Contains instead of comparing
            // the lists directly
            Assert.That(resultingNames.Count, Is.EqualTo(expectedNames.Count));
            foreach (var name in resultingNames)
            {
                Assert.That(expectedNames.Contains(name), "Expected names did not contain name: " + name);
            }
        }