TH1/Unity/Assets/Plugins/UnityComponent/UIWindowProperty.cs
2025-08-20 02:24:48 +08:00

190 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class UIWindowProperty : MonoBehaviour
{
[System.Serializable]
public struct NodeElement
{
public string Key;
public GameObject Object;
}
[System.Serializable]
public struct NodeElement2
{
public string Key;
public List<GameObject> ObjectList;
}
// public enum AutoBindType
// {
// GameObject = 0,
// Transform = 1,
// Btn = 2,
// Image = 3
// }
[System.Serializable]
public struct NodeElementAuto
{
public string Key;
public GameObject Object;
public UIAutoBindRuleHelper.AutoBindType type;
}
[System.Serializable]
public struct ValueElement
{
public string Key;
public float Value;
}
public List<NodeElementAuto> GameObjectList = new List<NodeElementAuto>();
public List<NodeElement2> GameObjectList2 = new List<NodeElement2>();
public List<NodeElementAuto> AutoGameObjectList = new List<NodeElementAuto>();
//
public List<ValueElement> ValueList = new List<ValueElement>();
//
public string ClassName;
public float GetValue(string key, float defaultValue)
{
for (int iter = 0; iter < ValueList.Count; ++iter)
{
ValueElement iterElement = ValueList[iter];
if (string.Compare(iterElement.Key, key, true) == 0)
{
return iterElement.Value;
}
}
//
return defaultValue;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++
public GameObject GetGameObject(string key)
{
return GetGameObject(key, true);
}
public GameObject GetGameObject(string key, bool logMessage)
{
for (int iter = 0; iter < GameObjectList.Count; ++iter)
{
NodeElementAuto iterElement = GameObjectList[iter];
if (string.Compare(iterElement.Key, key, true) == 0)
{
return iterElement.Object;
}
}
for (int iter = 0; iter < AutoGameObjectList.Count; ++iter)
{
NodeElementAuto iterElement = AutoGameObjectList[iter];
if (string.Compare(iterElement.Key, key, true) == 0)
{
return iterElement.Object;
}
}
if (logMessage)
{
ViDebuger.Error("WindowProperty[" + name + "].GameObject[" + key + "] not exist!");
}
return null;
}
public T GetComponent<T>(string key) where T : Component
{
return GetComponent<T>(key, true);
}
public T GetComponent<T>(string key, bool logMessage) where T : Component
{
GameObject obj = GetGameObject(key, logMessage);
if (obj != null)
{
T component = obj.GetComponent<T>();
if (System.Object.ReferenceEquals(component, null))
{
ViDebuger.Error("WindowProperty[" + name + "]." + typeof(T).Name + "[" + key + "] not exist!");
}
return component;
}
else
{
return null;
}
}
public T GetComponentInChildren<T>(string key) where T : Component
{
GameObject obj = GetGameObject(key);
if (obj != null)
{
T component = obj.GetComponentInChildren<T>();
if (System.Object.ReferenceEquals(component, null))
{
ViDebuger.Error("WindowProperty[" + name + "]." + typeof(T).Name + "[" + key + "] not exist!");
}
return component;
}
else
{
return null;
}
}
public T GetComponentInChildren<T>(string key, bool includeInactive) where T : Component
{
GameObject obj = GetGameObject(key);
if (obj != null)
{
T component = obj.GetComponentInChildren<T>(includeInactive);
if (System.Object.ReferenceEquals(component, null))
{
ViDebuger.Error("WindowProperty[" + name + "]." + typeof(T).Name + "[" + key + "] not exist!");
}
return component;
}
else
{
return null;
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public List<GameObject> GetGameObjectList(string key)
{
for (int iter = 0; iter < GameObjectList2.Count; ++iter)
{
NodeElement2 iterElement = GameObjectList2[iter];
if (string.Compare(iterElement.Key, key, true) == 0)
{
return iterElement.ObjectList;
}
}
ViDebuger.Error("WindowProperty[" + name + "].GameObjectList[" + key + "] not exist!");
return null;
}
public void GetComponentList<T>(string key, List<T> resultList) where T : Component
{
List<GameObject> objList = GetGameObjectList(key);
if (objList != null)
{
for (int iter = 0; iter < objList.Count; ++iter)
{
GameObject iterObj = objList[iter];
if (iterObj != null)
{
T component = iterObj.GetComponent<T>();
if (System.Object.ReferenceEquals(component, null))
{
ViDebuger.Error("WindowProperty[" + name + "].GameObject[" + iterObj.name + "]." + typeof(T).Name + "[" + key + "] not exist!");
}
resultList.Add(component);
}
}
}
}
}