[英]How can i use variables in (.) statements in Unity3d
所以我正在嘗試編寫一個代碼來改變我的角色/游戲對象(一個簡單的立方體)的顏色
我這樣做是通過有一個按鈕並單擊它會將一個名為 PlayerColor 的變量設置為 White,Red,Bue 基本上我在檢查器中寫的任何內容。 我在那里寫任何我想要的東西,然后把它放到變量 PlayerColor 中
在代碼中,我嘗試根據變量所具有的內容更改游戲對象的顏色,但顯然它不允許我這樣做,這就是我在這里的原因:D。 這是我的嘗試
在 google 中搜索並沒有真正的幫助,因為我真的找不到任何答案,因為在 google 搜索中描述這個位置非常困難,結果它顯示了不相關的東西(就像我搜索“如何在 c# 中的 (.) 語句中使用變量一樣”) ”)
您需要使用Reflection 。 using System.Reflection;
在腳本的頂部,然后:
public void ChangePlayerMaterial( string playerColor )
{
PropertyInfo propertyInfo = typeof(Color).GetProperty( playerColor.ToLower() );
if( propertyInfo != null )
{
Color color = (Color) propertyInfo.GetValue(null, null);
PlayerMat.color = color ;
}
else
{
Debug.LogError("The color " + playerColor + " does not exist");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.