Beispiel #1
0
    /// <summary>
    ///     Create a object prefab from pool immediately, the object retain from this pool should not be INSTANTIATED. just use
    ///     it.
    ///     NOTICE: you should always call Release to give the object back to pool, Destroy should NEVER called by yourself.
    /// </summary>
    /// <param name="res">the path of prefab you want to instinate</param>
    /// <param name="callbackIfReuse">
    ///     when a object is reused from the pool, this function will be called, callback will call
    ///     just after this one.
    /// </param>
    /// <param name="callbackIfNew">
    ///     when a new object is INSTANTIATED from resource, this function will be called, callback
    ///     will call just after this one.
    /// </param>
    public static GameObject NewObjectSync(string res,
                                           Action <GameObject> callbackIfReuse = null,
                                           Action <GameObject> callbackIfNew   = null)
    {
#if UNITY_EDITOR
        if (Quit)
        {
            return(null);
        }
#endif

        if (!Active)
        {
            Active = true;
            Cleaner.GetComponent <Helper>().StartCoroutine(CheckRelease());
        }

        ComplexObjectPoolImpl pool;
        if (!mDictionary.TryGetValue(res, out pool))
        {
            pool = new ComplexObjectPoolImpl(res);
            mDictionary.Add(res, pool);
        }

        var go = pool.NewObjectSync(callbackIfReuse, callbackIfNew);
        mObjectIndex[go.GetInstanceID()] = pool;
        return(go);
    }
Beispiel #2
0
    /// <summary>
    ///     Create a object prefab from pool, the object retain from this pool should not be INSTANTIATED. just use it.
    ///     NOTICE: you should always call Release to give the object back to pool, Destroy should NEVER called by yourself.
    /// </summary>
    /// <param name="res">the path of prefab you want to instinate</param>
    /// <param name="callback">
    ///     when the object is created for you, this function will be called, NOTICE, this function may
    ///     called several frame later.
    /// </param>
    /// <param name="callbackIfReuse">
    ///     when a object is reused from the pool, this function will be called, callback will call
    ///     just after this one.
    /// </param>
    /// <param name="callbackIfNew">
    ///     when a new object is INSTANTIATED from resource, this function will be called, callback
    ///     will call just after this one.
    /// </param>
    public static void NewObject(string res,
                                 Action <GameObject> callback,
                                 Action <GameObject> callbackIfReuse = null,
                                 Action <GameObject> callbackIfNew   = null,
                                 bool sync          = false,
                                 bool firstPriority = false,
                                 bool active        = true,
                                 string changedKey  = null)
    {
#if UNITY_EDITOR
        if (Quit)
        {
            return;
        }
#endif
        if (!Active)
        {
            Active = true;
            Holder.GetComponent <Helper>().StartCoroutine(CheckRelease());
        }

        ComplexObjectPoolImpl pool;
        string key;
        if (changedKey == null)
        {
            key = res;
        }
        else
        {
            key = res + changedKey;
        }

        if (!mDictionary.TryGetValue(key, out pool))
        {
            pool = new ComplexObjectPoolImpl(res);
            mDictionary.Add(key, pool);
        }

        pool.NewObject(obj =>
        {
            if (obj)
            {
                mObjectIndex[obj.GetInstanceID()] = pool;
            }
            try
            {
                if (callback != null)
                {
                    callback(obj);
                }
            }
            catch
            {
                // ignored
            }
        }, callbackIfReuse, callbackIfNew, sync, firstPriority, active);
    }
Beispiel #3
0
 public static void SetActive(GameObject go, bool active)
 {
     ComplexObjectPoolImpl.SetTransformActiveRecursively(go.transform, active);
 }