I know we can assign game objects which are already in the scene into another game object via script using Unity editor API, but how do I assign a prefab to a public var in a game object using Unity editor API?
for instance, I want to make a batch script to attach the required components to many different 3d objects at once, and then because some components require references to prefabs which are not yet instantiated in the scene (because they are in the project hierarchy), so how can we do that?
Prefabs are referenced to as GameObjects. So simply declare a public variable of GameObject type and assign your prefab to that variable via inspector.
Create a variable like var myGameObjects : GameObject[ ], select a size in the editor, and drag the prefab onto the variable in the inspector. You aren’t restricted to just using instantiated objects when storing variables in scripts.
Two basic options–one is to keep the drag-and-drop style of the Inspector by using a custom EditorWindow or ScriptableWizard: Unity - Scripting API: ScriptableWizard
If you want to do it with hard-coded paths, you can get a reference to a prefab GameObject with AssetDatabase.* functions, like: Unity - Scripting API: AssetDatabase.LoadAssetAtPath
Oops, pardon me, I thought the first option only allow objects that are already in the scene, because when I first tried that, the drop down menu only shows scene objects. I forgot that I can actually drag and drop the prefab my self from the project panel.
But probably just in case that Im too lazy to drag and drop every time I need to execute that script, I’ll go for option 2.
Thanks a lot for the helps.