在統一中,我通過獲取 UI 影像GameObject.Find()但是當我嘗試將另一個影像組件的值更改為游戲物件的結果時,我得到了錯誤:
Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.UI.Image' \[Assembly-CSharp\]csharp(CS0029)
代碼:
Image image;
image = GameObject.Find("the other image");
有沒有辦法通過它的名稱來獲取 UI Image 游戲物件而不將其設定為通用游戲物件?
uj5u.com熱心網友回復:
您正在嘗試將影像的值分配給游戲物件,您只需要獲取影像
Image image;
var imageGameObject = GameObject.Find("the other image");
if(imageGameObject == null)
{
//couldn't find the gameobject
}
image = imageGameObject.GetComponenet<Image>();
獲取實際的影像組件
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/455793.html
