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的更多相关文章

  1. 关于Unity中NGUI的Checkbox复选框、Slider滑动条和Button的6种触发回调事件的方式

    Checkbox复选框 1.创建一个NGUI背景Sprite1节点 2.打开NGUI---->Open---->Prefab Toolbar---->选择一个复选框节点,拖拽到背景节 ...

  2. NGUI的CheckBox的使用(toggle script)

    一,我们先添加一个sprite,选择sprite,右键选择attach,添加box collider, 然后右键选择attach,添加toggle script,得到如下图结果 1,但是如果你没有给U ...

  3. NGUI OnChange Event

    那些组件有OnChange? 下面这些组件都有OnChange事件,当你点击,下拉选择时,就会触发它们. NGUI中对应的组件 PopupList (下拉列表) Toggle (复选框) Input ...

  4. NGUI学习笔记汇总

    NGUI学习笔记汇总,适用于NGUI2.x,NGUI3.x 一.NGUI的直接用法 1. Attach a Collider:表示为NGUI的某些物体添加碰撞器,如果界面是用NGUI做的,只能这样添加 ...

  5. C#程序员整理的Unity 3D笔记(十五):Unity 3D UI控件至尊–NGUI

    目前,UGUI问世不过半年(其随着Unity 4.6发布问世),而市面上商用的产品,UI控件的至尊为NGUI:影响力和广度(可搜索公司招聘Unity 3D,常常能看到对NGUI关键词). NGUI虽然 ...

  6. NGUI系列教程三

    接下来我们再来看Progress Bar和Slider,对比参数我们可以发现,Progress Bar和slider的明显区别在于slider多一个Thumb选项,这里的Thumb就是我们拖动的时候点 ...

  7. NGUI 的使用教程与实例(入门)(1 )

    NGUI教程:步骤1-Scene 1.创建一个新的场景(New Scene).2.选择并删除场景里的MainCamera.3.在NGUI菜单下选择Create a New UI,会打开UI创建向导. ...

  8. Unity3d ngui基础教程

    Unity3d ngui基础教程 NGUI教程:步骤1-Scene 1.创建一个新的场景(New Scene).2.选择并删除场景里的MainCamera.3.在NGUI菜单下选择Create a N ...

  9. NGUI: Documentation

    Video Tutorials Basic Tutorial (v.2.5.0+) SD & HD atlas switching (advanced) Packed Font (advanc ...

随机推荐

  1. 在Centos 7中使用 Docker搭建MySQL异地双向复制环境

    (0)一些准备操作: Centos安装好之后(这里使用的是vm虚拟机) 将当前用户添加到sudoers中: su root vim /etc/sudoers 找到 root ALL=(ALL) ALL ...

  2. 使用JavaScript和Canvas实现下雪动画效果

    该下雪动画效果使用了HTML5中Canvas画布实现,其中涉及了物理学中曲线运动的相关知识与运算. index.html <!DOCTYPE html> <html lang=&qu ...

  3. 阿里云ECS linux通过iptables 配置SNAT代理网关,实现局域网上网

    场景说明: 本文将介绍如何通过为VPC中Linux系统的ECS实例配置SNAT,实现无公网ECS通过有EIP的服务器代理访问公网. 步骤: 1.使用SSH的方法登陆一个已经绑定EIP外网的ECS实例. ...

  4. Java实现图片裁剪预览功能

    在项目中.我们须要做些类似头像上传,图片裁剪的功能,ok看以下文章! 须要插件:jQuery Jcrop 后端代码: package org.csg.upload; import java.awt.R ...

  5. 微信小程序bindtap和catchtap区别

    bindtap可以产生冒泡事件 catchtap只自身触发事件,不会传递到父视图         文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注 ...

  6. C# Windows form application 播放小视频

    1. 下载direcly-show lib DLL点击打开链接 2. DxPlay.cs (能够在下载的样例中找到):    public class DxPlay : IDisposable { e ...

  7. 《深入理解jvm》笔记---第七章

    虚拟机类载入机制 1. 类的生命周期: 载入.验证.准备.解析.初始化.使用.卸载七个阶段.当中验证.准备.解析三个阶段统称为连接. 当中,解析的阶段的时机并不一定. 2. Java类载入的时机: J ...

  8. Odoo8中“更多”下拉菜单选项指定后台执行代码

    在Odoo8中的仓库模块,根据每日最小安全库存数量,系统会自动生成一些补货单,而且是一个产品会生成一笔,如果产品比较多,这里生成的补货单也会很多. 如果这里的补货单没有即时处理,那相同产品后续不会再生 ...

  9. Python 的错误和异常处理

    语法错误 Python 的语法错误或者称之为解析错,如下: >>> while True print('Hello world') File "<stdin>& ...

  10. 牛散NO.1:MACD计啜诱多,勾魂枪连环夺命时

    上证日线“连环夺命勾魂枪” 话说MACD中圈C的回勾,好事者皆认为新的冲击波即将曙光再现.伴随着K线出现红柱中阳,更多的投资者将会被这一勾诱惑得群情亢奋,盲断行情又要 起来了.但往往事与愿违,“潘金莲 ...