2017-03-05 28 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; 
    } 
}