Exemplo n.º 1
0
    public static void DoDestroy(GameObject go, bool checkIngore = true)
    {
        if (!Application.isPlaying)
        {
            go.SetActive(false);
            return;
        }

        GameObjectPoolObject poolObject = go.GetComponent <GameObjectPoolObject>();

        if (poolObject == null)
        {
            //GameObject.Destroy(this.gameObject);
            go.SetActive(false);
            return;
        }

        if (poolObject.IsInPool)
        {
            go.SetActive(false);
            return;
        }

        //GameObjectPoolObject提供了无视这次销毁操作的接口,用于某些特殊的特效做延迟销毁
        if (checkIngore && poolObject.m_onIngoreDestroy != null && poolObject.m_onIngoreDestroy())
        {
            return;
        }


        go.SetActive(false);
    }
Exemplo n.º 2
0
    public void Put(GameObject go)
    {
        if (this == null)
        {
            return;//可能已经被销毁
        }
        if (go == null)
        {
            Debuger.LogError("往对象池里放了空对象");
            return;
        }

        LinkedList <GameObjectPoolObject> l = m_pools.Get(go.name);

        if (l == null)
        {
            Debuger.LogError("放入的时候对象池找不到列表:{0}", go.name);
            return;
        }

        GameObjectPoolObject poolObj = go.GetComponent <GameObjectPoolObject>();

        if (poolObj == null)
        {
            Debuger.LogError("找不到GameObjectPoolObject。不是对象池中的对象:{0}", name);
            return;
        }
        if (poolObj.IsInPool)
        {
            Debuger.LogError("已经对象池里,重复放入了:{0}", name);
            return;
        }

        Put(l, poolObj);
    }
Exemplo n.º 3
0
    public IPoolObject <GameObject> Get()
    {
        IPoolObject <GameObject> poolObject = null;

        if (objectsInPools.Count == 0)
        {
            GameObject content = GameObject.Instantiate(prefab);
            poolObject = new GameObjectPoolObject(this, content);

            poolObject.Content.SetActive(true);

            objectsPoped.Add(poolObject);
        }
        else
        {
            int lastIndex = objectsInPools.Count - 1;
            poolObject = objectsInPools[lastIndex];

            poolObject.Content.SetActive(true);

            objectsInPools.RemoveAt(lastIndex);
            objectsPoped.Add(poolObject);
        }
        poolObject.Content.transform.SetParent(null);
        return(poolObject);
    }
Exemplo n.º 4
0
    GameObjectPoolObject Get(LinkedList <GameObjectPoolObject> l, bool active = true)
    {
        //有可能是加载不到的资源
        if (l.Count == 0)
        {
            return(null);
        }

        //要保证取出后预制体不为空,如果只剩一个那么加载多一个
        if (l.Count == 1)
        {
            PreLoad(l.Last.Value.gameObject, false);
        }

        GameObjectPoolObject poolObj = l.First.Value;
        GameObject           go      = poolObj.gameObject;

        l.RemoveFirst();
        if (active)
        {
            go.SetActive(true);//有时候要挂到别的父节点下,这种情况下应该挂到父节点下之后才显示
        }
        poolObj.OnGet();
#if UNITY_EDITOR
        if (!string.IsNullOrEmpty(m_debugName) && go.name == m_debugName)
        {
            Debuger.Log("get了对象池一个:{0}", m_debugName);
        }
#endif



        return(poolObj);
    }
Exemplo n.º 5
0
    public void Play()
    {
        if (m_isPlaying)
        {
            Debuger.Log("还没结束就重新播放了");
            Stop();//先结束老的
        }

        //先找到对应的脚本
        m_fx = GetComponent <PostEffectsBase>();
        if (m_fx == null)
        {
            Debuger.Log("没有找到后期处理特效");
            FxDestroy.DoDestroy(this.gameObject);
            return;
        }
        GetComponent <Camera>().enabled = false;//隐藏相机,如果没有隐藏的话
        Type t = m_fx.GetType();

        m_method = t.GetMethod("OnRenderImage", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        if (m_method == null)
        {
            Debuger.Log("没有找到OnRenderImage方法:{0}", t.Name);
            FxDestroy.DoDestroy(this.gameObject);
            return;
        }

        //添加到相机特效管理器
        Camera c = null;

#if ART_DEBUG
        c = Camera.main;
#else
        c = CameraMgr.instance == null?null:CameraMgr.instance.CurCamera;
#endif
        if (c == null)
        {
            Debuger.LogError("找不到相机");
            FxDestroy.DoDestroy(this.gameObject);
            return;
        }
        CameraFxMgr camFxMgr = c.AddComponentIfNoExist <CameraFxMgr>();
        m_handle = camFxMgr.Add(t.Name, this);

        m_isPlaying   = true;
        m_beginTime   = Util.time;
        m_destroyTime = -1;

        GameObjectPoolObject po = this.AddComponentIfNoExist <GameObjectPoolObject>();
        if (po != null)//为了实现延迟销毁机制,要用到对象池
        {
            po.m_onIngoreDestroy = IngorePoolDestroy;
        }


        foreach (CameraAni a in m_anis)
        {
            a.OnBegin(m_fx);
        }
    }
Exemplo n.º 6
0
 public void WarmUp()
 {
     if (TotalCount < MaxSize)
     {
         GameObject content = GameObject.Instantiate(prefab);
         content.transform.SetParent(poolRoot.transform);
         content.SetActive(false);
         IPoolObject <GameObject> poolObject = new GameObjectPoolObject(this, content);
         objectsInPools.Add(poolObject);
     }
 }
Exemplo n.º 7
0
 //清空到剩下一个
 public void Clear()
 {
     m_counter = 0;
     foreach (var l in m_pools.Values)
     {
         while (l.Count > 1)
         {
             GameObjectPoolObject go = l.Last.Value;
             l.RemoveLast();
             GameObject.Destroy(go.gameObject);
         }
         ++m_counter;
     }
 }
Exemplo n.º 8
0
    //复制游戏对象到对象池,注意对象池是靠对象名字识别的,如果预制体名和对象名不同可能会出现异常
    //addIfNo = true,如果有了就不要预加载了
    public void PreLoad(GameObject go, bool addIfNo = true)
    {
        if (go == null)
        {
            Debuger.LogError("预制体为空?不能放入对象池");
            return;
        }
        //删除后缀
        string name = go.name;
        int    idx  = name.IndexOf(" (");

        if (idx != -1)
        {
            name = name.Substring(0, idx - 1);
        }
        idx = name.IndexOf(" (");
        if (idx != -1)
        {
            name = name.Substring(0, idx - 1);
        }

        //已经有了就不用预加载了
        LinkedList <GameObjectPoolObject> l = m_pools.GetNewIfNo(name);

        if (l.Count > 0 && addIfNo)
        {
            return;
        }

        //增加到对象池
        s_isPreloading = true;
        GameObject goNew = GameObject.Instantiate(go);

        s_isPreloading = false;
        goNew.name     = name;
        GameObjectPoolObject poolObj = goNew.AddComponentIfNoExist <GameObjectPoolObject>();

        poolObj.OnInit();
        Put(l, poolObj);
        ++m_counter;//每Instantiate一个计数下,用于检查泄露

        //预加载对应的音效
#if !ART_DEBUG
        if (m_poolType == enPool.Fx)
        {
            //FxSoundCfg.PreLoad(name);
        }
#endif
    }
Exemplo n.º 9
0
    public static void DoDestroy(GameObject go, bool checkIngore = true)
    {
        if (!Application.isPlaying)
        {
            go.SetActive(false);
            return;
        }

        GameObjectPoolObject poolObject = go.GetComponent <GameObjectPoolObject>();

        if (poolObject == null)
        {
            //GameObject.Destroy(this.gameObject);
            go.SetActive(false);
            return;
        }

        if (poolObject.IsInPool)
        {
            go.SetActive(false);
            return;
        }

        //GameObjectPoolObject提供了无视这次销毁操作的接口,用于某些特殊的特效做延迟销毁
        if (checkIngore && poolObject.m_onIngoreDestroy != null && poolObject.m_onIngoreDestroy())
        {
            return;
        }

#if !ART_DEBUG
        if (Main.instance != null)
        {
            GameObjectPool.GetPool(GameObjectPool.enPool.Fx).Put(go);
        }
        else
        {
            go.SetActive(false);
        }
#else
        go.SetActive(false);
#endif
    }
Exemplo n.º 10
0
    void Put(LinkedList <GameObjectPoolObject> l, GameObjectPoolObject poolObject)
    {
        bool isPuting = s_isPuting;

        if (!s_isPuting)
        {
            s_isPuting = true;
            Transform t   = poolObject.transform;
            Vector3   pos = t.position;//位置要设置回去
            t.SetParent(this.transform, false);
            t.position   = pos;
            t.localScale = Vector3.one;
        }
        else
        {
            m_putGameObjects.Add(poolObject);
        }

        try
        {
            poolObject.OnPut();//要在SetActive(false)之前,因为有些脚本OnDisable的时候也会把自己放回对象池,这个时候会造成逻辑错误
            //正在隐藏一个对象的时候可能会由OnDisable()导致绑在身上的特效的对象池回收,
            //这时候如果修改父节点为对象池会报错,先放到对象池的列表里,等下一帧再把父节点设置为对象池
            poolObject.gameObject.SetActive(false);
        }
        finally
        {
#if ART_DEBUG
            l.AddFirst(poolObject);
#else
            l.AddLast(poolObject);
#endif
            if (!isPuting && s_isPuting)
            {
                s_isPuting = false;
            }
        }
    }