Skip to content

Instantly share code, notes, and snippets.

@eestradalucero
Created October 17, 2017 17:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save eestradalucero/0192ea4e2fee793696ba9b98afd67734 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
namespace Emilio.Utilities
{
public class PrefixAdder : ScriptableWizard {
[SerializeField] string prefix;
[SerializeField] string subfix;
[MenuItem("Tools/PreFix Subfix Adder", true)]
static bool CreateWizardValidator(){
Transform[] transforms = Selection.GetTransforms(SelectionMode.ExcludePrefab);
return transforms.Length >= 1;
}
[MenuItem("Tools/PreFix Subfix Adder", false)]
static void CreateWizard(){
ScriptableWizard.DisplayWizard("Prefix and Subfix Adder", typeof(PrefixAdder), "Create and Close" ,
"Create");
}
void OnWizardCreate(){
ApplyPrefix();
}
void ApplyPrefix()
{
GameObject[] gos = Selection.gameObjects;
foreach (GameObject go in gos)
{
var children = go.GetComponentsInChildren(typeof(Transform));
foreach (Transform child in children)
{
Undo.RegisterCompleteObjectUndo (child.gameObject,
"Added: " + (string.IsNullOrEmpty (prefix) ? "" : prefix + " ") +
(string.IsNullOrEmpty (subfix) ? "" : subfix));
// Don't apply to root object.
if (child == go.transform)
continue;
if (!string.IsNullOrEmpty (prefix)) {
child.name = prefix + child.name;
}
if (!string.IsNullOrEmpty (subfix)) {
child.name = child.name + subfix;
}
}
}
}
void OnWizardOtherButton (){
ApplyPrefix();
}
void OnWizardUpdate(){
Transform[] transforms = Selection.GetTransforms(SelectionMode.ExcludePrefab);
helpString = "Objects selected: " + transforms.Length;
errorString = "";
isValid = true;
if(transforms.Length < 1){
errorString += "No object selected to rename";
}
isValid = string.IsNullOrEmpty(errorString);
}
void OnSelectionChange(){
OnWizardUpdate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment