Graphics Reference
In-Depth Information
return (GameObject)playerGameObjects[indexNum];
}
As with GetPlayerGO(), the GetPlayerTransform() function allows us to quickly
access the transform of a particular player indexed via an integer.
public Transform GetPlayerTransform (int indexNum)
{
return (Transform)playerTransforms[indexNum];
}
For incidental spawning, such as particle effects, the SpawnController.cs script provides
a Spawn() method.
At this stage, you need to understand what prefabs are, and in turn, how they can be
dynamically added to the scene. To quote the Unity documentation:
[Prefabs are] … a collection of predefined GameObjects & Components that are re-usable
throughout your game.
They are saved into the project, available in the same way regular assets are, and may
be dragged into a scene or added dynamically through code with the Instantiate keyword.
Instantiate makes a clone of an object you pass in as a parameter and returns a reference
to the clone.
Instantiate takes three parameters:
object
An existing object (such as a prefab or gameObject) to make a copy of
position
A Vector3 position for the new object
rotation
A Quaternion rotation for the new object
One important consideration to keep in mind is that there is a CPU hit for object instan-
tiation as memory is freed and allocated for it. This is much more noticeable on mobile plat-
forms, particularly those lower performing systems such as older (3-4-year-old) devices. In
situations where performance is an issue, it is advisable to use an object pooling system. Test
early, test often, and keep an eye out for performance hits like this.
By passing in a prefab (gameObject), a 3D position vector, and a Quaternion rotation
value, the Spawn function will instantiate (make an instance of) an object in the correct
place with the correct rotation and return the transform of the newly instantiated object:
public Transform Spawn(GameObject anObject, Vector3 aPosition,
Quaternion aRotation)
{
if(objectList==null)
objectList=new ArrayList();
// instantiate the object
tempGO=(GameObject)Instantiate(anObject, aPosition,
aRotation);
tempTrans= tempGO.transform;
Search WWH ::




Custom Search