最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何避免在OnTriggerEnter()中調(diào)用GetComponent()?

如何避免在OnTriggerEnter()中調(diào)用GetComponent()?

C#
慕萊塢森 2023-08-13 13:50:54
簡單的問題在這里..我只是想知道有沒有辦法避免GetComponent<Script>()在里面打電話OnTriggerEnter(Collider other)?我試圖避免這樣做,因?yàn)槲抑?GetComponent 很慢。private void OnTriggerEnter(Collider other){    Tile tile = other.GetComponent<Tile>();    if (tile.colorIndex == GameManager.Instance.currentTargetColorIndex)    {        Debug.Log("Hit!");    }}
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個贊

除非此方法在單個幀中的許多對象上觸發(fā),否則可能不值得。


但是,您可以通過將 Tile 對象緩存在字典中并使用以下索引來實(shí)現(xiàn)Collider.gameObject.GetInstanceID():


在某些腳本中,運(yùn)行的腳本的每個實(shí)例都OnTriggerEnter可以訪問(例如游戲管理器):


public Dictionary<int, Tile> tileCache;


// ...


// Initializing:

tileCache = new Dictionary<int, Tile>();

使用示例:


private void OnTriggerEnter(Collider other)

{

    int tileCacheIndex = other.gameObject.GetInstanceID();

    Tile tile;


    if (GameManager._instance.tileCache.ContainsKey(tileCacheIndex)) 

    {

        tile = GameManager._instance.tileCache[tileCacheIndex];

    }

    else 

    {

        tile = other.GetComponent<Tile>();

        GameManager._instance.tileCache[tileCacheIndex] = tile;

    }


    if (tile.colorIndex == GameManager.Instance.currentTargetColorIndex)

    {

        Debug.Log("Hit!");

    }

}

因?yàn)槟褂玫氖怯螒驅(qū)ο蟮膶?shí)例 ID,所以您可以執(zhí)行一些操作,例如在每個圖塊的 Start 中預(yù)加載圖塊緩存。索引只是gameObject.GetInstanceID(),不需要GetComponent在那里調(diào)用。


查看完整回答
反對 回復(fù) 2023-08-13
  • 1 回答
  • 0 關(guān)注
  • 159 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號