mod: 修改绑定数据
parent
cbd3ba86b8
commit
57dc91f28b
|
@ -98,22 +98,22 @@ namespace ZEditor
|
||||||
|
|
||||||
allText = allText.Replace("#CLASSNAME#", substring);
|
allText = allText.Replace("#CLASSNAME#", substring);
|
||||||
|
|
||||||
allText = allText.Replace("#VARIABLEDEFINITION#", ReplaceVD(binding.AllData));
|
allText = allText.Replace("#VARIABLEDEFINITION#", ReplaceVD(binding.Datas));
|
||||||
allText = allText.Replace("#INITFIND#", ReplaceInitBind(binding.AllData));
|
allText = allText.Replace("#INITFIND#", ReplaceInitBind(binding.Datas));
|
||||||
allText = allText.Replace("#INITADDLISTENER#", ReplaceInitAddListener(binding.AllData));
|
allText = allText.Replace("#INITADDLISTENER#", ReplaceInitAddListener(binding.Datas));
|
||||||
allText = allText.Replace("#METHOD#", ReplaceAddMethod(binding.AllData));
|
allText = allText.Replace("#METHOD#", ReplaceAddMethod(binding.Datas));
|
||||||
allText = allText.Replace("#DISPOSE#", ReplaceDisposeRemoveListener(binding.AllData));
|
allText = allText.Replace("#DISPOSE#", ReplaceDisposeRemoveListener(binding.Datas));
|
||||||
allText = allText.Replace("#DISPOSEISNULL#", ReplaceDisposeIsNull(binding.AllData));
|
allText = allText.Replace("#DISPOSEISNULL#", ReplaceDisposeIsNull(binding.Datas));
|
||||||
|
|
||||||
File.WriteAllText(filePath, allText);
|
File.WriteAllText(filePath, allText);
|
||||||
Debug.Log("自动生成绑定数据 完成");
|
Debug.Log("自动生成绑定数据 完成");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ReplaceVD(IReadOnlyList<GameObjectBindingData> AllData) // 定义
|
static string ReplaceVD(IReadOnlyList<GameObjectBindingData> Datas) // 定义
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
foreach (var data in AllData)
|
foreach (var data in Datas)
|
||||||
{
|
{
|
||||||
var typeStr = CheckType(data);
|
var typeStr = CheckType(data);
|
||||||
sb.AppendLine($"public {typeStr} {data.name};");
|
sb.AppendLine($"public {typeStr} {data.name};");
|
||||||
|
@ -122,10 +122,10 @@ namespace ZEditor
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ReplaceInitBind(IReadOnlyList<GameObjectBindingData> AllData) // 定义
|
static string ReplaceInitBind(IReadOnlyList<GameObjectBindingData> Datas) // 定义
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
foreach (var data in AllData)
|
foreach (var data in Datas)
|
||||||
{
|
{
|
||||||
var typeStr = CheckType(data);
|
var typeStr = CheckType(data);
|
||||||
sb.AppendLine($"{data.name} = GetValue<{typeStr}>(\"{data.name}\");");
|
sb.AppendLine($"{data.name} = GetValue<{typeStr}>(\"{data.name}\");");
|
||||||
|
@ -134,10 +134,10 @@ namespace ZEditor
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ReplaceInitAddListener(IReadOnlyList<GameObjectBindingData> AllData) // 定义
|
static string ReplaceInitAddListener(IReadOnlyList<GameObjectBindingData> Datas) // 定义
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
foreach (var data in AllData)
|
foreach (var data in Datas)
|
||||||
{
|
{
|
||||||
var typeStr = CheckType(data);
|
var typeStr = CheckType(data);
|
||||||
if (typeStr == nameof(Button))
|
if (typeStr == nameof(Button))
|
||||||
|
@ -153,10 +153,10 @@ namespace ZEditor
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ReplaceAddMethod(IReadOnlyList<GameObjectBindingData> AllData) // 定义
|
static string ReplaceAddMethod(IReadOnlyList<GameObjectBindingData> Datas) // 定义
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
foreach (var data in AllData)
|
foreach (var data in Datas)
|
||||||
{
|
{
|
||||||
var typeStr = CheckType(data);
|
var typeStr = CheckType(data);
|
||||||
if (typeStr == nameof(Button))
|
if (typeStr == nameof(Button))
|
||||||
|
@ -176,10 +176,10 @@ namespace ZEditor
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ReplaceDisposeRemoveListener(IReadOnlyList<GameObjectBindingData> AllData) // 定义
|
static string ReplaceDisposeRemoveListener(IReadOnlyList<GameObjectBindingData> Datas) // 定义
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
foreach (var data in AllData)
|
foreach (var data in Datas)
|
||||||
{
|
{
|
||||||
var typeStr = CheckType(data);
|
var typeStr = CheckType(data);
|
||||||
if (typeStr == nameof(Button))
|
if (typeStr == nameof(Button))
|
||||||
|
@ -195,10 +195,10 @@ namespace ZEditor
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ReplaceDisposeIsNull(IReadOnlyList<GameObjectBindingData> AllData) // 定义
|
static string ReplaceDisposeIsNull(IReadOnlyList<GameObjectBindingData> Datas) // 定义
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
foreach (var data in AllData)
|
foreach (var data in Datas)
|
||||||
{
|
{
|
||||||
var typeStr = CheckType(data);
|
var typeStr = CheckType(data);
|
||||||
sb.AppendLine($"{data.name} = null;");
|
sb.AppendLine($"{data.name} = null;");
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 753282de038d4f6f91930e91e61dda21
|
||||||
|
timeCreated: 1733207236
|
|
@ -1,78 +1,6 @@
|
||||||
using System;
|
namespace ZC
|
||||||
using System.Collections.Generic;
|
|
||||||
using Sirenix.OdinInspector;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace ZC
|
|
||||||
{
|
{
|
||||||
public class GameObjectBinding : MonoBehaviour
|
public class GameObjectBinding : BindingBase
|
||||||
{
|
{
|
||||||
[SerializeField] private List<GameObjectBindingData> Datas = new List<GameObjectBindingData>();
|
|
||||||
|
|
||||||
#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
|
|
||||||
|
|
||||||
public GameObject GetValue(string nameStr)
|
|
||||||
{
|
|
||||||
foreach (var data in Datas)
|
|
||||||
{
|
|
||||||
if (data.name == nameStr)
|
|
||||||
{
|
|
||||||
return data.go;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NullReferenceException($"没有找到绑定这个名字的物体,name:{nameStr}");
|
|
||||||
}
|
|
||||||
|
|
||||||
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}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue