第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

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

1 回答

?
哈士奇WWW

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

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


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


在某些腳本中,運行的腳本的每個實例都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!");

    }

}

因為您使用的是游戲對象的實例 ID,所以您可以執(zhí)行一些操作,例如在每個圖塊的 Start 中預加載圖塊緩存。索引只是gameObject.GetInstanceID(),不需要GetComponent在那里調用。


查看完整回答
反對 回復 2023-08-13
  • 1 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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