我创建了一系列按钮,我想这样做,当我点击一个按钮时,按钮的纹理会发生变化。当我点击任何按钮时,我的代码会改变所有按钮的纹理。
有没有办法对它进行编码,这样如果我点击一个按钮,那么按钮的纹理会从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;
}
}
}
答案 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
这是输出:
答案 1 :(得分:0)
你应该避免使用团队团队自己所说的OnGUI,而是依赖新的UI。
要解决您的问题,您应该这样做:
ChangeSprite()
方法,然后选择按钮本身的Image
组件作为参数Button Prefab
字段中,放置Button预制件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;
}
}