2017-05-01 23 views
1

我有一张沙发的3D模型,其纹理要通过屏幕上显示的纹理更改按钮进行更改。如何通过Unity中的按钮更改纹理

例如。如果我触摸红色按钮,沙发颜色将变成红色;如果我触摸黑色按钮,沙发颜色将变黑。

目前,我可以通过触摸屏幕上的任何位置来更改沙发的纹理,但现在我想要触摸特定的按钮并根据按钮改变沙发纹理。

这里是我是我到目前为止有:

public class change_texture : MonoBehaviour { 

    public Texture[] texture; 
    public int currentTexture; 


    // Use this for initialization 
    void Start() { 
     Screen.orientation = ScreenOrientation.Landscape; 
    } 

    // Update is called once per frame 
    void Update() { 
     if (Input.touches[0].phase == TouchPhase.Began) { 
      currentTexture++; 
      currentTexture %= texture.Length; 
      GetComponent<Renderer>().material.mainTexture = texture [currentTexture]; 
     } 
    } 
} 

回答

2

您应该使用GUI按钮onclick事件。首先将您的脚本(连接到沙发)更改为这一个:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class textureChange : MonoBehaviour { 

    public Texture[] texture; //set in inspector 
    public int[] price; // price for every texture (in parallel to the textures array) 
    public int currentTexture; //set default/starting index in inspector 
    bool shouldChange = false; //this bool is set to prevent unwanted texture changes to the same texture every frame 
    public Text priceObject; // the Price text object 
    // Use this for initialization 
    void Start() 
    { 
     Screen.orientation = ScreenOrientation.Landscape; //your code 
     priceObject.text = "Price: " + price[currentTexture] + "$"; //show default price 
    } 
    public void changetexture(int textureToSet) 
    { 
     currentTexture = textureToSet; 
     shouldChange = true; 
    } 
    public void incrementtexture() 
    { 
     currentTexture++; 
     shouldChange = true; 
    } 
    // Update is called once per frame 
    void Update() 
    { 
     if (shouldChange) 
     { 
      shouldChange = false; 
      GetComponent<Renderer>().material.mainTexture = texture[currentTexture]; //set new texture 
      priceObject.text = "Price: " + price[currentTexture] + "$"; 
     } 
    } 
} 

然后将画布作为父项添加到场景中。 在按钮检查员,通过添加一个函数来的OnClick列表:

  1. 按下“+”按钮。

  2. 将沙发游戏对象拖放到检查器上。

  3. 选择textureChange脚本中的changetexture函数。

  4. 设置新的纹理索引(0是第一个纹理,1是第二个,等等)。

enter image description here

+0

我已经发布我的查询中后下,请审查并更新我! – Kushan2

+0

你为什么在Update中运行?也无法防止出界? – Everts

0

HI WizzardMaker和itay_421我想你的解决方案,但遗憾的是它不工作。

这里我附上屏幕我做了什么检查,让我知道我错在哪里。

enter image description here

enter image description here

请让我尽快知道。

在此先感谢。

+0

当您按下按钮时会发生什么?我不明白解决方案是否有效。也尝试调试代码。 –

+0

如果我在索引脚本中传递静态值,它将正常工作。现在,如果我有多个纹理超过5,并希望在同一个按钮上逐个更改,请点击我该怎么做? – Kushan2

+1

我在我的答案中增加了incrementtexture()方法。将OnClick函数更改为这个。 –

0

这将只使用一个按钮。

public class textureChange : MonoBehaviour { 

    public Texture[] textures; 
    public int index; 

    public void Changetexture() 
    { 
     // Will loop through array and restart from 0 
     int index = (++index == this.textures.Length) ? 0 : index; 
     GetComponent<Renderer>().material.mainTexture = this.textures[index]; 
    } 
} 

如果您有增加/减少按钮,请为每个按钮分配以下内容。到达阵列的结尾/开始时它将停止变化。

public class TextureChange : MonoBehaviour { 

    public Texture[] textures; 
    private int index; 

    public void IncreaseIndex() 
    { 
     // Will loop through array and restart from 0 
     if(++index == this.textures.Length){index = this.textures.Length - 1;} 
     GetComponent<Renderer>().material.mainTexture = this.textures[index]; 
    } 
    public void DecreaseIndex() 
    { 
     // Will loop through array and restart from 0 
     if(--index < 0){ index = 0} 
     GetComponent<Renderer>().material.mainTexture = this.textures[index]; 
    } 
} 

你也可以把它在一个方法:

public void SetIndex(int polarity) 
    { 
     // Will loop through array and restart from 0 
     index += polarity; 
     if(index < 0){ index = 0 } 
     else if(index >= this.textures.Length){ index = this.textures.Length -1; } 
     GetComponent<Renderer>().material.mainTexture = this.textures[index]; 
    } 
相关问题