UIToggle是NGUI中用来实现按钮的复选,一组按钮的单选功能的。
但是在实际开发当中,UIToggle有它的局限性:例如我点击一个按钮,需要有多种高亮显示、按钮上的字体颜色变换、点击按钮不高亮显示,只是弹一段提示等问题。这种时候,就发现还是自己实现一套 “多选一”的框架比较靠谱,自己来控制逻辑。
接下来,先详细看一下UIToggle这个脚本。
static public BetterList<UIToggle> list = new BetterList<UIToggle>(); //Ngui将场景中所有enable为true的UIToggle都放到这个静态list当中,所以使用的时候要注意group的ID,不要影响到其他功能Toggle按钮
public int group = 0; //如果是0就代表可以做复选,不为0可以做单选
publ void Set (bool state) //UIToggle的关键代码
{
if (validator != null && !validator(state)) return;
if (!mStarted) //在编辑器模式下
{
mIsActive = state;
startsActive = state;
if (activeSprite != null) activeSprite.alpha = state ? 1f : 0f;
}
else if (mIsActive != state)
{
// Uncheck all other toggles
if (group != 0 && state) //是复选,并且设置为state为激活的时候
{
for (int i = 0, imax = list.size; i < imax; )
{
UIToggle cb = list[i];
if (cb != this && cb.group == group) cb.Set(false);
if (list.size != imax) //这段代码,猜测是为了防止在遍历的时候,有UIToggle的enable为false了,还进行设值
{
imax = list.size;
i = 0;
}
else ++i;
}
}
// Remember the state
mIsActive = state;
// Tween the color of the active sprite
if (activeSprite != null) //设置图片的高亮显示
{
if (instantTween || !NGUITools.GetActive(this))
{
activeSprite.alpha = mIsActive ? 1f : 0f;
}
else
{
TweenAlpha.Begin(activeSprite.gameObject, 0.15f, mIsActive ? 1f : 0f);
}
}
if (current == null)
{
UIToggle tog = current;
current = this;
if (EventDelegate.IsValid(onChange))
{
EventDelegate.Execute(onChange);
}
else if (eventReceiver != null && !string.IsNullOrEmpty(functionName))
{
// Legacy functionality support (for backwards compatibility)
eventReceiver.SendMessage(functionName, mIsActive, SendMessageOptions.DontRequireReceiver);
}
current = tog;
}
// Play the checkmark animation
if (animator != null)
{
ActiveAnimation aa = ActiveAnimation.Play(animator, null,
state ? Direction.Forward : Direction.Reverse,
EnableCondition.IgnoreDisabledState,
DisableCondition.DoNotDisable);
if (aa != null && (instantTween || !NGUITools.GetActive(this))) aa.Finish();
}
else if (activeAnimation != null)
{
ActiveAnimation aa = ActiveAnimation.Play(activeAnimation, null,
state ? Direction.Forward : Direction.Reverse,
EnableCondition.IgnoreDisabledState,
DisableCondition.DoNotDisable);
if (aa != null && (instantTween || !NGUITools.GetActive(this))) aa.Finish();
}
}
}
以上是UIToggle的代码,接着自己实现一套简单复选的代码框架,用来解决上面所说的问题
private GameObject ClickGo; //点击按钮
private GameObject mCurChooseGo; //当前选择的按钮
private string s16ChooseColor = ""; //选中的颜色
private string s16UnChooseColor = ""; //未选中的颜色
private Color tabShow;
private Color tabHide;
private void Start()
{
ColorUtility.TryParseHtmlString(s16ChooseColor, out tabShow);
ColorUtility.TryParseHtmlString(s16UnChooseColor, out tabHide);
UIEventListener.Get(ClickGo).onClick = OnClickTab;
}
private void OnClickTab(GameObject go)
{
SetTabIsVaild(go);
}
private void SetTabIsVaild(GameObject goTab, bool isShow = true)
{
if(mCurChooseGo != null)
{
Utility.Get<UILabel>(mCurChooseGo.transform, "Lab").color = tabHide; //设置颜色
Utility.FindChildren(mCurChooseGo.transform, "ActiveSpr").SetActive(false); //设置图片
}
Utility.Get<UILabel>(goTab.transform, "Lab").color = isShow ? tabShow : tabHide;
Utility.FindChildren(goTab.transform, "ActiveSpr").SetActive(isShow);
mCurChooseGo = goTab; //给当前选择的按钮重新赋值
}
写的不好,有什么好的意见,评论指正!