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);
///
/// Sprite that's visible when the 'isChecked' status is 'true'.
///
public UISprite checkSprite;
///
/// Animation to play on the checkmark sprite, if any.
///
public Animation checkAnimation;
///
/// Whether the checkbox starts checked.
///
public bool startsChecked = true;
///
/// If the checkbox is part of a radio button group, specify the root object to use that all checkboxes are parented to.
///
public Transform radioButtonRoot;
///
/// Can the radio button option be 'none'?
///
public bool optionCanBeNone = false;
///
/// Generic event receiver that will be notified when the state changes.
///
public GameObject eventReceiver;
///
/// Function that will be called on the event receiver when the state changes.
///
public string functionName = "OnActivate";
///
/// Delegate that will be called when the checkbox's state changes. Faster than using 'eventReceiver'.
///
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;
///
/// Whether the checkbox is checked.
///
public bool isChecked
{
get { return mChecked; }
set { if (radioButtonRoot == null || value || optionCanBeNone || !mStarted) Set(value); }
}
///
/// Legacy functionality support -- set the radio button root if the 'option' value was 'true'.
///
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;
}
}
///
/// Activate the initial state.
///
void Start ()
{
if (eventReceiver == null) eventReceiver = gameObject;
mChecked = !startsChecked;
mStarted = true;
Set(startsChecked);
}
///
/// Check or uncheck on click.
///
void OnClick () { if (enabled) isChecked = !isChecked; }
///
/// Fade out or fade in the checkmark and notify the target of OnChecked event.
///
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
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);
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章