2016-08-20 6 views
0

ボタンの視覚的な状態を変更するコードを書きました(クリックされたボタンは青色で強調表示され、他のボタンはすべてデフォルトの色に戻します)。それはうまくいくようですが、面倒です。私のコードを書き直すより効率的な/簡潔な方法がありますか?どうもありがとう!トグルボタンのオン/オフ機能をスクリプト化する最も効率的な方法

配列でこれをやって
using UnityEngine ; 
using System.Collections ; 
using UnityEngine.UI ; 

public class ToolButtons : MonoBehaviour 
{ 
    public Color activeColor ; 
    public Color inactiveColor ; 
    public GameObject iconBG ; 
    public Button ink, brush, crayon, pencil, spray, eraser, chnageColor, brushSize, undo, redo, clear, newAnimal ; 
    public GameObject inkIconBG, brushIconBG, crayonIconBG, pencilIconBG, sprayIconBG, eraserIconBG, changeColorIconBG, brushSizeIconBG ; 

    void Start() 
    { 
     inactiveColor = iconBG.GetComponent <Image>().color ; 
    } 

    // Use this for initialization 
    void buttonCallBack (Button buttonClicked) 
    { 
     //Change Color Palette Button clicked 
     if (buttonClicked == ink) 
     { 
      inkIconBG.GetComponent <Image>().color = activeColor ; 
     } else if (buttonClicked != ink) 
     { 
      inkIconBG.GetComponent <Image>().color = inactiveColor ; 
     } 

     if (buttonClicked == brush) 
     { 
      brushIconBG.GetComponent <Image>().color = activeColor ; 
     } else if (buttonClicked != brush) 
     { 
      brushIconBG.GetComponent <Image>().color = inactiveColor ; 
     } 

     if (buttonClicked == crayon) 
     { 
      crayonIconBG.GetComponent <Image>().color = activeColor ; 
     } else if (buttonClicked != crayon) 
     { 
      crayonIconBG.GetComponent <Image>().color = inactiveColor ; 
     } 

     if (buttonClicked == pencil) 
     { 
      pencilIconBG.GetComponent <Image>().color = activeColor ; 
     } else if (buttonClicked != pencil) 
     { 
      pencilIconBG.GetComponent <Image>().color = inactiveColor ; 
     } 

     if (buttonClicked == spray) 
     { 
      sprayIconBG.GetComponent <Image>().color = activeColor ; 
     } else if (buttonClicked != spray) 
     { 
      sprayIconBG.GetComponent <Image>().color = inactiveColor ; 
     } 

     if (buttonClicked == eraser) 
     { 
      eraserIconBG.GetComponent <Image>().color = activeColor ; 
     } else if (buttonClicked != eraser) 
     { 
      eraserIconBG.GetComponent <Image>().color = inactiveColor ; 
     } 
    } 

    void OnEnable() 
    { 
     ink.onClick.AddListener (() => buttonCallBack (ink)) ; 
     brush.onClick.AddListener (() => buttonCallBack (brush)) ; 
     crayon.onClick.AddListener (() => buttonCallBack (crayon)) ; 
     pencil.onClick.AddListener (() => buttonCallBack (pencil)) ; 
     spray.onClick.AddListener (() => buttonCallBack (spray)) ; 
     eraser.onClick.AddListener (() => buttonCallBack (eraser)) ; 
    } 


    void OnDisable() 
    { 

    } 
} 
+1

トグルグループコンポーネントについてご存じですか? – Aizen

+0

私はそれを知っています@Aizenしかし、私は以前それを使用していない。 – greyBow

+0

Toggle Componentはコンポーネント(ボタンの場合)を1つだけ選択し、コンポーネントのLookまたは色を変更できます。そして自動的に、他のものはデフォルト状態に設定されます。自分のカスタム定義でオフコース。これは、コンポーネントについて学ぶのに最適な時間になるはずなので、多くの時間を節約できます。 Unity3Dが既に提供しているコンポーネントを再作成します。 – Aizen

答えて

1

は良くなっているだろうが、問題は、それが難しい後にコードを変更すること、あなたのボタンとゲームオブジェクトの名前を失うだろうということです。

これにはDictionaryを使用できます。 Button - GameObjectのペアを作成してから、それぞれのアイコン/ゲームオブジェクトに一致するように手動でそれぞれButtonを追加します。 Buttonをクリックするとループし、DictionaryのキーでクリックされたButtonと比較し、比較結果に基づいてactiveColorまたはinactiveColorを割り当てます。

注::ボタンとアイコンをさらに追加する場合は、pairButtonIcon()機能にも追加する必要があります。

私がforeachループをDictionary以上に使用しなかったのは誰でも、Unityにメモリを割り当てるためです。

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using UnityEngine.UI; 

public class ToolButtons : MonoBehaviour 
{ 
    public Color activeColor; 
    public Color inactiveColor; 
    public GameObject iconBG; 
    public Button ink, brush, crayon, pencil, spray, eraser, chnageColor, brushSize, undo, redo, clear, newAnimal; 
    public GameObject inkIconBG, brushIconBG, crayonIconBG, pencilIconBG, sprayIconBG, eraserIconBG, changeColorIconBG, brushSizeIconBG; 

    Dictionary<Button, GameObject> buttonIconPair = new Dictionary<Button, GameObject>(); 

    void pairButtonIcon() 
    { 
     buttonIconPair.Add(ink, inkIconBG); 
     buttonIconPair.Add(brush, brushIconBG); 
     buttonIconPair.Add(crayon, crayonIconBG); 
     buttonIconPair.Add(pencil, pencilIconBG); 
     buttonIconPair.Add(spray, sprayIconBG); 
     buttonIconPair.Add(eraser, eraserIconBG); 
     buttonIconPair.Add(chnageColor, changeColorIconBG); 
     buttonIconPair.Add(brushSize, brushSizeIconBG); 
    } 

    void Start() 
    { 
     pairButtonIcon(); 
     inactiveColor = iconBG.GetComponent<Image>().color; 
    } 

    // Use this for initialization 
    void buttonCallBack(Button buttonClicked) 
    { 

     //My Code 
     for (int i = 0; i < buttonIconPair.Count; i++) 
     { 
      var item = buttonIconPair.ElementAt(i); 
      var itemKey = item.Key; 
      var itemValue = item.Value; 

      if (buttonClicked == itemKey) 
      { 
       itemValue.GetComponent<Image>().color = activeColor; 
      } 
      else 
      { 
       itemValue.GetComponent<Image>().color = inactiveColor; 
      } 
     } 
    } 

    void OnEnable() 
    { 
     ink.onClick.AddListener(() => buttonCallBack(ink)); 
     brush.onClick.AddListener(() => buttonCallBack(brush)); 
     crayon.onClick.AddListener(() => buttonCallBack(crayon)); 
     pencil.onClick.AddListener(() => buttonCallBack(pencil)); 
     spray.onClick.AddListener(() => buttonCallBack(spray)); 
     eraser.onClick.AddListener(() => buttonCallBack(eraser)); 
    } 


    void OnDisable() 
    { 

    } 
} 

EDIT:あなたはまた、各1上Buttonとゲームオブジェクト/アイコンを格納し、その後、複数のListsを使用することができます。

public class ToolButtons : MonoBehaviour 
{ 
    public Color activeColor; 
    public Color inactiveColor; 
    public GameObject iconBG; 
    public Button ink, brush, crayon, pencil, spray, eraser, chnageColor, brushSize, undo, redo, clear, newAnimal; 
    public GameObject inkIconBG, brushIconBG, crayonIconBG, pencilIconBG, sprayIconBG, eraserIconBG, changeColorIconBG, brushSizeIconBG; 

    List<Button> button = new List<Button>(); 
    List<GameObject> iconGameObjects = new List<GameObject>(); 

    void pairButtonIcon() 
    { 
     button.Add(ink); 
     iconGameObjects.Add(inkIconBG); 

     button.Add(brush); 
     iconGameObjects.Add(brushIconBG); 

     button.Add(crayon); 
     iconGameObjects.Add(crayonIconBG); 

     button.Add(pencil); 
     iconGameObjects.Add(pencilIconBG); 

     button.Add(spray); 
     iconGameObjects.Add(sprayIconBG); 

     button.Add(eraser); 
     iconGameObjects.Add(eraserIconBG); 

     button.Add(chnageColor); 
     iconGameObjects.Add(changeColorIconBG); 

     button.Add(brushSize); 
     iconGameObjects.Add(brushSizeIconBG); 
    } 

    void Start() 
    { 
     pairButtonIcon(); 
     inactiveColor = iconBG.GetComponent<Image>().color; 
    } 

    // Use this for initialization 
    void buttonCallBack(Button buttonClicked) 
    { 

     //My Code 
     for (int i = 0; i < button.Count; i++) 
     { 
      if (buttonClicked == button[i]) 
      { 
       iconGameObjects[i].GetComponent<Image>().color = activeColor; 
      } 
      else 
      { 
       iconGameObjects[i].GetComponent<Image>().color = inactiveColor; 
      } 
     } 
    } 

    void OnEnable() 
    { 
     ink.onClick.AddListener(() => buttonCallBack(ink)); 
     brush.onClick.AddListener(() => buttonCallBack(brush)); 
     crayon.onClick.AddListener(() => buttonCallBack(crayon)); 
     pencil.onClick.AddListener(() => buttonCallBack(pencil)); 
     spray.onClick.AddListener(() => buttonCallBack(spray)); 
     eraser.onClick.AddListener(() => buttonCallBack(eraser)); 
    } 


    void OnDisable() 
    { 

    } 
} 
+0

これは完璧です、ありがとうございます! – greyBow

+0

あなたは歓迎です。どの方法で行ったのですか? – Programmer

+0

辞書は完全に機能しました。 – greyBow

関連する問題