87 lines
2.3 KiB
C#
87 lines
2.3 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using Sirenix.OdinInspector;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace ZC
|
|||
|
{
|
|||
|
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}");
|
|||
|
}
|
|||
|
|
|||
|
#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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|