I found that using prefabs from code was a big of a jaunt. If you wanted to get a prefab you had to load a PrefabFile then call SceneUtility.GetPrefabScene. It was all very unnecessary, really.
So now to load a prefab you can do this
GameObject prefab = GameObject.GetPrefab( "weapons/glock/glock.prefab" );
Previously these prefab objects weren't loaded. They were empty. They were just references. Now they are loaded, they have all the child GameObjects and Components.
This means you can do stuff like this to get information about your prefab without instantiating it into the Scene.
GameObject prefab = GameObject.GetPrefab( "weapons/glock/glock.prefab" );
var weapon = prefab.GetComponent<BaseWeapon>();
And when you want to actually spawn it into the scene, that's the same as ever.
GameObject prefab = GameObject.GetPrefab( "weapons/glock/glock.prefab" );
var glock = prefab.Clone();
As an added bonus here, these prefabs can be referenced over the network. So in this example, you can send prefab as an RPC argument and when it's called on other computers, it'll be the same prefab.
Comments