ボタンの視覚的な状態を変更するコードを書きました(クリックされたボタンは青色で強調表示され、他のボタンはすべてデフォルトの色に戻します)。それはうまくいくようですが、面倒です。私のコードを書き直すより効率的な/簡潔な方法がありますか?どうもありがとう!トグルボタンのオン/オフ機能をスクリプト化する最も効率的な方法
配列でこれをやって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()
{
}
}
トグルグループコンポーネントについてご存じですか? – Aizen
私はそれを知っています@Aizenしかし、私は以前それを使用していない。 – greyBow
Toggle Componentはコンポーネント(ボタンの場合)を1つだけ選択し、コンポーネントのLookまたは色を変更できます。そして自動的に、他のものはデフォルト状態に設定されます。自分のカスタム定義でオフコース。これは、コンポーネントについて学ぶのに最適な時間になるはずなので、多くの時間を節約できます。 Unity3Dが既に提供しているコンポーネントを再作成します。 – Aizen