緩存池的測試

丟人個人來補上次緩存池中沒寫的測試代碼了c#

緩存池的測試

緩存池的測試須要一個激活物體的代碼和一個移走物體的代碼,具體以下:
激活物體:緩存

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是有限的,這樣就達到了緩存池的目的,減小內存的消耗。this

相關文章
相關標籤/搜索