- Home /
How to make a editor script that sets the values of serialized variables in the editor
Hi all,
I want to automate a process I need to do a lot of times.
I want to write a editor script, that does these things:
1- Set variables in the editor, for example I have a script that has a variable : "public Transform myTransform" . I want this script to instantiate a prefab in the editor, put it in a specific place in the hierarchy, and find one of this prefab's children and set it to this 'myTransform' variable's value.
2- Change positions of some of the subobjects of this prefabs.
3- Save one of the gameobject in the hierarchy as prefab in the project folder.
4- If you also can direct me to some learning material that could help me create a new windows, so I can drag my prefab on to it's variable value. And then click a button. and that way it makes these actions.
I don't expect you to step by step teach me these, if you can just give a few tips and tell me where to look to learn to do theese things, would be great. Thanks in advance.
Answer by ttRevan · Nov 05, 2015 at 11:11 AM
4- you can create editor window by inheriting EditorWindow class and creating menu item to open this window.
3- Use PrefabUtility.CreatePrefab:
var prefab = new GameObject(prefabName);
var rigidbody = prefab.AddComponent<Rigidbody>();
PrefabUtility.CreatePrefab("Assets/"+ prefab.name + ".prefab", prefab);
UnityEngine.Object.DestroyImmediate(prefab);
2- AssetDatabase is your friend:
var guids = AssetDatabase.FindAssets("SomeQuery t:prefab", new[] { "Assets/Prefab" });
for (int i = 0; i < guids.Length; i++) {
var path = AssetDatabase.GUIDToAssetPath(guids[i]);
var prefab = AssetDatabase.LoadAssetAtPath<Transform>(path);
//modify prefab
AssetDatabase.SaveAssets();
}
1- Not sure what type of script you're talking about, byt in any editor-aware code you can use, again, AssetDatabase to load prefabs and instantiate them as usual via Instantiate method.