UISavedOption.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Saved Option")]
  4. public class UISavedOption : MonoBehaviour
  5. {
  6. private string key
  7. {
  8. get
  9. {
  10. return (!string.IsNullOrEmpty(this.keyName)) ? this.keyName : ("NGUI State: " + base.name);
  11. }
  12. }
  13. private void Awake()
  14. {
  15. this.mList = base.GetComponent<UIPopupList>();
  16. this.mCheck = base.GetComponent<UIToggle>();
  17. this.mSlider = base.GetComponent<UIProgressBar>();
  18. }
  19. private void OnEnable()
  20. {
  21. if (this.mList != null)
  22. {
  23. EventDelegate.Add(this.mList.onChange, new EventDelegate.Callback(this.SaveSelection));
  24. string @string = PlayerPrefs.GetString(this.key);
  25. if (!string.IsNullOrEmpty(@string))
  26. {
  27. this.mList.value = @string;
  28. }
  29. }
  30. else if (this.mCheck != null)
  31. {
  32. EventDelegate.Add(this.mCheck.onChange, new EventDelegate.Callback(this.SaveState));
  33. this.mCheck.value = (PlayerPrefs.GetInt(this.key, (!this.mCheck.startsActive) ? 0 : 1) != 0);
  34. }
  35. else if (this.mSlider != null)
  36. {
  37. EventDelegate.Add(this.mSlider.onChange, new EventDelegate.Callback(this.SaveProgress));
  38. this.mSlider.value = PlayerPrefs.GetFloat(this.key, this.mSlider.value);
  39. }
  40. else
  41. {
  42. string string2 = PlayerPrefs.GetString(this.key);
  43. UIToggle[] componentsInChildren = base.GetComponentsInChildren<UIToggle>(true);
  44. int i = 0;
  45. int num = componentsInChildren.Length;
  46. while (i < num)
  47. {
  48. UIToggle uitoggle = componentsInChildren[i];
  49. uitoggle.value = (uitoggle.name == string2);
  50. i++;
  51. }
  52. }
  53. }
  54. private void OnDisable()
  55. {
  56. if (this.mCheck != null)
  57. {
  58. EventDelegate.Remove(this.mCheck.onChange, new EventDelegate.Callback(this.SaveState));
  59. }
  60. else if (this.mList != null)
  61. {
  62. EventDelegate.Remove(this.mList.onChange, new EventDelegate.Callback(this.SaveSelection));
  63. }
  64. else if (this.mSlider != null)
  65. {
  66. EventDelegate.Remove(this.mSlider.onChange, new EventDelegate.Callback(this.SaveProgress));
  67. }
  68. else
  69. {
  70. UIToggle[] componentsInChildren = base.GetComponentsInChildren<UIToggle>(true);
  71. int i = 0;
  72. int num = componentsInChildren.Length;
  73. while (i < num)
  74. {
  75. UIToggle uitoggle = componentsInChildren[i];
  76. if (uitoggle.value)
  77. {
  78. PlayerPrefs.SetString(this.key, uitoggle.name);
  79. break;
  80. }
  81. i++;
  82. }
  83. }
  84. }
  85. public void SaveSelection()
  86. {
  87. PlayerPrefs.SetString(this.key, UIPopupList.current.value);
  88. }
  89. public void SaveState()
  90. {
  91. PlayerPrefs.SetInt(this.key, (!UIToggle.current.value) ? 0 : 1);
  92. }
  93. public void SaveProgress()
  94. {
  95. PlayerPrefs.SetFloat(this.key, UIProgressBar.current.value);
  96. }
  97. public string keyName;
  98. private UIPopupList mList;
  99. private UIToggle mCheck;
  100. private UIProgressBar mSlider;
  101. }