把代码核心逻辑移入热更工程,热更工程生成编译后可以直接Copy dll 和 mdb文件到UnityAsset目录下

把代码核心逻辑移入热更工程,热更工程生成编译后可以直接Copy dll 和 mdb文件到UnityAsset目录下
This commit is contained in:
ALEXTANG
2022-05-23 13:49:07 +08:00
parent 2e7263101a
commit 572b768ce2
82 changed files with 13764 additions and 2 deletions

View File

@@ -0,0 +1,119 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace TEngineCore
{
public enum UIDragType
{
Draging,
Drop
}
public class DragItem : UIEventItem<DragItem>
{
private UIDragType m_DragState = UIDragType.Drop;
private Vector3 m_ItemOldPos;
private Vector3 m_ItemCachePos;
private bool m_CanDrag = false;
/// <summary>
/// 是否可以拖拽
/// </summary>
public bool CanDrag
{
get
{
return m_CanDrag;
}
set
{
m_CanDrag = value;
if (m_CanDrag)
{
BindDrag();
}
}
}
protected override void OnCreate()
{
base.OnCreate();
BindDrag();
}
private void BindDrag()
{
if (m_CanDrag)
{
BindBeginDragEvent(delegate (DragItem item, PointerEventData data)
{
if (!m_CanDrag)
{
return;
}
StartDragItem(UIDragType.Draging);
});
BindEndDragEvent(delegate (DragItem item, PointerEventData data)
{
if (!m_CanDrag)
{
return;
}
EndDrag();
});
}
}
protected override void OnUpdate()
{
if (!m_CanDrag)
{
return;
}
UpdateDragPos();
}
private void StartDragItem(UIDragType type)
{
if (type != UIDragType.Drop)
{
m_ItemOldPos = transform.position;
Vector3 pos;
UISys.Mgr.GetMouseDownUiPos(out pos);
m_ItemCachePos = pos;
UpdateDragPos();
m_DragState = type;
}
}
private void EndDrag()
{
m_DragState = UIDragType.Drop;
transform.position = m_ItemOldPos;
#if UNITY_EDITOR
//Debug.LogError("m_ItemCachePos.y - m_ItemOldPos.y " + (m_ItemCachePos.y - m_ItemOldPos.y));
#endif
if (m_ItemCachePos.y - m_ItemOldPos.y > 3)
{
}
}
private void UpdateDragPos()
{
if (m_DragState == UIDragType.Drop)
{
return;
}
Vector3 pos;
UISys.Mgr.GetMouseDownUiPos(out pos);
transform.position += (pos - m_ItemCachePos);
m_ItemCachePos = pos;
}
}
}

View File

@@ -0,0 +1,18 @@
using UnityEngine.UI;
public class EmptyGraph : Graphic
{
public bool m_debug = false;
protected override void OnPopulateMesh(VertexHelper vbo)
{
vbo.Clear();
#if UNITY_EDITOR
if (m_debug)
{
base.OnPopulateMesh(vbo);
}
#endif
}
}

View File

@@ -0,0 +1,253 @@
using System.Collections;
using System.Collections.Generic;
using TEngineCore;
using UnityEngine;
using UnityEngine.UI;
namespace TEngineCore
{
public class MsgUI : UIWindow
{
protected override void RegisterEvent()
{
base.RegisterEvent();
GameEventMgr.Instance.AddEventListener<string>(TipsEvent.Log, TipsUI.Instance.Show);
}
}
public class TipsEvent
{
public static int Log = StringId.StringToHash("TipsEvent.Log");
}
public class TipsUI : MonoBehaviour
{
//单例
public static TipsUI Instance;
private void Awake()
{
Instance = this;
}
//Prefab
public GameObject Prefab;
public GameObject Mask;
//对象池
public List<GameObject> AvailablePool;
public List<GameObject> UsedPool;
//自定义内容
[SerializeField] private float Speed;
//定时器
private float timer1;
private float timer2;
void Start()
{
AvailablePool = new List<GameObject>();
UsedPool = new List<GameObject>();
for (int i = 0; i < 5; i++)
{
AvailablePool.Add(Instantiate(Prefab, Mask.transform));
AvailablePool[i].SetActive(false);
}
//GameEventMgr.Instance.AddEventListener<string>(TipsEvent.Log, Show);
}
void Update()
{
//如果一定时间内没有新消息,则挨个消失
timer1 += Time.deltaTime;
if (timer1 >= 2)
{
timer2 += Time.deltaTime;
if (timer2 > 1)
{
timer2 = 0;
if (UsedPool.Count > 0)
{
Text Tname = UsedPool[0].transform.Find("Name").GetComponent<Text>();
Text Tmessage = UsedPool[0].transform.Find("Message").GetComponent<Text>();
Image BG = UsedPool[0].GetComponent<Image>();
StartCoroutine(AlphaDown(Tname));
StartCoroutine(AlphaDown(Tmessage));
StartCoroutine(ImageAlphaDown(BG));
}
}
}
}
public void Show(string message)
{
GameObject go;
//使用对象池
if (AvailablePool.Count > 0)
{
go = AvailablePool[0];
AvailablePool.Remove(go);
UsedPool.Add(go);
}
else
{
go = UsedPool[0];
UsedPool.Remove(go);
UsedPool.Add(go);
}
//进行一些初始化设定
go.transform.localPosition = new Vector3(0, -75, 0);
go.SetActive(true);
go.GetComponent<Image>().color = new Color(0, 0, 0, 150f / 255f);
timer1 = timer2 = 0;
Text Tname = go.transform.Find("Name").GetComponent<Text>();
Text Tmessage = go.transform.Find("Message").GetComponent<Text>();
Tname.text = " " + "SYSTEM";
Tmessage.text = " " + message;
float TnameWidth = Tname.preferredWidth;
float TmessageWidth = Tmessage.preferredWidth;
float goSizey = go.transform.GetComponent<RectTransform>().sizeDelta.y;
go.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(TnameWidth + TmessageWidth, goSizey);
Tname.rectTransform.sizeDelta = new Vector2(TnameWidth, Tname.rectTransform.sizeDelta.y);
Tname.rectTransform.anchoredPosition = new Vector2(TnameWidth / 2, Tname.rectTransform.anchoredPosition.y);
Tmessage.rectTransform.sizeDelta = new Vector2(TmessageWidth, Tmessage.rectTransform.sizeDelta.y);
Tmessage.rectTransform.anchoredPosition = new Vector2(-TmessageWidth / 2, Tmessage.rectTransform.anchoredPosition.y);
StartCoroutine(AlphaUP(Tname));
StartCoroutine(AlphaUP(Tmessage));
foreach (GameObject go1 in UsedPool)
{
StartCoroutine(MoveUP(go1));
}
if (UsedPool.Count >= 4)
{
Text Tname2 = UsedPool[UsedPool.Count - 4].transform.Find("Name").GetComponent<Text>();
Text Tmessage2 = UsedPool[UsedPool.Count - 4].transform.Find("Message").GetComponent<Text>();
Image BG = UsedPool[UsedPool.Count - 4].GetComponent<Image>();
StartCoroutine(AlphaDown(Tname2));
StartCoroutine(AlphaDown(Tmessage2));
StartCoroutine(ImageAlphaDown(BG));
}
}
public void Show(string name, string message)
{
GameObject go;
//使用对象池
if (AvailablePool.Count > 0)
{
go = AvailablePool[0];
AvailablePool.Remove(go);
UsedPool.Add(go);
}
else
{
go = UsedPool[0];
UsedPool.Remove(go);
UsedPool.Add(go);
}
//进行一些初始化设定
go.transform.localPosition = new Vector3(0, -75, 0);
go.SetActive(true);
go.GetComponent<Image>().color = new Color(0, 0, 0, 150f / 255f);
timer1 = timer2 = 0;
Text Tname = go.transform.Find("Name").GetComponent<Text>();
Text Tmessage = go.transform.Find("Message").GetComponent<Text>();
Tname.text = " " + name;
Tmessage.text = " " + message;
float TnameWidth = Tname.preferredWidth;
float TmessageWidth = Tmessage.preferredWidth;
float goSizey = go.transform.GetComponent<RectTransform>().sizeDelta.y;
go.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(TnameWidth + TmessageWidth, goSizey);
Tname.rectTransform.sizeDelta = new Vector2(TnameWidth, Tname.rectTransform.sizeDelta.y);
Tname.rectTransform.anchoredPosition = new Vector2(TnameWidth / 2, Tname.rectTransform.anchoredPosition.y);
Tmessage.rectTransform.sizeDelta = new Vector2(TmessageWidth, Tmessage.rectTransform.sizeDelta.y);
Tmessage.rectTransform.anchoredPosition = new Vector2(-TmessageWidth / 2, Tmessage.rectTransform.anchoredPosition.y);
StartCoroutine(AlphaUP(Tname));
StartCoroutine(AlphaUP(Tmessage));
foreach (GameObject go1 in UsedPool)
{
StartCoroutine(MoveUP(go1));
}
if (UsedPool.Count >= 4)
{
Text Tname2 = UsedPool[UsedPool.Count - 4].transform.Find("Name").GetComponent<Text>();
Text Tmessage2 = UsedPool[UsedPool.Count - 4].transform.Find("Message").GetComponent<Text>();
Image BG = UsedPool[UsedPool.Count - 4].GetComponent<Image>();
StartCoroutine(AlphaDown(Tname2));
StartCoroutine(AlphaDown(Tmessage2));
StartCoroutine(ImageAlphaDown(BG));
}
}
//文字透明度提高
public IEnumerator AlphaUP(Text text)
{
text.color += new Color(0, 0, 0, -1);
while (true)
{
yield return new WaitForSeconds(0.01f);
text.color += new Color(0, 0, 0, 0.08f);
if (text.color.a >= 1)
{
yield break;
}
}
}
//向上移动
public IEnumerator MoveUP(GameObject go)
{
float i = 0;
while (true)
{
yield return new WaitForSeconds(0.01f);
if (i + Speed >= 35)
{
go.transform.localPosition += new Vector3(0, 35 - i, 0);
yield break;
}
else
{
go.transform.localPosition += new Vector3(0, Speed, 0);
i += Speed;
}
}
}
//文字透明度下降
public IEnumerator AlphaDown(Text text)
{
while (true)
{
yield return new WaitForSeconds(0.01f);
text.color -= new Color(0, 0, 0, 0.08f);
if (text.color.a <= 0)
{
yield break;
}
}
}
//背景透明度下降
public IEnumerator ImageAlphaDown(Image image)
{
while (true)
{
yield return new WaitForSeconds(0.01f);
image.color -= new Color(0, 0, 0, 0.08f);
if (image.color.a <= 0)
{
image.gameObject.SetActive(false);
UsedPool.Remove(image.gameObject);
AvailablePool.Add(image.gameObject);
yield break;
}
}
}
//三个按钮的测试函数
public void test()
{
Show("[没关系丶是爱情啊(安娜)]", "该睡觉咯。");
}
public void test2()
{
Show("[没关系丶是爱情啊(安娜)]", "妈妈永远是对的。");
}
public void test3()
{
Show("[没关系丶是爱情啊(安娜)]", "天降正义。");
}
}
}

View File

@@ -0,0 +1,112 @@
using TEngineCore;
using UnityEngine;
namespace TEngineCore
{
public enum TweenType
{
Position,
Rotation,
Scale,
Alpha,
}
public class TweenUtil : MonoBehaviour
{
public bool isLocal;
public TweenType type;
public Vector3 from;
public Vector3 to;
public AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 1);
public float duration = 1f;
public bool isLoop;
public bool isPingPong;
private float timer = 0f;
private CanvasGroup canvasGroup;
void Awake()
{
canvasGroup = gameObject.GetComponent<CanvasGroup>();
}
void Update()
{
if (duration > 0)
{
timer += Time.deltaTime;
float curveValue;
if (isLoop)
{
float remainTime = timer % duration;
int loopCount = (int)(timer / duration);
float evaluateTime = remainTime / duration;
if (isPingPong)
{
evaluateTime = loopCount % 2 == 0 ? evaluateTime : 1 - evaluateTime;
}
curveValue = curve.Evaluate(evaluateTime);
}
else
{
curveValue = curve.Evaluate(timer);
}
var lerpValue = Vector3.Lerp(from, to, curveValue);
//if (lerpValue == lastValue)
//{
// return;
//}
//lastValue = lerpValue;
switch (type)
{
case TweenType.Position:
if (isLocal)
{
transform.localPosition = lerpValue;
}
else
{
transform.position = lerpValue;
}
break;
case TweenType.Rotation:
if (isLocal)
{
transform.localEulerAngles = lerpValue;
}
else
{
transform.eulerAngles = lerpValue;
}
break;
case TweenType.Scale:
if (isLocal)
{
transform.localScale = lerpValue;
}
else
{
var value1 = VectorWiseDivision(transform.lossyScale, transform.localScale);
var value2 = VectorWiseDivision(lerpValue, value1);
transform.localScale = value2;
}
break;
case TweenType.Alpha:
if (canvasGroup != null)
{
canvasGroup.alpha = lerpValue.x;
}
else
{
TLogger.LogError("Change Alpha need Component: [CanvasGroup]");
}
break;
}
}
}
Vector3 VectorWiseDivision(Vector3 a, Vector3 b)
{
return new Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
}
}
}

View File

@@ -0,0 +1,200 @@
using System;
using TEngineCore;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace TEngineCore
{
public class UIEventItem<T> : UIWindowWidget where T : UIEventItem<T>
{
protected Button m_buttonClick;
private object[] m_eventParam;
public object EventParam1
{
get
{
return m_eventParam != null && m_eventParam.Length > 0 ? m_eventParam[0] : null;
}
}
public object EventParam2
{
get
{
return m_eventParam != null && m_eventParam.Length > 1 ? m_eventParam[1] : null;
}
}
public object EventParam3
{
get { return m_eventParam != null && m_eventParam.Length > 2 ? m_eventParam[2] : null; }
}
public object this[int index]
{
get { return m_eventParam != null && index < m_eventParam.Length ? m_eventParam[index] : null; }
}
private Action<T> m_clickAction;
private Action<T, bool, PointerEventData> m_pressAction;
private Action<T, PointerEventData> m_beginDragAction;
private Action<T, PointerEventData> m_dragAction;
private Action<T, PointerEventData> m_endDragAction;
public void BindClickEvent(Action<T> clickAction, params object[] arg)
{
if (m_clickAction != null)
{
m_clickAction = clickAction;
}
else
{
m_clickAction = clickAction;
m_buttonClick = UnityUtil.AddMonoBehaviour<Button>(gameObject);
m_buttonClick.transition = Selectable.Transition.None;
m_buttonClick.onClick.AddListener(OnButtonClick);
}
SetEventParam(arg);
}
private void OnButtonClick()
{
if (m_clickAction != null)
{
//AudioSys.GameMgr.PlayUISoundEffect(UiSoundType.UI_SOUND_CLICK);
m_clickAction(this as T);
}
}
public void BindBeginDragEvent(Action<T, PointerEventData> dragAction, params object[] arg)
{
if (m_beginDragAction != null)
{
m_beginDragAction = dragAction;
}
else
{
m_beginDragAction = dragAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.BeginDrag;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerBeginDrag);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerBeginDrag(BaseEventData data)
{
var pointerEventData = (PointerEventData)data;
if (m_beginDragAction != null)
{
m_beginDragAction(this as T, pointerEventData);
}
}
public void BindDragEvent(Action<T, PointerEventData> dragAction, params object[] arg)
{
if (m_dragAction != null)
{
m_dragAction = dragAction;
}
else
{
m_dragAction = dragAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerDrag);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerDrag(BaseEventData data)
{
var pointerEventData = (PointerEventData)data;
if (m_dragAction != null)
{
m_dragAction(this as T, pointerEventData);
}
}
public void BindEndDragEvent(Action<T, PointerEventData> dragendAction, params object[] arg)
{
if (m_endDragAction != null)
{
m_endDragAction = dragendAction;
}
else
{
m_endDragAction = dragendAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.EndDrag;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerEndDrag);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerEndDrag(BaseEventData data)
{
if (m_endDragAction != null)
{
m_endDragAction(this as T, (PointerEventData)data);
}
}
public void BindPressEvent(Action<T, bool, PointerEventData> pressAction, params object[] arg)
{
if (m_pressAction != null)
{
m_pressAction = pressAction;
}
else
{
m_pressAction = pressAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerPointerDown);
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerPointerUp);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerPointerUp(BaseEventData data)
{
if (m_pressAction != null)
{
m_pressAction(this as T, false, data as PointerEventData);
}
}
private void OnTriggerPointerDown(BaseEventData data)
{
if (m_pressAction != null)
{
m_pressAction(this as T, true, data as PointerEventData);
}
}
public void SetEventParam(params object[] arg)
{
m_eventParam = arg;
}
}
}

View File

@@ -0,0 +1,178 @@
using TEngineCore;
using UnityEngine;
namespace TEngineCore
{
/// <summary>
/// 用来封装各个界面里子模块用
/// </summary>
public class UIWindowWidget : UIWindowBase
{
public int SortingOrder
{
get
{
if (m_canvas != null)
{
return m_canvas.sortingOrder;
}
return 0;
}
set
{
if (m_canvas != null)
{
int oldOrder = m_canvas.sortingOrder;
if (oldOrder != value)
{
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (int i = 0; i < listCanvas.Length; i++)
{
var childCanvas = listCanvas[i];
childCanvas.sortingOrder = value + (childCanvas.sortingOrder - oldOrder);
}
m_canvas.sortingOrder = value;
_OnSortingOrderChg();
}
}
}
}
/// <summary>
/// 所属的窗口
/// </summary>
public UIWindow OwnerWindow
{
get
{
var parent = m_parent;
while (parent != null)
{
if (parent.BaseType == UIWindowBaseType.Window)
{
return parent as UIWindow;
}
parent = parent.Parent;
}
return null;
}
}
public override UIWindowBaseType BaseType
{
get { return UIWindowBaseType.Widget; }
}
/// <summary> 根据类型创建 </summary>
public bool CreateByType<T>(UIWindowBase parent, Transform parentTrans = null) where T : UIWindowWidget
{
string resPath = string.Format("UI/{0}.prefab", typeof(T).Name);
return CreateByPath(resPath, parent, parentTrans);
}
/// <summary> 根据资源名创建 </summary>
public bool CreateByPath(string resPath, UIWindowBase parent, Transform parentTrans = null, bool visible = true)
{
GameObject goInst = TResources.Load(resPath, parentTrans);
if (goInst == null)
{
return false;
}
if (!Create(parent, goInst, visible))
{
return false;
}
goInst.transform.localScale = Vector3.one;
goInst.transform.localPosition = Vector3.zero;
return true;
}
/**
* 根据prefab或者模版来创建新的 widget
*/
public bool CreateByPrefab(UIWindowBase parent, GameObject goPrefab, Transform parentTrans, bool visible = true)
{
if (parentTrans == null)
{
parentTrans = parent.transform;
}
var widgetRoot = GameObject.Instantiate(goPrefab, parentTrans);
return CreateImp(parent, widgetRoot, true, visible);
}
/**
* 创建窗口内嵌的界面
*/
public bool Create(UIWindowBase parent, GameObject widgetRoot, bool visible = true)
{
return CreateImp(parent, widgetRoot, false, visible);
}
#region
private bool CreateImp(UIWindowBase parent, GameObject widgetRoot, bool bindGo, bool visible = true)
{
if (!CreateBase(widgetRoot, bindGo))
{
return false;
}
RestChildCanvas(parent);
m_parent = parent;
if (m_parent != null)
{
m_parent.AddChild(this);
}
if (m_canvas != null)
{
m_canvas.overrideSorting = true;
}
ScriptGenerator();
BindMemberProperty();
RegisterEvent();
OnCreate();
if (visible)
{
Show(true);
}
else
{
widgetRoot.SetActive(false);
}
return true;
}
private void RestChildCanvas(UIWindowBase parent)
{
if (gameObject == null)
{
return;
}
if (parent == null || parent.gameObject == null)
{
return;
}
Canvas parentCanvas = parent.gameObject.GetComponentInParent<Canvas>();
if (parentCanvas == null)
{
return;
}
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (var index = 0; index < listCanvas.Length; index++)
{
var childCanvas = listCanvas[index];
childCanvas.sortingOrder = parentCanvas.sortingOrder + childCanvas.sortingOrder % UIWindow.MaxCanvasSortingOrder;
}
}
#endregion
}
}

View File

@@ -0,0 +1,7 @@
namespace TEngineCore
{
interface IUIController
{
void ResigterUIEvent();
}
}

View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using TEngineCore;
using UnityEngine;
namespace TEngineCore
{
public class UIBase
{
protected GameObject m_go;
protected RectTransform m_transform;
protected string m_name;
protected bool m_destroyed = true;
private GameEventMgr m_eventMgr;
protected GameEventMgr EventMgr
{
get
{
if (m_eventMgr == null)
{
m_eventMgr = GameEventMgr.Instance;
}
return m_eventMgr;
}
}
public bool IsDestroyed
{
get { return m_destroyed; }
}
public bool IsCreated
{
get { return !IsDestroyed; }
}
public RectTransform transform
{
get { return m_transform; }
}
public GameObject gameObject
{
get { return m_go; }
}
public string name
{
get
{
if (string.IsNullOrEmpty(m_name))
{
m_name = GetType().Name;
}
return m_name;
}
}
#region Event
private Dictionary<int, Delegate> m_eventTable = new Dictionary<int, Delegate>();
protected void ClearAllRegisterEvent()
{
var element = m_eventTable.GetEnumerator();
while (element.MoveNext())
{
var m_event = element.Current.Value;
//GameEventMgr.Instance.RemoveEventListener(element.Current.Key, m_event);
}
m_eventTable.Clear();
}
protected void AddUIEvent(int eventType, Action handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
}
protected void AddUIEvent<T>(int eventType, Action<T> handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
}
protected void AddUIEvent<T, U>(int eventType, Action<T, U> handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
}
protected void AddUIEvent<T, U, V>(int eventType, Action<T, U, V> handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
}
#endregion
}
}

View File

@@ -0,0 +1,546 @@
using System.Collections.Generic;
using TEngineCore;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace TEngineCore
{
public enum WindowStackIndex
{
StackNormal = 0,
StackTop = 1,
StackMax
};
public class UIWindowStack
{
public WindowStackIndex m_stackIndex;
public int m_baseOrder = 0;
public List<uint> m_windowList = new List<uint>();
public Transform m_parentTrans;
public int FindIndex(uint windowId)
{
for (int i = 0; i < m_windowList.Count; i++)
{
if (m_windowList[i] == windowId)
{
return i;
}
}
return -1;
}
}
public sealed partial class UIManager : TSingleton<UIManager>
{
private Transform m_canvasTrans;
public Canvas m_canvas;
private Dictionary<uint, UIWindow> m_allWindow = new Dictionary<uint, UIWindow>();
/// <summary>
/// 不同显示顺序的的窗口列表
/// </summary>
private UIWindowStack[] m_listWindowStack = new UIWindowStack[(int)WindowStackIndex.StackMax];
/// <summary>
/// 类型到实例的索引
/// </summary>
private static Dictionary<string, UIWindow> m_typeToInst = new Dictionary<string, UIWindow>();
/// <summary>
/// 类型和资源的绑定关系
/// </summary>
private Dictionary<string, string> m_uiType2PrefabPath = new Dictionary<string, string>();
private GameObject m_uiManagerGo;
private Transform m_uiManagerTransform;
private Camera m_uiCamera;
public Camera Camera => m_uiCamera;
public UIManager()
{
m_uiManagerGo = TResources.Load("UI/UIRoot.prefab");
Object.DontDestroyOnLoad(m_uiManagerGo);
m_uiManagerTransform = m_uiManagerGo.transform;
m_uiCamera = UnityUtil.FindChildComponent<Camera>(m_uiManagerTransform, "Camera");
Canvas canvas = m_uiManagerGo.GetComponentInChildren<Canvas>();
if (canvas != null)
{
m_canvas = canvas;
m_canvasTrans = canvas.transform;
}
var windowRoot = m_canvasTrans;
int baseOrder = 1000;
for (int i = 0; i < (int)WindowStackIndex.StackMax; i++)
{
m_listWindowStack[i] = new UIWindowStack();
m_listWindowStack[i].m_stackIndex = (WindowStackIndex)i;
m_listWindowStack[i].m_baseOrder = baseOrder;
m_listWindowStack[i].m_parentTrans = windowRoot;
baseOrder += 1000;
}
CalcCameraRect();
}
void CalcCameraRect()
{
CanvasScaler canvasScale = m_canvas.GetComponent<CanvasScaler>();
if (canvasScale != null)
{
canvasScale.referenceResolution = new Vector2(UISys.DesginWidth, UISys.DesginHeight);
float sceneScale = Screen.width / (float)Screen.height;
float designScale = canvasScale.referenceResolution.x / canvasScale.referenceResolution.y;
canvasScale.matchWidthOrHeight = sceneScale > designScale ? 1 : 0;
}
}
public void Update()
{
var allList = GetAllWindowList();
for (int i = 0; i < allList.Count; i++)
{
UIWindow window = allList[i];
if (!window.IsDestroyed)
{
window.Update();
}
}
}
private List<UIWindow> m_tmpWindowList = new List<UIWindow>();
private bool m_tmpWindowListDirty = true;
private List<UIWindow> GetAllWindowList()
{
if (m_tmpWindowListDirty)
{
m_tmpWindowList.Clear();
var itr = m_allWindow.GetEnumerator();
while (itr.MoveNext())
{
var kv = itr.Current;
m_tmpWindowList.Add(kv.Value);
}
m_tmpWindowListDirty = false;
}
return m_tmpWindowList;
}
#region Methods
public T ShowWindow<T>(bool isAsync = false) where T : UIWindow, new()
{
string typeName = GetWindowTypeName<T>();
T window = GetUIWindowByType(typeName) as T;
if (window == null)
{
window = new T();
if (!CreateWindowByType(window, typeName, isAsync))
{
return null;
}
}
ShowWindow(window, -1);
return window;
}
public string GetWindowTypeName<T>()
{
string typeName = typeof(T).Name;
return typeName;
}
public string GetWindowTypeName(UIWindow window)
{
string typeName = window.GetType().Name;
return typeName;
}
public void CloseWindow<T>() where T : UIWindow
{
string typeName = GetWindowTypeName<T>();
CloseWindow(typeName);
}
public T GetWindow<T>() where T : UIWindow
{
string typeName = GetWindowTypeName<T>();
UIWindow window = GetUIWindowByType(typeName);
if (window != null)
{
return window as T;
}
return null;
}
public UIWindow GetUIWindowByType(string typeName)
{
UIWindow window;
if (m_typeToInst.TryGetValue(typeName, out window))
{
return window;
}
return null;
}
#endregion
private bool CreateWindowByType(UIWindow window, string typeName,bool async = false)
{
//先判断是否有缓存
GameObject uiGo = null;
string resPath = string.Format("{0}.prefab", GetUIResourcePath(typeName));
if (string.IsNullOrEmpty(resPath))
{
Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, cant find respath", typeName);
return false;
}
UIWindowStack windowStack = GetUIWindowStack(window);
if (async)
{
TResources.LoadAsync(resPath, (obj) =>
{
if (obj == null)
{
Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", typeName, resPath);
}
if (obj != null && windowStack.m_parentTrans != null)
{
obj.transform.SetParent(windowStack.m_parentTrans);
}
obj.name = typeName;
window.AllocWindowId();
var rectTrans_ = obj.transform as RectTransform;
if (window.NeedCenterUI())
{
rectTrans_.SetMax(); //localPosition = new Vector3(0, 0, 0);
}
rectTrans_.localRotation = Quaternion.identity;
rectTrans_.localScale = Vector3.one;
if (!window.Create(this, obj))
{
Debug.LogErrorFormat("window create failed, typeName:{0}", typeName);
if (obj != null)
{
Object.Destroy(obj);
obj = null;
}
}
m_typeToInst[typeName] = window;
m_allWindow[window.WindowId] = window;
m_tmpWindowListDirty = true;
});
return true;
}
uiGo = TResources.Load(resPath, windowStack.m_parentTrans);
if (uiGo == null)
{
Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", typeName, resPath);
//UISys.Mgr.ShowTipMsg(TextDefine.DOWNLOAD_TIP_UI);
//GameEvent.Get<IHomePageUI>().ShowDownloadUI();
return false;
}
uiGo.name = typeName;
window.AllocWindowId();
RectTransform rectTrans = uiGo.transform as RectTransform;
if (window.NeedCenterUI())
{
rectTrans.SetMax(); //localPosition = new Vector3(0, 0, 0);
}
rectTrans.localRotation = Quaternion.identity;
rectTrans.localScale = Vector3.one;
if (!window.Create(this, uiGo))
{
Debug.LogErrorFormat("window create failed, typeName:{0}", typeName);
if (uiGo != null)
{
Object.Destroy(uiGo);
uiGo = null;
}
return false;
}
m_typeToInst[typeName] = window;
m_allWindow[window.WindowId] = window;
m_tmpWindowListDirty = true;
return true;
}
#region MyRegion
private string GetUIResourcePath(string typeName)
{
string resPath;
if (m_uiType2PrefabPath.TryGetValue(typeName, out resPath))
{
return resPath;
}
string path = string.Format("UI/{0}", typeName);
m_uiType2PrefabPath.Add(typeName, path);
return path;
}
private void ShowWindow(UIWindow window, int showIndex)
{
UIWindowStack windowStack = GetUIWindowStack(window);
List<uint> windowList = windowStack.m_windowList;
int resortIndex = -1;
int findIndex = windowList.IndexOf(window.WindowId);
if (findIndex >= 0)
{
windowList.RemoveAt(findIndex);
resortIndex = findIndex;
}
windowList.Add(window.WindowId);
ResortStackUI(windowStack, resortIndex);
ShowTopUI(windowStack);
}
private void ResortStackUI(UIWindowStack stack, int startIdx)
{
if (stack.m_windowList.Count > 0)
{
startIdx = startIdx < 0 ? (stack.m_windowList.Count - 1) : startIdx;
for (int i = startIdx; i < stack.m_windowList.Count; i++)
{
uint windowId = stack.m_windowList[i];
UIWindow window = FindWindow(windowId);
if (window != null)
{
int order;
if (window.IsFixedSortingOrder)
{
order = stack.m_baseOrder + window.FixedAdditionalOrder;
}
else
{
order = stack.m_baseOrder + i * UIWindow.MaxCanvasSortingOrder;
}
window.SortingOrder = order;
}
}
}
}
private void ShowTopUI(UIWindowStack stack)
{
if (stack.m_windowList.Count > 0)
{
bool hasTop = false;
for (int i = stack.m_windowList.Count - 1; i >= 0; i--)
{
uint windowId = stack.m_windowList[i];
UIWindow window = FindWindow(windowId);
if (window != null)
{
if (!hasTop)
{
hasTop = window.IsFullScreen;
window.Show(true);
}
else
{
window.Show(false);
}
}
}
}
OnWindowVisibleChanged();
}
private void OnWindowVisibleChanged()
{
bool isFullScreenMaskscene = false;
for (int i = 0; i < m_listWindowStack.Length && !isFullScreenMaskscene; i++)
{
var stack = m_listWindowStack[i];
if (stack == null)
{
continue;
}
var listWindow = stack.m_windowList;
for (int k = 0; k < listWindow.Count; k++)
{
var winId = listWindow[k];
var win = FindWindow(winId);
if (win == null || !win.Visible)
{
continue;
}
if (win.IsFullScreenMaskScene)
{
isFullScreenMaskscene = true;
break;
}
}
}
//SceneSys.Instance.CameraMgr.SetSceneCameraEnableByUI(true);
}
public UIWindow FindWindow(uint windowId)
{
UIWindow window;
if (m_allWindow.TryGetValue(windowId, out window))
{
return window;
}
return null;
}
public void CloseWindow(string typeName)
{
UIWindow window = GetUIWindowByType(typeName);
if (window != null)
{
CloseWindow(window);
}
}
public void CloseWindow(UIWindow window)
{
if (window.IsDestroyed)
{
return;
}
//刷新窗口order保证新创建的窗口不会出现重叠
UIWindowStack windowStack = GetUIWindowStack(window);
int findIndex = windowStack.FindIndex(window.WindowId);
//window.Destroy();
DestroyWindowObject(window);
ResortStackUI(windowStack, findIndex);
ShowTopUI(windowStack);
}
private void DestroyWindowObject(UIWindow window)
{
string typeName = window.GetType().Name;
UIWindow typeWindow = null;
if (m_typeToInst.TryGetValue(typeName, out typeWindow) && typeWindow == window)
{
m_typeToInst.Remove(typeName);
}
uint windowId = window.WindowId;
m_allWindow.Remove(windowId);
UIWindowStack windowStack = GetUIWindowStack(window);
windowStack.m_windowList.Remove(windowId);
window.Destroy();
m_tmpWindowListDirty = true;
}
private int GetIndexByWindowType(UIWindowType windowType)
{
if (windowType == UIWindowType.WindowTop)
{
return (int)WindowStackIndex.StackTop;
}
return (int)WindowStackIndex.StackNormal;
}
public UIWindowStack GetUIWindowStack(UIWindow window)
{
int index = GetIndexByWindowType(window.GetWindowType());
return m_listWindowStack[index];
}
public UIWindow GetWindowById(uint windowId)
{
return FindWindow(windowId);
}
public UIWindowStack GetUIWindowStack(UIWindowType windowType)
{
int index = GetIndexByWindowType(windowType);
return m_listWindowStack[index];
}
#endregion
#region
/// <summary>
/// 给控件添加自定义事件监听
/// </summary>
/// <param name="control">控件对象</param>
/// <param name="type">事件类型</param>
/// <param name="callback">事件的响应函数</param>
public static void AddCustomEventListener(UIBehaviour control, EventTriggerType type, UnityAction<BaseEventData> callback)
{
EventTrigger trigger = control.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = control.gameObject.AddComponent<EventTrigger>();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = type;
entry.callback.AddListener(callback);
trigger.triggers.Add(entry);
}
public bool GetMouseDownUiPos(out Vector3 screenPos)
{
bool hadMouseDown = false;
Vector3 mousePos = Vector3.zero;
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
mousePos = Input.mousePosition;
hadMouseDown = Input.GetMouseButton(0);
#else
if (Input.touchCount > 0)
{
mousePos = Input.GetTouch(0).position;
hadMouseDown = true;
}
else
{
hadMouseDown = false;
}
#endif
Vector2 pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_canvasTrans as RectTransform, Input.mousePosition,
m_uiCamera, out pos);
screenPos = m_canvasTrans.TransformPoint(pos);
return hadMouseDown;
}
#endregion
}
}

View File

@@ -0,0 +1,166 @@
using System.Collections.Generic;
using TEngineCore;
using UnityEngine;
namespace TEngineCore
{
public class UISys : BaseLogicSys<UISys>
{
public static int DesginWidth
{
get
{
return 750;
}
}
public static int DesginHeight
{
get
{
return 1624;
}
}
public static int ScreenWidth;
public static int ScreenHeight;
public bool IsLandScape { private set; get; }
private List<IUIController> m_listController = new List<IUIController>();
public static UIManager Mgr
{
get { return UIManager.Instance; }
}
public override void OnUpdate()
{
UIManager.Instance.Update();
}
public override bool OnInit()
{
base.OnInit();
ScreenWidth = Screen.width;
ScreenHeight = Screen.height;
IsLandScape = ScreenWidth > ScreenHeight;
RegistAllController();
return true;
}
private void RegistAllController()
{
//AddController<LoadingUIController>();
}
private void AddController<T>() where T : IUIController, new()
{
for (int i = 0; i < m_listController.Count; i++)
{
var type = m_listController[i].GetType();
if (type == typeof(T))
{
Debug.LogError(string.Format("repeat controller type: {0}", typeof(T).Name));
return;
}
}
var controller = new T();
m_listController.Add(controller);
controller.ResigterUIEvent();
}
public static void ShowTipMsg(string str)
{
}
}
public static class CanvasUtils
{
public static void SetMax(this RectTransform rectTransform)
{
if (rectTransform == null)
{
return;
}
rectTransform.localPosition = new Vector3(0, 0, 0);
rectTransform.localRotation = Quaternion.identity;
rectTransform.localScale = Vector3.one;
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
rectTransform.anchorMin = Vector2.zero;
rectTransform.anchorMax = Vector2.one;
}
/// <summary>
/// 调整 RectTransform 组件中的 Left、Bottom 属性
/// </summary>
/// <param name="rt">引用目标 RectTransform 对象</param>
/// <param name="left">Left值</param>
/// <param name="bottom">Bottom值</param>
public static void LeftBottom(RectTransform rectTransform, float left, float bottom)
{
Vector2 size = rectTransform.rect.size;
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, left, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, bottom, size.y);
}
/// <summary>
/// 调整 RectTransform 组件中的 Left、Top 属性
/// </summary>
/// <param name="rt"></param>
/// <param name="left">Left值</param>
/// <param name="top">Top值</param>
public static void LeftTop(RectTransform rectTransform, float left, float top)
{
Vector2 size = rectTransform.rect.size;
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, left, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, top, size.y);
}
/// <summary>
/// 调整 RectTransform 组件中的 Right、Bottom 属性
/// </summary>
/// <param name="rt"></param>
/// <param name="right">Right值</param>
/// <param name="bottom">Bottom值</param>
public static void RightBottom(RectTransform rectTransform, float right, float bottom)
{
Vector2 size = rectTransform.rect.size;
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, right, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, bottom, size.y);
}
/// <summary>
/// 调整 RectTransform 组件中的 Right、Top 属性
/// </summary>
/// <param name="rt"></param>
/// <param name="right">Right值</param>
/// <param name="top">Top值</param>
public static void RightTop(RectTransform rectTransform, float right, float top)
{
Vector2 size = rectTransform.rect.size;
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, right, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, top, size.y);
}
public static void SetCenter(this RectTransform rectTransform, float x = 0, float y = 0)
{
rectTransform.localPosition = new Vector3(0, 0, 0);
rectTransform.localRotation = Quaternion.identity;
rectTransform.localScale = Vector3.one;
Vector2 size = rectTransform.rect.size;
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
rectTransform.localPosition = new Vector2(x, y);
}
}
}

View File

@@ -0,0 +1,280 @@
using TEngineCore;
using UnityEngine;
using UnityEngine.UI;
namespace TEngineCore
{
public enum UIWindowType
{
/// <summary> 普通窗口 </summary>
WindowNormal,
/// <summary> 置顶窗口 </summary>
WindowTop,
/// <summary> 模态窗口 </summary>
WindowModel
}
public class UIWindow : UIWindowBase
{
#region
private bool m_isClosed = false;
private bool m_isCreating = false;
private Image m_modalImage;
private float m_modalAlpha = 0.86f;
/// <summary>
/// 是否固定SortingOrder
/// </summary>
public virtual bool IsFixedSortingOrder
{
get { return false; }
}
public virtual bool NeedCenterUI()
{
return true;
}
/// <summary>
/// 窗口Id
/// </summary>
private uint m_windowId = 0;
/// <summary>
/// 窗口Id
/// </summary>
public uint WindowId
{
get { return m_windowId; }
}
private static uint m_nextWindowId = 0;
public virtual bool IsFullScreen
{
get { return false; }
}
public virtual bool IsFullScreenMaskScene
{
get { return false; }
}
/// <summary>
/// 一个界面中最大sortOrder值
/// </summary>
public const int MaxCanvasSortingOrder = 50;
/// <summary>
/// SortingOrder = stack.m_baseOrder + FixedAdditionalOrder
/// </summary>
public virtual int FixedAdditionalOrder
{
get { return UIWindow.MaxCanvasSortingOrder; }
}
public int SortingOrder
{
get
{
if (m_canvas != null)
{
return m_canvas.sortingOrder;
}
return 0;
}
set
{
if (m_canvas != null)
{
int oldOrder = m_canvas.sortingOrder;
if (oldOrder != value)
{
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (int i = 0; i < listCanvas.Length; i++)
{
var childCanvas = listCanvas[i];
childCanvas.sortingOrder = value + (childCanvas.sortingOrder - oldOrder);
}
m_canvas.sortingOrder = value;
_OnSortingOrderChg();
}
}
}
}
#endregion
public void AllocWindowId()
{
if (m_nextWindowId == 0)
{
m_nextWindowId++;
}
m_windowId = m_nextWindowId++;
}
#region virtual function
public virtual UIWindowType GetWindowType()
{
return UIWindowType.WindowNormal;
}
#endregion
#region Call From UIManager
public bool Create(UIManager uiMgr, GameObject uiGo)
{
if (IsCreated)
{
return true;
}
m_isClosed = false;
if (!CreateBase(uiGo, true))
{
return false;
}
if (m_canvas == null)
{
Debug.LogErrorFormat("{0} have not a canvas!!", this.ToString());
Destroy();
return false;
}
m_ownUIManager = uiMgr;
m_firstVisible = false;
if (m_canvas != null)
{
m_canvas.overrideSorting = true;
}
ScriptGenerator();
BindMemberProperty();
RegisterEvent();
m_isCreating = true;
OnCreate();
m_isCreating = false;
if (m_isClosed)
{
Destroy();
return false;
}
SetModalState(GetModalType());
return true;
}
public virtual ModalType GetModalType()
{
if (IsFullScreen || GetWindowType() == UIWindowType.WindowTop)
{
return ModalType.TransparentType;
}
return ModalType.NormalType;
}
private void SetModalState(ModalType type)
{
var canClose = false;
switch (type)
{
case ModalType.NormalType:
{
m_modalAlpha = 0f;
break;
}
case ModalType.NormalHaveClose:
{
canClose = true;
break;
}
case ModalType.TransparentType:
{
m_modalAlpha = 0.01f;
break;
}
case ModalType.TransparentHaveClose:
{
m_modalAlpha = 0.01f;
canClose = true;
break;
}
default:
{
m_modalAlpha = 0f;
break;
}
}
if (m_modalAlpha > 0)
{
string path = "UI/ModalSprite.prefab";
GameObject modal = TResources.Load(path);
modal.transform.SetParent(transform);
modal.transform.SetAsFirstSibling();
modal.transform.localScale = Vector3.one;
modal.transform.localPosition = Vector3.zero;
if (canClose)
{
var button = UnityUtil.AddMonoBehaviour<Button>(modal);
button.onClick.AddListener(Close);
}
m_modalImage = UnityUtil.AddMonoBehaviour<Image>(modal);
m_modalImage.color = new Color(0, 0, 0, m_modalAlpha);
}
}
public virtual void Close()
{
if (m_isCreating)
{
m_isClosed = true;
return;
}
var mgr = UIMgr;
if (mgr != null)
{
mgr.CloseWindow(this);
}
}
#endregion
}
public enum ModalType
{
/// <summary> 普通模态 </summary>
NormalType,
/// <summary> 透明模态 </summary>
TransparentType,
/// <summary> 普通状态且有关闭功能 </summary>
NormalHaveClose,
/// <summary> 透明状态且有关闭功能 </summary>
TransparentHaveClose,
/// <summary> 非模态 </summary>
NoneType,
}
public enum UIWindowBaseType
{
None,
Window,
Widget,
}
}

View File

@@ -0,0 +1,445 @@
using System.Collections;
using System.Collections.Generic;
using TEngineCore;
using UnityEngine;
namespace TEngineCore
{
public class UIWindowBase : UIBase
{
/// <summary>
/// 所属的window
/// </summary>
protected UIWindowBase m_parent = null;
protected Canvas m_canvas;
private List<UIWindowBase> m_listChild = null;
private List<UIWindowBase> m_listUpdateChild = null;
private bool m_updateListValid = false;
/// <summary>
/// 是否首次显示过了
/// </summary>
protected bool m_firstVisible = false;
/// <summary>
/// 当前是否显示出来了
/// </summary>
private bool m_visible = false;
public bool Visible
{
get { return m_visible; }
}
public UIWindowBase Parent
{
get { return m_parent; }
}
protected UIManager m_ownUIManager = null;
protected UIManager UIMgr
{
get
{
if (m_ownUIManager == null && m_parent != null)
{
return m_parent.UIMgr;
}
return m_ownUIManager;
}
}
public virtual UIWindowBaseType BaseType
{
get { return UIWindowBaseType.None; }
}
#region
/**
* 创建对象
*
* bindGO 是否把GameObject和Window绑定在一起
*/
protected bool CreateBase(GameObject go, bool bindGo)
{
///has created
if (!m_destroyed)
{
Debug.LogErrorFormat("UIBase has created: {0}", go.name);
return false;
}
if (go == null)
{
return false;
}
m_destroyed = false;
m_go = go;
m_transform = go.GetComponent<RectTransform>();
m_canvas = gameObject.GetComponent<Canvas>();
var canvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (var i = 0; i < canvas.Length; i++)
{
var canva = canvas[i];
canva.additionalShaderChannels = AdditionalCanvasShaderChannels.TexCoord1;
}
if (m_transform == null)
{
Debug.LogErrorFormat("{0} ui base element need to be RectTransform", go.name);
}
return true;
}
protected void DestroyAllChild()
{
//销毁子对象
if (m_listChild != null)
{
for (int i = 0; i < m_listChild.Count; i++)
{
var child = m_listChild[i];
child.Destroy();
}
m_listChild.Clear();
}
}
public void Destroy()
{
if (IsDestroyed)
{
return;
}
m_destroyed = true;
OnDestroy();
DestroyAllChild();
if (m_parent != null)
{
m_parent.RmvChild(this);
m_parent = null;
}
if (m_go != null)
{
Object.Destroy(m_go);
m_go = null;
}
m_transform = null;
}
#endregion
#region
/// <summary> 脚本生成的代码 </summary>
protected virtual void ScriptGenerator()
{
}
/// <summary>
/// 绑定代码和prefab之间元素的关系
/// </summary>
protected virtual void BindMemberProperty()
{
}
protected virtual void RegisterEvent()
{
}
private bool m_hasOverrideUpdate = true;
protected virtual void OnUpdate()
{
m_hasOverrideUpdate = false;
}
/// <summary>
/// 界面创建出来的时候调用,被覆盖不可见不会重复触发
/// </summary>
protected virtual void OnCreate()
{
}
protected virtual void OnDestroy()
{
}
/// <summary>
/// 创建出来首次visible
/// 用来播放一些显示动画之类的
/// </summary>
protected virtual void OnFirstVisible()
{
}
/// <summary>
/// 当显示出来的时候调用
/// 包括首次初始化后显示和上面的界面消失后重新恢复显示
/// </summary>
protected virtual void OnVisible()
{
}
/// <summary>
/// 界面不可见的时候调用
/// 当被上层全屏界面覆盖后,也会触发一次隐藏
/// </summary>
protected virtual void OnHidden()
{
}
protected void _OnSortingOrderChg()
{
if (m_listChild != null)
{
for (int i = 0; i < m_listChild.Count; i++)
{
if (m_listChild[i].m_visible)
{
m_listChild[i]._OnSortingOrderChg();
}
}
}
OnSortingOrderChg();
}
protected virtual void OnSortingOrderChg()
{
}
#endregion
public void AddChild(UIWindowBase child)
{
if (m_listChild == null)
{
m_listChild = new List<UIWindowBase>();
}
m_listChild.Add(child);
MarkListChanged();
}
public void RmvChild(UIWindowBase child)
{
//如果已经销毁了或者销毁过程中,那么不掉用删除
if (m_destroyed)
{
return;
}
if (m_listChild != null)
{
if (m_listChild.Remove(child))
{
MarkListChanged();
}
}
}
/// <summary>
/// 重新整理update和lateupdate的调用缓存
/// </summary>
private void MarkListChanged()
{
m_updateListValid = false;
if (m_parent != null)
{
m_parent.MarkListChanged();
}
}
#region
protected Coroutine StartCoroutine(IEnumerator routine)
{
return MonoManager.Instance.StartCoroutine(routine);
}
protected void StopCoroutine(Coroutine cort)
{
MonoManager.Instance.StopCoroutine(cort);
}
#endregion
#region
public Transform FindChild(string path)
{
return UnityUtil.FindChild(transform, path);
}
public Transform FindChild(Transform _transform, string path)
{
return UnityUtil.FindChild(_transform, path);
}
public T FindChildComponent<T>(string path) where T : Component
{
return UnityUtil.FindChildComponent<T>(transform, path);
}
public T FindChildComponent<T>(Transform _transform, string path) where T : Component
{
return UnityUtil.FindChildComponent<T>(_transform, path);
}
public void Show(bool visible)
{
// 加个保护
if (m_destroyed || gameObject == null)
{
return;
}
if (m_visible != visible)
{
m_visible = visible;
if (visible)
{
gameObject.SetActive(true);
_OnVisible();
}
else
{
_OnHidden();
if (gameObject == null)
{
Debug.LogErrorFormat("ui bug, hiden destory gameobject: {0}", name);
}
else
{
gameObject.SetActive(false);
}
}
MarkListChanged();
}
}
protected void _OnVisible()
{
if (m_listChild != null)
{
for (int i = 0; i < m_listChild.Count; i++)
{
var child = m_listChild[i];
if (child.gameObject.activeInHierarchy)
{
child._OnVisible();
}
}
}
if (!m_firstVisible)
{
m_firstVisible = true;
OnFirstVisible();
}
OnVisible();
}
protected void _OnHidden()
{
if (m_listChild != null)
{
for (int i = 0; i < m_listChild.Count; i++)
{
var child = m_listChild[i];
if (child.gameObject.activeInHierarchy)
{
child._OnHidden();
}
}
}
OnHidden();
}
/// <summary>
/// 返回是否有必要下一帧继续执行
/// </summary>
/// <returns></returns>
public bool Update()
{
if (!m_visible || m_destroyed)
{
return false;
}
List<UIWindowBase> listNextUpdateChild = null;
if (m_listChild != null && m_listChild.Count > 0)
{
listNextUpdateChild = m_listUpdateChild;
var updateListValid = m_updateListValid;
List<UIWindowBase> listChild = null;
if (!updateListValid)
{
if (listNextUpdateChild == null)
{
listNextUpdateChild = new List<UIWindowBase>();
m_listUpdateChild = listNextUpdateChild;
}
else
{
listNextUpdateChild.Clear();
}
listChild = m_listChild;
}
else
{
listChild = listNextUpdateChild;
}
for (int i = 0; i < listChild.Count; i++)
{
var window = listChild[i];
var needValid = window.Update();
if (!updateListValid && needValid)
{
listNextUpdateChild.Add(window);
}
}
if (!updateListValid)
{
m_updateListValid = true;
}
}
bool needUpdate = false;
if (listNextUpdateChild == null || listNextUpdateChild.Count <= 0)
{
m_hasOverrideUpdate = true;
OnUpdate();
needUpdate = m_hasOverrideUpdate;
}
else
{
OnUpdate();
needUpdate = true;
}
return needUpdate;
}
#endregion
}
}