[英]How to change texture of game object in Unity?
我正在為Unity學校項目工作,我正在使用Kinect來識別面部表情。 它顯示的“默認”表達是中性的,它會隨着我將面部表情改為Happy或Suprised等而改變。
我創建了一個名為Face的游戲對象,我設置了不同的紋理(PC顯示的面部以響應我的表情),我希望他在我改變面部表情時改變。 但由於某種原因它不起作用。
我正在使用C#,首先我設置了這個:
public static Texture[] textures = new Texture[7];
public Texture neutral, smiling, happy, angry, sad, kissing, surprised;
public GameObject Face;
public Renderer rend;
在開始我有這個:
Face = GameObject.Find ("Face");
textures[0] = neutral;
textures[1] = smiling;
textures[2] = happy;
textures[3] = angry;
textures[4] = sad;
textures[5] = sad;
textures[6] = surprised;
順便說一句,它沒有找到Face所以我把它放在檢查員那里,我對紋理/面孔做了同樣的事情。 然后在更新中我把它定義如下:
ClassifyAndApply(numbers);
private void SaveAnimUnits()
{
numbers[0] = _animUnits.LipRaiser;
numbers[1] = _animUnits.JawLowerer;
numbers[2] = _animUnits.LipStretcher;
numbers[3] = _animUnits.BrowLowerer;
numbers[4] = _animUnits.LipCornerDepressor;
numbers[5] = _animUnits.OuterBrowRaiser;
}
private void ClassifyAndApply(float[] units){
// Renderer rend = GetComponent<Renderer>();
// Face = GameObject.Find ("Face");
if (units[2] <= 0.264888){
if (units[3] <= 0.817408){
if (units[1] <= 0.181886){
if (units[0] <= -0.216908){
if (units[4] <= 0.395523){
if (units[1] <= 0.104226){
Face. GetComponent<Renderer>().material.mainTexture = textures[3];
}
else{
Face. GetComponent<Renderer>().material.mainTexture = textures[0];
}
這棵樹繼續,但我認為我的問題是這個,但我不確定。
Face. GetComponent<Renderer>().material.mainTexture = textures[0];
嘗試執行Face.GetComponent<Renderer>().material.mainTexture = textures[0];
沒有巨大的if
結構,100%確定它不起作用。 您確定在檢查器中拖放了所有紋理嗎?
如果是這種情況,那么嘗試創建新材料(右鍵在項目中選項卡 - >創建 - >材質)並為其指定Standard
着色器。 然后將材質提供給Face
對象。
設置mainTexture
僅在您的對象使用具有名為“MainTexture”參數的着色器的材質時才有效(某些簡單的顏色或奇異的fx着色器可能沒有用於紋理並且沒有這樣的參數)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.