- Home /
Renaming Child Assets in Project
I'm looking to see if it's possible, and if so then how to rename an asset in your project that exists as a child of another object.
I am currently using "AssetDatabase.AddObjectToAsset" API to add an object as a child of another object. But after that occurs I want to have the ability to rename that child object. Can I do this? How would I do this?
As a note, I do not want to destroy the object and then recreate it with a new name, as I want to use these as references on other objects.
Answer by Ash-Blue · Aug 10, 2017 at 05:05 AM
You can easily change the name of a nested asset with a simple custom editor script.
using UnityEditor;
using UnityEngine;
namespace MyNamespace {
[CustomEditor(typeof(MyScriptableObject))]
public class MyScriptableObjectEditor : Editor {
public override void OnInspectorGUI () {
target.name = EditorGUILayout.TextField("Name", target.name);
base.OnInspectorGUI();
}
}
}
I did this and works ok. But the change is not reflected immediately in the project hierarchy, I need to collapse the parent asset and expand it again to make the change visible. Any way to do it immediate? thanks.
Yeah Unity's project asset window isn't refreshing nested objects unless they recompile. Calling refresh doesn't even work. Definitely seems like a bug as the serialization seems fine.
Answer by N1warhead · May 03, 2017 at 02:51 AM
// 0 I believe would be the parent, just increment up 1 each time.
transform.GetChild (0).name = "SomeNewName";
Answer by toddisarockstar · May 03, 2017 at 02:44 AM
GameObject gme;
void Start() {
//get a unique parent or child;
gme = gameObject.Find ("some parent object name");
// lookup a child and rename like this:
gme.transform.Find ("original name of child").name = "some new name";
//find a child of a child like this:
gme.transform.Find ("name of child/child of a child").name = "some new name";
}
Answer by Nobody1 · Jul 10, 2017 at 02:07 PM
@Nerull22 If you're using a custom Editor script, you can use the Editor script's "target" reference to rename a Child Asset while it's inside its Parent Asset.
From within the ItemEditor script, just use:
target.name = newName;
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath((Item)target));
Where "Item" is the class of the Child Asset, and "newName" is the name you want to assign to the Child Asset.
The 2nd line reimports the asset, and doesn't seem to be strictly necessary, but if you don't use it, you won't see the name change in your "Project" view until you save Unity manually.
Note: The "target" reference is just a reference to the Object that the Editor script is targeting. I imagine that if you changed the Child Asset's .name value via another reference it would also work, but I haven't tested this.
Great idea, but if performance is an issue constantly hitting ImportAsset makes Unity extremely slow. Not great if an input prompt is being actively monitored.
Your answer
Follow this Question
Related Questions
A Constantly Updating Child Check 1 Answer
Renaming a child within code. 1 Answer
How do I register undo for the moving/renaming of a folder along with the assets contained? 0 Answers
Make child's transform independent of parent 1 Answer
Get child component of a prefab to reference with another prefab 1 Answer