HomeView_TopBarView.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. [Serializable]
  7. public class TopBarButtonInfo
  8. {
  9. public string Name;
  10. public Image Icon;
  11. public Text text;
  12. public bool selected;
  13. public DeviceMode deviceMode;
  14. public GameObject container;
  15. }
  16. public class HomeView_TopBarView : MonoBehaviour
  17. {
  18. public List<TopBarButtonInfo> topBarButtonInfos;
  19. public UnityEngine.Events.UnityEvent<DeviceMode> onChangeTypeEvent;
  20. string _saveKey = "DeviceModeKey";
  21. int _value = 0;
  22. int _valueDefault = 0;
  23. // Start is called before the first frame update
  24. void Start()
  25. {
  26. int deviceValue = GetDeviceModeValue();
  27. GlobalData.MyDeviceMode = (DeviceMode)deviceValue;
  28. Debug.Log(" GlobalData.MyDeviceMode :" + GlobalData.MyDeviceMode);
  29. onChangeTypeEvent?.Invoke(GlobalData.MyDeviceMode);
  30. for (int i = 0; i < topBarButtonInfos.Count; i++)
  31. {
  32. TopBarButtonInfo topBarButtonInfo = topBarButtonInfos[i];
  33. if (i == deviceValue)
  34. {
  35. topBarButtonInfo.Icon.color = new Color32(0, 0, 0, 255);
  36. topBarButtonInfo.text.color = new Color32(48, 57, 57, 255);
  37. }
  38. else
  39. {
  40. topBarButtonInfo.Icon.color = new Color32(255, 255, 255, 255);
  41. topBarButtonInfo.text.color = new Color32(141, 150, 151, 255);
  42. }
  43. }
  44. }
  45. public TopBarButtonInfo GetCurrentTopBarButtonInfo() {
  46. return topBarButtonInfos[(int)GlobalData.MyDeviceMode];
  47. }
  48. // Update is called once per frame
  49. //void Update()
  50. //{
  51. //}
  52. public void onChangeType(int index)
  53. {
  54. if (index > 0 || index < topBarButtonInfos.Count) {
  55. for (int i = 0; i < topBarButtonInfos.Count; i++)
  56. {
  57. TopBarButtonInfo topBarButtonInfo = topBarButtonInfos[i];
  58. if (i == index) {
  59. topBarButtonInfo.Icon.color = new Color32(0, 0, 0, 255);
  60. topBarButtonInfo.text.color = new Color32(48, 57, 57, 255);
  61. }
  62. else {
  63. topBarButtonInfo.Icon.color = new Color32(255, 255, 255, 255);
  64. topBarButtonInfo.text.color = new Color32(141, 150, 151, 255);
  65. }
  66. }
  67. SetDeviceModeValue(index);
  68. GlobalData.MyDeviceMode = (DeviceMode)index;
  69. Debug.Log("Set GlobalData.MyDeviceMode :" + GlobalData.MyDeviceMode);
  70. onChangeTypeEvent?.Invoke(GlobalData.MyDeviceMode);
  71. if(HomeView.ins != null) HomeView.ins.UpdateParent();
  72. }
  73. }
  74. public int GetDeviceModeValue()
  75. {
  76. _value = PlayerPrefs.GetInt(_saveKey, _valueDefault);
  77. return _value;
  78. }
  79. public void SetDeviceModeValue(int value)
  80. {
  81. _value = value;
  82. PlayerPrefs.SetInt(_saveKey, _value);
  83. PlayerPrefs.Save();
  84. }
  85. }