| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using UnityEngine.UI;
- using System;
- using UnityCommon;
- namespace DTYJ
- {
- public class UIToolPanel : MonoBehaviour
- {
- public static UIToolPanel instance;
- public DicStringSprite dicPic;
- private GameObject goItem;
- private Transform tfImage;
- private void Awake()
- {
- instance = this;
- goItem = transform.GetChild(0).GetChild(1).gameObject;
- goItem.SetActive(false);
- tfImage = transform.GetChild(1);
- Init();
- }
- private void Init()
- {
- foreach (var item in dicPic)
- {
- var goNew = Instantiate(goItem, goItem.transform.parent);
- goNew.SetActive(true);
- var itemName = item.Key;
- goNew.GetComponent<Image>().sprite = item.Value;
- goNew.GetComponent<Button>().onClick.AddListener(() => { OnClickItem(itemName); });
- goNew.GetComponentInChildren<Text>().text = itemName;
- }
- }
- public string nowSelected = null;
- private void OnClickItem(string itemName)
- {
- SelectItem(itemName);
- }
- private void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- if (nowSelected != null)
- {
- CancelSelect();
- }
- }
- if (tfImage.gameObject.activeSelf)
- {
- tfImage.position = Input.mousePosition;
- }
- }
- private void CancelSelect()
- {
- Utility.DelayExe(0.2f, () =>
- {
- nowSelected = null;
- tfImage.gameObject.SetActive(false);
- });
- }
- private void SelectItem(string strName)
- {
- nowSelected = strName;
- tfImage.GetComponent<Image>().sprite = dicPic[strName];
- tfImage.gameObject.SetActive(true);
- }
- [Serializable]
- public class DicStringSprite : UnitySerializedDictionary<string, Sprite> { }
- public abstract class UnitySerializedDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
- {
- [SerializeField, HideInInspector]
- private List<TKey> keyData = new List<TKey>();
- [SerializeField, HideInInspector]
- private List<TValue> valueData = new List<TValue>();
- void ISerializationCallbackReceiver.OnAfterDeserialize()
- {
- this.Clear();
- for (int i = 0; i < this.keyData.Count && i < this.valueData.Count; i++)
- {
- this[this.keyData[i]] = this.valueData[i];
- }
- }
- void ISerializationCallbackReceiver.OnBeforeSerialize()
- {
- this.keyData.Clear();
- this.valueData.Clear();
- foreach (var item in this)
- {
- this.keyData.Add(item.Key);
- this.valueData.Add(item.Value);
- }
- }
- }
- }
- }
|