2024-11-12 16:57:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace ZC
|
|
|
|
|
{
|
|
|
|
|
public class GameObjectBinding : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private List<GameObjectBindingData> Datas = new List<GameObjectBindingData>();
|
|
|
|
|
|
2024-11-22 16:13:10 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
public IReadOnlyList<GameObjectBindingData> AllData => Datas;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2024-11-12 16:57:51 +08:00
|
|
|
|
public GameObject GetValue(string nameStr)
|
|
|
|
|
{
|
|
|
|
|
foreach (var data in Datas)
|
|
|
|
|
{
|
|
|
|
|
if (data.name == nameStr)
|
|
|
|
|
{
|
|
|
|
|
return data.go;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NullReferenceException($"没有找到绑定这个名字的物体,name:{nameStr}");
|
|
|
|
|
}
|
2024-11-22 16:13:10 +08:00
|
|
|
|
|
|
|
|
|
public T GetValue<T>(string nameStr) where T : Component
|
2024-11-12 16:57:51 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var data in Datas)
|
|
|
|
|
{
|
|
|
|
|
if (data.name == nameStr)
|
|
|
|
|
{
|
|
|
|
|
return data.go.GetComponent<T>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NullReferenceException($"没有找到绑定这个名字的物体,name:{nameStr}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|