UIToolPanel.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using UnityEngine.UI;
  6. using System;
  7. using UnityCommon;
  8. namespace DTYJ
  9. {
  10. public class UIToolPanel : MonoBehaviour
  11. {
  12. public static UIToolPanel instance;
  13. public DicStringSprite dicPic;
  14. private GameObject goItem;
  15. private Transform tfImage;
  16. private void Awake()
  17. {
  18. instance = this;
  19. goItem = transform.GetChild(0).GetChild(1).gameObject;
  20. goItem.SetActive(false);
  21. tfImage = transform.GetChild(1);
  22. Init();
  23. }
  24. private void Init()
  25. {
  26. foreach (var item in dicPic)
  27. {
  28. var goNew = Instantiate(goItem, goItem.transform.parent);
  29. goNew.SetActive(true);
  30. var itemName = item.Key;
  31. goNew.GetComponent<Image>().sprite = item.Value;
  32. goNew.GetComponent<Button>().onClick.AddListener(() => { OnClickItem(itemName); });
  33. goNew.GetComponentInChildren<Text>().text = itemName;
  34. }
  35. }
  36. public string nowSelected = null;
  37. private void OnClickItem(string itemName)
  38. {
  39. SelectItem(itemName);
  40. }
  41. private void Update()
  42. {
  43. if (Input.GetMouseButtonDown(0))
  44. {
  45. if (nowSelected != null)
  46. {
  47. CancelSelect();
  48. }
  49. }
  50. if (tfImage.gameObject.activeSelf)
  51. {
  52. tfImage.position = Input.mousePosition;
  53. }
  54. }
  55. private void CancelSelect()
  56. {
  57. Utility.DelayExe(0.2f, () =>
  58. {
  59. nowSelected = null;
  60. tfImage.gameObject.SetActive(false);
  61. });
  62. }
  63. private void SelectItem(string strName)
  64. {
  65. nowSelected = strName;
  66. tfImage.GetComponent<Image>().sprite = dicPic[strName];
  67. tfImage.gameObject.SetActive(true);
  68. }
  69. [Serializable]
  70. public class DicStringSprite : UnitySerializedDictionary<string, Sprite> { }
  71. public abstract class UnitySerializedDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
  72. {
  73. [SerializeField, HideInInspector]
  74. private List<TKey> keyData = new List<TKey>();
  75. [SerializeField, HideInInspector]
  76. private List<TValue> valueData = new List<TValue>();
  77. void ISerializationCallbackReceiver.OnAfterDeserialize()
  78. {
  79. this.Clear();
  80. for (int i = 0; i < this.keyData.Count && i < this.valueData.Count; i++)
  81. {
  82. this[this.keyData[i]] = this.valueData[i];
  83. }
  84. }
  85. void ISerializationCallbackReceiver.OnBeforeSerialize()
  86. {
  87. this.keyData.Clear();
  88. this.valueData.Clear();
  89. foreach (var item in this)
  90. {
  91. this.keyData.Add(item.Key);
  92. this.valueData.Add(item.Value);
  93. }
  94. }
  95. }
  96. }
  97. }