Exemple #1
0
    /// <summary>
    /// Use thid method to instantiate a gameobject using a Gameobject reference
    /// </summary>
    /// <param name="obj">  PREFAB </param>
    /// <param name="pos">  Position    </param>
    /// <param name="rot">  Rotation    </param>
    /// <returns></returns>
    private GameObject InstantiatePool(GameObject obj, Vector3 pos, Quaternion rot)
    {
        if (!m_poolObjects.ContainsKey(obj.name))
        {
            CreatePool(obj);
        }
        foreach (GameObject poolObj in m_poolObjects[obj.name])
        {
            if (poolObj == null)
            {
                continue;
            }
            if (poolObj.activeSelf == false)
            {
                poolObj.transform.position = pos;
                poolObj.transform.rotation = rot;
                IPooled pooledObj = poolObj.GetComponent <IPooled>();
                if (pooledObj != null)
                {
                    pooledObj.OnInstantiate();
                }
                poolObj.SetActive(true);
                return(poolObj);
            }
        }


        GameObject currentObj = Instantiate(obj) as GameObject;

        currentObj.transform.position = pos;
        currentObj.transform.rotation = rot;
        currentObj.name = obj.name;
        currentObj.SetActive(true);
        IPooled newpooledObj = currentObj.GetComponent <IPooled>();

        if (newpooledObj != null)
        {
            newpooledObj.OnInstantiate();
        }
        m_poolObjects[obj.name].Add(currentObj);
        return(currentObj);
    }