2017-03-05 7 views
0

ボタンの配列が作成されています。ボタンをクリックするとボタンのテクスチャが変わります。私が持っているコードは、ボタンのどれかをクリックすると、すべてのボタンのテクスチャを変更します。配列内の押されたボタンのテクスチャの変更 - Unity 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について詳しく知ることができます。

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

コードの最初の部分は、私は必要なもののために完全である新UIで


。私を助けてくれてありがとう。 – Zoolar

+0

あなたは大歓迎です。パフォーマンス上の理由から、私の2番目の解決策も考慮してください。 – Programmer

0

Unityチーム自身が述べたように、OnGUIの使用を避け、新しいUIに頼ってください。

あなたの問題を解決するには、この操作を行う必要があります。

  • はキャンバスを作成し、スクリプトにそれにCreateButtonsを添付する。
  • キャンバス内にボタンを作成し、スクリプトChangeButtonSpriteを添付し、インスペクタのフィールドに適切なAスプラッシュとBスプライトを付けます。
  • スクリプトChangeButtonSpriteは、ChangeSprite()方法を選択し、それに渡す、ボタンのOnClickイベントを作成し、引数
  • として、ボタン自体のImageコンポーネントを選択してください、このボタンをプレハブ、その後からそれを削除します階層;キャンバスのButton Prefab分野で
  • 、それだそれ
  • プレハブボタンを入れて、あなたはこれがちょうどだったので、など、位置がどのように生成されるかを拡張ボタンの開始画像/変更することもできますユニティUIを使用する方法の例としては、(onClick.AddListenerを使用して、でも直接スクリプトOFCからのOnClickイベントをインターセプトすることができます)

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; 
     } 
    } 
} 
を埋め込まれたイベントをonclickの

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; 
    } 
} 
関連する問題