NGUI Checkbox与PlayerPrefs
UICheckboxPrefs.cs
1,bool isChecked:false 为“初始”状态,true为“选中”;
2,bool startsChecked:true,一运行,就显示UISprite :checkSprite;
3,UISprite checkSprite:一般为“选中”状态的UI;
using UnityEngine;
using AnimationOrTween;
[AddComponentMenu("WuKk/UI/Checkbox Prefs")]
publicclass UICheckboxPrefs : MonoBehaviour
{
static public UICheckboxPrefs current;
public delegate void OnStateChange (bool state);
/// <summary>
/// Sprite that's visible when the 'isChecked' status is 'true'.
/// </summary>
public UISprite checkSprite;
/// <summary>
/// Animation to play on the checkmark sprite, if any.
/// </summary>
public Animation checkAnimation;
/// <summary>
/// Whether the checkbox starts checked.
/// </summary>
public bool startsChecked = true;
/// <summary>
/// If the checkbox is part of a radio button group, specify the root object to use that all checkboxes are parented to.
/// </summary>
public Transform radioButtonRoot;
/// <summary>
/// Can the radio button option be 'none'?
/// </summary>
public bool optionCanBeNone = false;
/// <summary>
/// Generic event receiver that will be notified when the state changes.
/// </summary>
public GameObject eventReceiver;
/// <summary>
/// Function that will be called on the event receiver when the state changes.
/// </summary>
public string functionName = "OnActivate";
/// <summary>
/// Delegate that will be called when the checkbox's state changes. Faster than using 'eventReceiver'.
/// </summary>
public OnStateChange onStateChange;
// Prior to 1.90 'option' was used to toggle the radio button group functionality
[HideInInspector][SerializeField] bool option = false;
bool mChecked = true;
bool mStarted = false;
Transform mTrans;
/// <summary>
/// Whether the checkbox is checked.
/// </summary>
public bool isChecked
{
get { return mChecked; }
set { if (radioButtonRoot == null || value || optionCanBeNone || !mStarted) Set(value); }
}
/// <summary>
/// Legacy functionality support -- set the radio button root if the 'option' value was 'true'.
/// </summary>
void Awake ()
{
mTrans = transform;
startsChecked=isPrefsSet();
if (checkSprite != null) checkSprite.alpha = startsChecked ? 1f : 0f;
if (checkSprite != null && startsChecked)
{
Color c = checkSprite.color;
c.a = mChecked ? 1f : 0f;
checkSprite.color=c;
}
if (option)
{
option = false;
if (radioButtonRoot == null) radioButtonRoot = mTrans.parent;
}
}
/// <summary>
/// Activate the initial state.
/// </summary>
void Start ()
{
if (eventReceiver == null) eventReceiver = gameObject;
mChecked = !startsChecked;
mStarted = true;
Set(startsChecked);
}
/// <summary>
/// Check or uncheck on click.
/// </summary>
void OnClick () { if (enabled) isChecked = !isChecked; }
/// <summary>
/// Fade out or fade in the checkmark and notify the target of OnChecked event.
/// </summary>
void Set (bool state)
{
if (!mStarted)
{
mChecked = state;
startsChecked = state;
if (checkSprite != null) checkSprite.alpha = state ? 1f : 0f;
}
else if (mChecked != state)
{
// Uncheck all other checkboxes
if (radioButtonRoot != null && state)
{
UICheckboxPrefs[] cbs = radioButtonRoot.GetComponentsInChildren<UICheckboxPrefs>(true);
for (int i = 0, imax = cbs.Length; i < imax; ++i)
{
UICheckboxPrefs cb = cbs[i];
if (cb != this && cb.radioButtonRoot == radioButtonRoot) cb.Set(false);
}
}
// Remember the state
mChecked = state;
// Tween the color of the checkmark
if (checkSprite != null)
{
Color c = checkSprite.color;
c.a = mChecked ? 1f : 0f;
TweenColor.Begin(checkSprite.gameObject, 0.2f, c);
}
// Notify the delegate
if (onStateChange != null) onStateChange(mChecked);
// Send out the event notification
if (eventReceiver != null && !string.IsNullOrEmpty(functionName))
{
current = this;
eventReceiver.SendMessage(functionName, mChecked, SendMessageOptions.DontRequireReceiver);
}
// Play the checkmark animation
if (checkAnimation != null)
{
ActiveAnimation.Play(checkAnimation, state ? Direction.Forward : Direction.Reverse);
}
checkboxPrefsSet(mChecked);
}
}
public string prefsKey="";
public int prefsInitInt=0;
public int prefsSetInt=1;
void checkboxPrefsSet(bool isActivity){
if(isActivity)
PlayerPrefs.SetInt(prefsKey,prefsSetInt);
else
PlayerPrefs.SetInt(prefsKey,prefsInitInt);
}
bool isPrefsSet(){
return ( PlayerPrefs.GetInt(prefsKey,prefsInitInt)==prefsInitInt ? false:true);
}
}
NGUI Checkbox与PlayerPrefs的更多相关文章
- 关于Unity中NGUI的Checkbox复选框、Slider滑动条和Button的6种触发回调事件的方式
Checkbox复选框 1.创建一个NGUI背景Sprite1节点 2.打开NGUI---->Open---->Prefab Toolbar---->选择一个复选框节点,拖拽到背景节 ...
- NGUI的CheckBox的使用(toggle script)
一,我们先添加一个sprite,选择sprite,右键选择attach,添加box collider, 然后右键选择attach,添加toggle script,得到如下图结果 1,但是如果你没有给U ...
- NGUI OnChange Event
那些组件有OnChange? 下面这些组件都有OnChange事件,当你点击,下拉选择时,就会触发它们. NGUI中对应的组件 PopupList (下拉列表) Toggle (复选框) Input ...
- NGUI学习笔记汇总
NGUI学习笔记汇总,适用于NGUI2.x,NGUI3.x 一.NGUI的直接用法 1. Attach a Collider:表示为NGUI的某些物体添加碰撞器,如果界面是用NGUI做的,只能这样添加 ...
- C#程序员整理的Unity 3D笔记(十五):Unity 3D UI控件至尊–NGUI
目前,UGUI问世不过半年(其随着Unity 4.6发布问世),而市面上商用的产品,UI控件的至尊为NGUI:影响力和广度(可搜索公司招聘Unity 3D,常常能看到对NGUI关键词). NGUI虽然 ...
- NGUI系列教程三
接下来我们再来看Progress Bar和Slider,对比参数我们可以发现,Progress Bar和slider的明显区别在于slider多一个Thumb选项,这里的Thumb就是我们拖动的时候点 ...
- NGUI 的使用教程与实例(入门)(1 )
NGUI教程:步骤1-Scene 1.创建一个新的场景(New Scene).2.选择并删除场景里的MainCamera.3.在NGUI菜单下选择Create a New UI,会打开UI创建向导. ...
- Unity3d ngui基础教程
Unity3d ngui基础教程 NGUI教程:步骤1-Scene 1.创建一个新的场景(New Scene).2.选择并删除场景里的MainCamera.3.在NGUI菜单下选择Create a N ...
- NGUI: Documentation
Video Tutorials Basic Tutorial (v.2.5.0+) SD & HD atlas switching (advanced) Packed Font (advanc ...
随机推荐
- Android之——获取手机安装的应用程序
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47114331 前几篇有关Android的博文中.向大家介绍了几个项目中经常使用的有 ...
- Android EditText输入字数限制总结(包含中文输入内存溢出的解决方法)
转载请注明,大飞:http://blog.csdn.net/rflyee/article/details/38856539 限定EditText输入个数的解决方式非常多,可是一般主要考虑两点.也就是处 ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何获取标准驱动器扭矩值获取电流值
双击某个驱动器(以松下伺服驱动器为例),在Process Data中,注意默认显示了PDO mapping1的数据(Error code, status word等) 注意左侧,2和3分别表示了与 ...
- Linux学习笔记 (七)挂载命令
在Linux中,光盘,U盘,硬盘在使用之前必须进行挂载,挂载类似windows中的分配盘符. 一.查看挂载和自动挂载 1.mount:直接输入mount表示查看系统中所有的挂载点. 2.mount - ...
- GCD编程-串行队列与并发队列
接着上面的GCD封装,以下进行列子验证 1.导入GCD.h 2.创一个串行队列: - (void)serailQueue{ //创建出队列 GCDQueue *queue = [[GCDQueue ...
- Linux 下动态查找磁盘数量方法
#!/bin/bash ##磁盘数量 Disk=$( fdisk -l |grep 'Disk' |grep 'sd' |awk -F , '{print "%s",substr( ...
- Web前端开发实战1:二级下拉式菜单之CSS实现
二级下拉式菜单在各大学校站点.电商类站点.新闻类站点等大型?站点非经常见,那么它的实现原理是什么呢? 学习了Web前端开发的知识后,我们是能够实现这种功能的.复杂的都是从基础效果上加入做出来的.原理和 ...
- EF中多表公共字段,以及设置EntityBase使所有实体类继承自定义类
使用EF框架访问数据库时,如果某些表具有公共字段,例如在审核流程中,对于各类申请单资料的创建人.创建时间.修改人.修改时间,这些可能多表都需要的字段,如果在每个实体中进行赋值操作显然是类似和重复的,下 ...
- 各种broker对比
broker的主要职责是接受发布者发布的所有消息,并将其过滤后分发给不同的消息订阅者.如今有很多的broker,下面就是一张关于各种broker对比的图片: 在使用mosquitto时,如果想使用集群 ...
- 8.1.3 在BroadcastReceiver中启动Service
2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...