Skip to content
Matheus Lessa Rodrigues edited this page Oct 28, 2017 · 1 revision

Create

A collection of prefab instantiation utility functions.

All functions are used to instantiate prefabs and take an optional Transform parameter to tell the instantiated object's parent.

GameObject Prefab( GameObject prefab, Transform parent = null )

Instantiates a prefab given a GameObject.

using UnityEngine;
using BitStrap;

public sealed class MyScript : MonoBehaviour
{
	public GameObject prefab;

	private GameObject instance;

	private Start()
	{
		instance = Create.Prefab( prefab, transform.parent ); // Instantiate a sibling GameObject
	}
}

T Behaviour<T>( Transform parent = null )

Instantiates a MonoBehaviour given its type. This is equal to create a new GameObject and then attaching a component of type T.

using UnityEngine;
using BitStrap;

public sealed class MyScript : MonoBehaviour
{
	private SomeMonoBehaviourScript instance;

	private Start()
	{
		instance = Create.Behaviour<SomeMonoBehaviourScript>(); // Create a new GameObject with a SomeMonoBehaviourScript component in it
	}
}

T Behaviour<T>( T behaviourPrefab, Transform parent = null )

Like the combination of the previous two, it will instantiate a prefab but the reference to that prefab is made by a script it has of type T.

using UnityEngine;
using BitStrap;

public sealed class MyScript : MonoBehaviour
{
	public SomeMonoBehaviourScript prefab;

	private SomeMonoBehaviourScript instance;

	private Start()
	{
		instance = Create.Behaviour( prefab ); // Instantiate a GameObject from a SomeMonoBehaviourScript prefab
	}
}
Clone this wiki locally