2025-04-26 21:05:13 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace HK
|
|
|
|
|
{
|
|
|
|
|
public abstract class BindingBase : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private List<GameObjectBindingData> _datas = new List<GameObjectBindingData>();
|
|
|
|
|
|
|
|
|
|
public IReadOnlyList<GameObjectBindingData> Datas => _datas;
|
|
|
|
|
|
|
|
|
|
public GameObject GetValue(string nameStr)
|
|
|
|
|
{
|
|
|
|
|
foreach (var data in _datas)
|
|
|
|
|
{
|
|
|
|
|
if (data.name == nameStr)
|
|
|
|
|
{
|
|
|
|
|
return data.go;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NullReferenceException($"没有找到绑定这个名字的物体,name:{nameStr},物体名字:{gameObject.name}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetValue<T>(string nameStr) where T : Component
|
|
|
|
|
{
|
|
|
|
|
foreach (var data in _datas)
|
|
|
|
|
{
|
|
|
|
|
if (data.name == nameStr)
|
|
|
|
|
{
|
|
|
|
|
return data.go.GetComponent<T>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NullReferenceException($"没有找到绑定这个名字的物体,name:{nameStr},物体名字:{gameObject.name}");
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 15:53:01 +08:00
|
|
|
|
public List<GameObject> GetAllValue()
|
|
|
|
|
{
|
|
|
|
|
List<GameObject> result = new List<GameObject>();
|
|
|
|
|
foreach (var data in _datas)
|
|
|
|
|
{
|
|
|
|
|
result.Add(data.go);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-26 21:05:13 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
[Button("刷新键值对数据")]
|
|
|
|
|
void Refresh()
|
|
|
|
|
{
|
|
|
|
|
foreach (var bindingData in _datas)
|
|
|
|
|
{
|
|
|
|
|
bindingData.name = bindingData.go.name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearValue()
|
|
|
|
|
{
|
|
|
|
|
_datas.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddValue(Transform trans)
|
|
|
|
|
{
|
|
|
|
|
foreach (var data in _datas)
|
|
|
|
|
{
|
|
|
|
|
if (data.name == trans.name)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"重复添加了, {trans.name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_datas.Add(new GameObjectBindingData() { name = trans.name, go = trans.gameObject });
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class GameObjectBindingData
|
|
|
|
|
{
|
|
|
|
|
[HorizontalGroup("aaa")] [LabelText("k"), LabelWidth(10)]
|
|
|
|
|
public string name;
|
|
|
|
|
|
|
|
|
|
[HorizontalGroup("aaa")] [LabelText("v"), LabelWidth(10)] [OnValueChanged(nameof(OnValueChanged))]
|
|
|
|
|
public GameObject go;
|
|
|
|
|
|
|
|
|
|
private void OnValueChanged()
|
|
|
|
|
{
|
|
|
|
|
if (go != null)
|
|
|
|
|
name = go.name;
|
|
|
|
|
else
|
|
|
|
|
name = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|