缓存池的测试

丢人我的来补上次缓存池中没写的测试代码了

缓存池的测试

缓存池的测试需要一个激活物体的代码和一个移走物体的代码,具体如下:
激活物体:

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if ( Input.GetMouseButtonDown(0) )
        {
            PoolManager.GetInstance().GetObj("目标路径/Obj1");
        }
        if ( Input.GetMouseButtonDown (1))
        {
            PoolManager.GetInstance().GetObj("目标路径/Obj2");
        }
    }
}

点一下鼠标左键,会出现Obj1;点一下鼠标右键,会出现Obj2。可以挂在场景的任何物体上,测试生成的结果。
移走物体:

public class DelayPush : MonoBehaviour
{
    // Start is called before the first frame update
    void OnEnable()
    {
        Invoke("Push", 1);
    }

    // Update is called once per frame
    void Update()
    {
        PoolManager.GetInstance().PushObj(this.gameObject.name, this.gameObject);
    }
}

与Test一样随便挂在场景中,可以使物体延迟1秒后失活,从场景中消失。
观察unity中的hierarchy,发现无论你手速有多快,创建的GameObject是有限的,这样就达到了缓存池的目的,减少内存的消耗。

posted @ 2020-05-09 14:55  Jay_Auditore  阅读(138)  评论(0)    收藏  举报