zoukankan      html  css  js  c++  java
  • 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);

    }

    }

     

  • 相关阅读:
    redis requires Ruby version >= 2.2.2.
    redis-持久化、主从复制、集群
    lucene索引文件大小优化小结
    spring+websocket的整合实例--可使用
    使用nexus3.10搭建maven私有仓库
    oracle 查看所有表的数据量并排序
    Spring框架-经典的案例和demo,一些可以直接用于生产,使用atomikos来处理多数据源的一致性事务等
    ORACLE WITH AS 用法
    判断对象部分属性是否为空
    代码行数统计
  • 原文地址:https://www.cnblogs.com/kuangwu/p/3257670.html
Copyright © 2011-2022 走看看