/** * @brief Instantiates a new prefab in a deterministic way. * * @param prefab GameObject's prefab to instantiate. * @param position Position to place the new GameObject. * @param rotation Rotation to set in the new GameObject. **/ public static GameObject SyncedInstantiate(GameObject prefab, TSVector position, TSQuaternion rotation) { GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject; InitializeGameObject(go, position, rotation); return(go); }
private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation) { ICollider[] tsColliders = go.GetComponentsInChildren <ICollider>(); if (tsColliders != null) { for (int index = 0, length = tsColliders.Length; index < length; index++) { PhysicsManager.instance.AddBody(tsColliders[index]); } } FPTransform rootFPTransform = go.GetComponent <FPTransform>(); if (rootFPTransform != null) { rootFPTransform.Initialize(); rootFPTransform.position = position; rootFPTransform.rotation = rotation; } FPTransform[] FPTransforms = go.GetComponentsInChildren <FPTransform>(); if (FPTransforms != null) { for (int index = 0, length = FPTransforms.Length; index < length; index++) { FPTransform FPTransform = FPTransforms[index]; if (FPTransform != rootFPTransform) { FPTransform.Initialize(); } } } FPTransform2D rootFPTransform2D = go.GetComponent <FPTransform2D>(); if (rootFPTransform2D != null) { rootFPTransform2D.Initialize(); rootFPTransform2D.position = new TSVector2(position.x, position.y); rootFPTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z; } FPTransform2D[] FPTransforms2D = go.GetComponentsInChildren <FPTransform2D>(); if (FPTransforms2D != null) { for (int index = 0, length = FPTransforms2D.Length; index < length; index++) { FPTransform2D FPTransform2D = FPTransforms2D[index]; if (FPTransform2D != rootFPTransform2D) { FPTransform2D.Initialize(); } } } }