2017-03-05 26 views
0

我创建按钮的数组,我想使它所以当我点击一个按钮的按钮变化纹理。当我点击任何按钮时,我已经更改了所有按钮的纹理。改变质地 - 统一C#

有没有办法将其代码,这样,如果我点击一个按钮,然后从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

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

只需将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]; 
     } 
    } 
} 

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


与新UI

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]; 
} 

这是输出:

enter image description here

+0

的第一段代码非常适合我的需要。谢谢你帮助我。 – Zoolar

+0

不客气。请考虑我的第二个解决方案,因为性能原因应该使用它。 – Programmer

0

您应该避免使用Unity团队自己声明的OnGUI,而应该依赖新的UI。

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

  • 创建一个画布,并附加给它的脚本CreateButtons;
  • 在画布内创建一个按钮,附加到脚本ChangeButtonSprite,并在检查器字段中给它适当的A和B精灵。
  • 创建的按钮onclick事件,传递给它的脚本ChangeButtonSprite,选择ChangeSprite()方法,并选择按钮本身的Image组件作为参数
  • 使得该按钮一个预制,然后从删除层次结构;
  • 在画布的Button Prefab领域,把按钮预制
  • 就是这样,你可能要改变/扩大如何生成的位置,按钮等的起始图像,因为这只是一个例如如何使用统一UI的onclick内嵌事件的(你甚至可以拦截的OnClick直接从脚本OFC事件,通过使用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; 
    } 
}