public static GameObject InstantiateAsSibling(InstantiationData i, int siblingTokenId)
        {
            var go      = Instantiate(i);
            var sibling = ManagedObjects.Find(siblingTokenId);

            if (sibling != null)
            {
                go.transform.parent = sibling.transform.parent;
            }
            else
            {
                throw new Exception("Sibling with Id " + siblingTokenId + " not found.");
            }
            return(go);
        }
        public static GameObject InstantiateAsChild(InstantiationData i, int parentTokenId)
        {
            var go     = Instantiate(i);
            var parent = ManagedObjects.Find(parentTokenId);

            if (parent != null)
            {
                go.transform.parent = parent.transform;
            }
            else
            {
                throw new Exception("Parent with Id " + parentTokenId + " not found.");
            }
            return(go);
        }
Example #3
0
        /// <summary>
        /// stores an object in the pool, removing it from the game world
        /// </summary>
        public void StoreObject(GameObject g)
        {
            PoolToken pt = ObjectPool.GetPoolToken(g);

            SendMessage("PreObjectDeactivate", g, SendMessageOptions.DontRequireReceiver);
            g.BroadcastMessage("OnObjectPoolDestroy", SendMessageOptions.DontRequireReceiver);
            g.BroadcastMessage("OnObjectPoolDelete", SendMessageOptions.DontRequireReceiver);
            if (transform.childCount > MaxPoolSize)
            {
                pt.usable = false;
                Destroy(g);
            }
            else
            {
                g.BroadcastMessage("OnDestroy", SendMessageOptions.DontRequireReceiver);
                g.SetActive(false);
                g.transform.parent   = transform;
                g.transform.position = Vector3.zero;
                g.transform.rotation = Quaternion.identity;
            }
            deletePlayerObject(g);
            ManagedObjects.RemoveManagedObject(g);
            SendMessage("PostObjectDeactivate", g, SendMessageOptions.DontRequireReceiver);
        }
Example #4
0
 public List <GameObject> GetMyObjectsOfType()
 {
     return(ManagedObjects.GetOwnersObjectsOfType(ownerId, poolPrefabString));
 }
Example #5
0
 public List <GameObject> GetMyObjectsOfType(string prefabPath)
 {
     return(ManagedObjects.GetOwnersObjectsOfType(ownerId, prefabPath));
 }
Example #6
0
        /// <summary>
        /// gets an object from the pool, instantiating a new gameobject if necessary
        /// </summary>
        public GameObject GetObject(InstantiationData i)
        {
            GameObject g;

            bool newObj = false;

            if (transform.childCount > 0)
            {
                int ind = 0;
                while (ind < transform.childCount)
                {
                    GameObject go = transform.GetChild(ind).gameObject;
                    PoolToken  pt = go.GetComponent <PoolToken>();
                    if (pt == null || pt.usable == true)
                    {
                        g = go;
                        break;
                    }
                    ind++;
                }
                if (ind >= transform.childCount)
                {
                    throw new Exception("No usable objects in pool. This should never happen.");
                }
                g = transform.GetChild(ind).gameObject;
            }
            else
            {
                g      = LoadInactiveObject();
                newObj = true;
            }
            i.GameObject = g;

            SendMessage("PreObjectActivate", i, SendMessageOptions.DontRequireReceiver);

            g.transform.parent   = null;
            g.transform.position = i.Position;
            g.transform.rotation = i.Rotation;

            var rigidbody = g.GetComponent <Rigidbody>();

            if (rigidbody != null)
            {
                rigidbody.velocity        = Vector3.zero;
                rigidbody.angularVelocity = Vector3.zero;
            }

            var token = PoolToken.AddPoolToken(g, PrefabString, i);

            token.firstInstantiate = newObj;
            if (ObjectPool.objectPool.AutoNameObjects)
            {
                g.name = getPoolNameFromPrefabPath(PrefabString) + "." + i.OwnerId + "." + token.tokenId;
            }

            g.SetActive(prefabDefaultActive);
            SendMessage("PostObjectActivate", i, SendMessageOptions.DontRequireReceiver);

            if (!newObj && prefabDefaultActive)
            {
                g.BroadcastMessage("Awake", SendMessageOptions.DontRequireReceiver);
                g.BroadcastMessage("Start", SendMessageOptions.DontRequireReceiver);
            }

            addPlayerObject(i.OwnerId, g);
            ManagedObjects.AddManagedObject(g);
            g.BroadcastMessage("OnObjectPoolInstantiate", SendMessageOptions.DontRequireReceiver);
            return(g);
        }