Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;

/// <summary>
/// Attach this script to a popup list, the parent of a group of toggles, or to a toggle itself to save its state.
/// </summary>

[AddComponentMenu("NGUI/Interaction/Saved Option")]
public class UISavedOption : MonoBehaviour
{
	/// <summary>
	/// PlayerPrefs-stored key for this option.
	/// </summary>

	public string keyName;

	string key { get { return (string.IsNullOrEmpty(keyName)) ? "NGUI State: " + name : keyName; } }

	UIPopupList mList;
	UIToggle mCheck;

	/// <summary>
	/// Cache the components and register a listener callback.
	/// </summary>

	void Awake ()
	{
		mList = GetComponent<UIPopupList>();
		mCheck = GetComponent<UIToggle>();	
	}

	/// <summary>
	/// Load and set the state of the toggles.
	/// </summary>

	void OnEnable ()
	{
		if (mList != null) EventDelegate.Add(mList.onChange, SaveSelection);
		if (mCheck != null) EventDelegate.Add(mCheck.onChange, SaveState);

		if (mList != null)
		{
			string s = PlayerPrefs.GetString(key);
			if (!string.IsNullOrEmpty(s)) mList.value = s;
			return;
		}

		if (mCheck != null)
		{
			mCheck.value = (PlayerPrefs.GetInt(key, 1) != 0);
		}
		else
		{
			string s = PlayerPrefs.GetString(key);
			UIToggle[] toggles = GetComponentsInChildren<UIToggle>(true);

			for (int i = 0, imax = toggles.Length; i < imax; ++i)
			{
				UIToggle ch = toggles[i];
				ch.value = (ch.name == s);
			}
		}
	}

	/// <summary>
	/// Save the state on destroy.
	/// </summary>

	void OnDisable ()
	{
		if (mCheck != null) EventDelegate.Remove(mCheck.onChange, SaveState);
		if (mList != null) EventDelegate.Remove(mList.onChange, SaveSelection);

		if (mCheck == null && mList == null)
		{
			UIToggle[] toggles = GetComponentsInChildren<UIToggle>(true);

			for (int i = 0, imax = toggles.Length; i < imax; ++i)
			{
				UIToggle ch = toggles[i];

				if (ch.value)
				{
					PlayerPrefs.SetString(key, ch.name);
					break;
				}
			}
		}
	}

	/// <summary>
	/// Save the selection.
	/// </summary>

	void SaveSelection () { PlayerPrefs.SetString(key, UIPopupList.current.value); }

	/// <summary>
	/// Save the state.
	/// </summary>

	void SaveState () { PlayerPrefs.SetInt(key, UIToggle.current.value ? 1 : 0); }
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Scripts/Interaction/UISavedOption.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000