- Home /
Add prefabs to hierarchy without game running?
Hi! I have a prefab I want to add 100 times and have them in the hierarchy without running the game, as a 10x10 grid. I can do this from a script but it runs only when the game is running, so..
How do I instantiate a prefab to my scene hierarchy without the game running?
(I can drag them from the projects folder I know, but doing that 100 times is not fun or precise)
YES, I have actually searched for this for 30 min and nothing found...
Thanks!
Answer by Cherno · Apr 21, 2015 at 07:51 PM
Here's a simple Editor Extension that lets you call any function on any given script. You don't have to attach this script to any gameobject, just leave it somewhere in your Assets folder. The specified script "YourScriptName" will have a button that, when clicked, calls the function "FunctionNameInYourScript". To call the function 100 times, just create a loop like you normally would. You could also forgo calling a function on YourScript, and directly instantiate from the extension code, or call a function that is defined in the editor extension script, or... Well, effectively, you can do anything in the Editor script that you want.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[CustomEditor(typeof(YourScriptName))]
public class YourScriptNameExtension : Editor {
#if UNITY_EDITOR
override public void OnInspectorGUI () {
YourScriptName yourScriptName = (YourScriptName)target;
if(GUILayout.Button("Name of the button")) {
yourScriptName.FunctionNameInYourScript();
//If you want to directly instantiate a prefab,
//just replace the above line with your instantiating code,
//and add "EditorUtility.SetDirty(instantiatedGameObject);"
//after instantiating just to be sure, where "instantiatedGameObject"
//is the variable of your newly instantiated gameObject (NOT the prefab!)
}
if (GUI.changed) {
EditorUtility.SetDirty(target);
}
DrawDefaultInspector();
}
#endif
}
This is exactly what I was looking for! Thanks ALOT man have a great day :D