admin 管理员组文章数量: 1086864
NGUI
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 togglesif (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 statemIsActive = state;// Tween the color of the active spriteif (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 animationif (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; //给当前选择的按钮重新赋值}
写的不好,有什么好的意见,评论指正!
本文标签: NGUI
版权声明:本文标题:NGUI 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1687705509a131415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论