ShanxiKnowledgeBase/SXKnowledgeBase-master/sxknowledgebase/Assets/Scripts/UI/Public/UITool.cs

236 lines
8.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Schema;
using UnityEngine;
using UnityEngine.UI;
namespace AFeijia.UI
{
public class UITool : MonoBehaviour
{
System.Reflection.BindingFlags FieldBindingFlags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.SetProperty
| System.Reflection.BindingFlags.GetProperty;
System.Reflection.BindingFlags PropertyBindingFlags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.GetProperty;
private Graphic[] m_graphics;
protected Graphic[] m_Graphics { get { if (m_graphics == null) { m_graphics = GetComponentsInChildren<Graphic>(); Array.ForEach(m_graphics, g => m_graphicColors.Add(g, g.color)); } return m_graphics; } }
private Dictionary<Graphic, Color> m_graphicColors = new Dictionary<Graphic, Color>();
/// <summary>
/// 各Graphic的初始Color
/// </summary>
protected Dictionary<Graphic, Color> m_GraphicsColor => m_graphicColors;
/// <summary>
/// 所有继承该类的UI
/// </summary>
protected static List<UITool> ui_tools = new List<UITool>();
protected virtual void Awake()
{
ui_tools.Add(this);
UIInit();
}
/// <summary>
/// 初始化UI 查找
/// </summary>
protected void UIInit()
{
var myType = GetType();
try
{
//Field
var myFields = myType.GetFields(FieldBindingFlags);
foreach (var field in myFields)
{
var type = field.FieldType;
var fieldName = field.Name;
var attr = field.GetCustomAttributes(true);
if ( attr.Length != 0)
{
if (attr[0] is ManualAttribute)
{
continue;
}
}
if (fieldName.Contains("<") && fieldName.Contains(">") && fieldName.Contains("k__BackingField"))
continue;
var target = RecursiveFind(transform, fieldName);
if (target != null)
{
var component = target.GetComponent(field.FieldType);
field.SetValue(this, component);
}
else
{
Debug.LogWarning("未找到字段:" + fieldName);
}
//if (type.Module.Name.Equals("UnityEngine.UI.dll"))
//{
// //var fieldName = field.Name.Contains("_") ? field.Name.Split('_')[1] : field.Name;
// var fieldName = field.Name;
// var target = RecursiveFind(transform, fieldName);
// if (target != null)
// {
// var component = target.GetComponent(field.FieldType);
// field.SetValue(this, component);
// }
// else
// {
// Debug.Log("未找到:" + fieldName);
// }
//}
}
//Property
var myProperties = myType.GetProperties(PropertyBindingFlags);
foreach (var property in myProperties)
{
var type = property.PropertyType;
var propertyName = property.Name;
var target = RecursiveFind(transform, property.Name);
if (target != null)
{
var component = target.GetComponent(property.PropertyType);
property.SetValue(this, component);
}
else
{
Debug.Log("未找到属性:" + propertyName);
}
//if (type.Module.Name.Equals("UnityEngine.UI.dll"))
//{
// var propertyName = property.Name.Contains("_") ? property.Name.Split('_')[1] : property.Name;
// var target = RecursiveFind(transform, property.Name);
// if (target != null)
// {
// var component = target.GetComponent(property.PropertyType);
// property.SetValue(this, component);
// }
//}
}
}
catch (Exception ex)
{
Debug.LogError("【UITool Log Error[" + ex.Message + "]】" + ex.StackTrace);
}
}
protected static T GetUI<T>()
{
T res = default;
var t = ui_tools.Find(x => x.GetType().Equals(typeof(T)));
res = (T)Convert.ChangeType(t, typeof(T));
return res;
}
/// <summary>
/// 设置Graphic是否可见
/// </summary>
/// <param name="visible"></param>
/// <param name="gradual"></param>
protected void SetGraphicVisible(bool visible, bool gradual = true)
{
if (!visible)
{
Array.ForEach(m_Graphics, (g) =>
{
g.raycastTarget = false;
//if (gradual)
// DOTween.ToAlpha(() => g.color, x => g.color = x, 0, 0.5f).OnComplete(() => g.enabled = false);
//else
//{
// Color color = g.color;
// color.a = 0;
// g.color = color;
// g.enabled = false;
//}
});
}
else
{
Array.ForEach(m_Graphics, (g) =>
{
g.enabled = true;
//if (gradual)
// DOTween.ToAlpha(() => g.color, x => g.color = x, m_GraphicsColor[g].a, 0.5f).OnComplete(() => g.raycastTarget = true);
//else
//{
// g.color = m_graphicColors[g];
// g.raycastTarget = true;
//}
});
}
}
public static void SetGraphicVisible(RectTransform obj, bool visible, bool gradual = true)
{
UITool tool = obj.GetComponentInParent<UITool>();
if (tool == null) return;
var m_graphics = obj.GetComponentsInChildren<Graphic>();
if (!visible)
{
Array.ForEach(m_graphics, (g) =>
{
g.raycastTarget = false;
//if (gradual)
// DOTween.ToAlpha(() => g.color, x => g.color = x, 0, 0.5f).OnComplete(() => g.enabled = false);
//else
//{
// Color color = g.color;
// color.a = 0;
// g.color = color;
// g.enabled = false;
//}
});
}
else
{
Array.ForEach(m_graphics, (g) =>
{
g.enabled = true;
//if (gradual)
// DOTween.ToAlpha(() => g.color, x => g.color = x, tool.m_GraphicsColor[g].a, 0.5f).OnComplete(() => g.raycastTarget = true);
//else
//{
// g.color = tool.m_graphicColors[g];
// g.raycastTarget = true;
//}
});
}
}
/// <summary>
/// 递归查询
/// </summary>
/// <param name="root"></param>
/// <param name="target"></param>
/// <returns></returns>
Transform RecursiveFind(Transform root, string target)
{
if (root.name == target)
return root;
var targetTra = root.Find(target);
if (targetTra == null)
{
for (int i = 0; i < root.childCount; i++)
{
targetTra = RecursiveFind(root.GetChild(i), target);
if (targetTra != null)
return targetTra;
}
}
return targetTra;
}
}
}