更改数组中按下按钮的纹理 - Unity c#

时间:2017-03-05 00:12:48

标签: c# arrays button unity3d textures

我创建了一系列按钮,我想这样做,当我点击一个按钮时,按钮的纹理会发生变化。当我点击任何按钮时,我的代码会改变所有按钮的纹理。

有没有办法对它进行编码,这样如果我点击一个按钮,那么按钮的纹理会从texA变为texB,其余的按钮会保留为texA?

public Texture texA;
public Texture texB;
static Vector2[] loc = new Vector2[25]; 
bool visited = false;

void Start () {
    int i = 0;
    while (i < loc.Length){
        loc [i] = new Vector2 (Random.Range (Screen.width * 0.1f, Screen.width * 0.9f), Random.Range(Screen.height * 0.1f, Screen.height * 0.9f));
        i = i + 1;
    }
}

void OnGUI(){
    for (int i = 0; i < loc.Length; i++) {
        if (GUI.Button (new Rect (loc [i].x, loc [i].y, Screen.width * 0.025f, Screen.height * 0.05f), visited ? texA:texB, "")) {
            visited = !visited;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点。您使用数据结构来存储有关单击按钮的信息。最简单的方法是使用数组。

只需将visited变量放入一个数组中,然后将visited[i]使用它的任何地方替换为public Texture texA; public Texture texB; static Vector2[] loc = new Vector2[25]; bool[] visited = new bool[25]; void Start() { int i = 0; while (i < loc.Length) { loc[i] = new Vector2(Random.Range(Screen.width * 0.1f, Screen.width * 0.9f), Random.Range(Screen.height * 0.1f, Screen.height * 0.9f)); i = i + 1; } } void OnGUI() { for (int i = 0; i < loc.Length; i++) { if (GUI.Button(new Rect(loc[i].x, loc[i].y, Screen.width * 0.025f, Screen.height * 0.05f), visited[i] ? texA : texB, "")) { visited[i] = !visited[i]; } } } 。就是这样。

public Texture texA;
public Texture texB;

const int buttonCount = 25;

public GameObject buttonPrefab;
public Canvas canvasForButtons;

Button[] buttons = new Button[buttonCount];
static Vector2[] loc = new Vector2[buttonCount];
bool[] visited = new bool[buttonCount];


void Start()
{
    int i = 0;
    while (i < loc.Length)
    {
        loc[i] = new Vector2(Random.Range(Screen.width * 0.1f, Screen.width * 0.9f), Random.Range(Screen.height * 0.1f, Screen.height * 0.9f));
        i = i + 1;
    }

    createButtons();
}

void createButtons()
{
    for (int i = 0; i < loc.Length; i++)
    {
        //Instantiate Button
        GameObject tempButtonObj = Instantiate(buttonPrefab, canvasForButtons.transform) as GameObject;
        Button tempButton = tempButtonObj.GetComponent<Button>();
        buttons[i] = tempButton;

        //Create rect for position
        Rect buttonRect = new Rect(loc[i].x, loc[i].y, Screen.width * 0.025f, Screen.height * 0.05f);

        //Assign Position of each Button
        buttons[i].GetComponent<RectTransform>().position = buttonRect.position;
        //buttons[i].GetComponent<RectTransform>().sizeDelta = buttonRect.size;

        //Don't capture local variable
        int tempIndex = i;
        //Add click Event
        buttons[i].onClick.AddListener(() => buttonClickCallBack(tempIndex));
    }
}

//Called when Button is clicked
void buttonClickCallBack(int buttonIndex)
{
    Debug.Log(buttonIndex);

    //Get Texture to change
    Texture textureToUse = visited[buttonIndex] ? texA : texB;

    //Convert that Texture to Sprite
    Sprite spriteToUse = Sprite.Create((Texture2D)textureToUse, new Rect(0.0f, 0.0f, textureToUse.width,
        textureToUse.height), new Vector2(0.5f, 0.5f), 100.0f);

    //Change the Button Image
    buttons[buttonIndex].image.sprite = spriteToUse;

    //Flip Image
    visited[buttonIndex] = !visited[buttonIndex];
}

这解决了您的问题,但您需要放弃当前的代码,并使用Unity的新UI系统和Button组件及其事件系统。您可以详细了解新的UI事件here

使用新用户界面

sendAction

这是输出:

enter image description here

答案 1 :(得分:0)

你应该避免使用团队团队自己所说的OnGUI,而是依赖新的UI。

要解决您的问题,您应该这样做:

  • 创建一个画布,并附加脚本 CreateButtons ;
  • 在“画布”中创建一个按钮,在其上附加脚本 ChangeButtonSprite ,并在检查器字段中为其提供正确的A和B精灵。
  • 在Button上创建一个OnClick事件,将脚本ChangeButtonSprite传递给它,选择ChangeSprite()方法,然后选择按钮本身的Image组件作为参数
  • 将此按钮设为预制件,然后将其从层次结构中删除;
  • 在Canvas的Button Prefab字段中,放置Button预制件
  • 就是这样,您可能想要更改/扩展位置的生成方式,按钮的起始图像等,因为这只是如何使用Unity UI OnClick嵌入式事件的一个示例(您甚至可以直接从脚本ofc拦截OnClick事件,使用onClick.AddListener

<强> CreateButtons.cs

using UnityEngine;

public class CreateButtons : MonoBehaviour {

    public GameObject ButtonPrefab;
    public int NumberOfButtons;

    void Start () {
        for (int i=0; i<NumberOfButtons; i++) {
            Vector3 buttonPos = new Vector3 (Random.Range(0f, Screen.width), Random.Range(0f, Screen.height), 0);
            GameObject buttonSpawned = Instantiate(ButtonPrefab, buttonPos, Quaternion.identity, gameObject.transform) as GameObject;
        }
    }
}

<强> ChangeButtonSprite

using UnityEngine;
using UnityEngine.UI;

public class ChangeButtonSprite : MonoBehaviour {

    public Sprite TexA,TexB;

    void Start(){
        GetComponent<Image>().sprite = TexA;
    }

    public void ChangeSprite(Image image){
        if (image.sprite == TexA) {
            image.sprite = TexB;
            return;
        }
        image.sprite = TexA;
    }
}