//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 Jiang Yin. All rights reserved. // Homepage: https://gameframework.cn/ // Feedback: mailto:ellan@gameframework.cn //------------------------------------------------------------ using System.Collections; using System.Collections.Generic; using GeometryTD; using TMPro; using UnityEngine; using UnityEngine.UI; using UnityGameFramework.Runtime; namespace GeometryTD.UI { public abstract class UGuiForm : UIFormLogic { public const int DepthFactor = 100; private const float FadeTime = 0.3f; private static Font _mainFont = null; private static TMP_FontAsset _mainTMPFont = null; private Canvas _cachedCanvas = null; private CanvasGroup _canvasGroup = null; private List _cachedCanvasContainer = new List(); public int OriginalDepth { get; private set; } public int Depth => _cachedCanvas.sortingOrder; public void Close() { Close(false); } public void Close(bool ignoreFade) { StopAllCoroutines(); if (ignoreFade) { GameEntry.UI.CloseUIForm(this); } else { StartCoroutine(CloseCo(FadeTime)); } } public void PlayUISound(int uiSoundId) { GameEntry.Sound.PlayUISound(uiSoundId); } public static void SetMainFont(Font mainFont) { if (mainFont == null) { Log.Error("Main font is invalid."); return; } _mainFont = mainFont; } public static void SetMainTMPFont(TMP_FontAsset mainTMPFont) { if (mainTMPFont == null) { Log.Error("Main font is invalid."); return; } _mainTMPFont = mainTMPFont; } #if UNITY_2017_3_OR_NEWER protected override void OnInit(object userData) #else protected internal override void OnInit(object userData) #endif { base.OnInit(userData); _cachedCanvas = gameObject.GetOrAddComponent(); _cachedCanvas.overrideSorting = true; OriginalDepth = _cachedCanvas.sortingOrder; _canvasGroup = gameObject.GetOrAddComponent(); RectTransform rect = GetComponent(); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.anchoredPosition = Vector2.zero; rect.sizeDelta = Vector2.zero; gameObject.GetOrAddComponent(); if (_mainTMPFont == null) { Log.Warning("Main TMP font isn't set."); } else { var tmp_texts = GetComponentsInChildren(true); foreach (var text in tmp_texts) { text.font = _mainTMPFont; } } if (_mainFont == null) { Log.Warning("Main font isn't set."); } else { var texts = GetComponentsInChildren(true); foreach (var text in texts) { text.font = _mainFont; } } } #if UNITY_2017_3_OR_NEWER protected override void OnRecycle() #else protected internal override void OnRecycle() #endif { base.OnRecycle(); } #if UNITY_2017_3_OR_NEWER protected override void OnOpen(object userData) #else protected internal override void OnOpen(object userData) #endif { base.OnOpen(userData); _canvasGroup.alpha = 0f; StopAllCoroutines(); StartCoroutine(_canvasGroup.FadeToAlpha(1f, FadeTime)); } #if UNITY_2017_3_OR_NEWER protected override void OnClose(bool isShutdown, object userData) #else protected internal override void OnClose(bool isShutdown, object userData) #endif { base.OnClose(isShutdown, userData); } #if UNITY_2017_3_OR_NEWER protected override void OnPause() #else protected internal override void OnPause() #endif { base.OnPause(); } #if UNITY_2017_3_OR_NEWER protected override void OnResume() #else protected internal override void OnResume() #endif { base.OnResume(); _canvasGroup.alpha = 0f; StopAllCoroutines(); StartCoroutine(_canvasGroup.FadeToAlpha(1f, FadeTime)); } #if UNITY_2017_3_OR_NEWER protected override void OnCover() #else protected internal override void OnCover() #endif { base.OnCover(); } #if UNITY_2017_3_OR_NEWER protected override void OnReveal() #else protected internal override void OnReveal() #endif { base.OnReveal(); } #if UNITY_2017_3_OR_NEWER protected override void OnRefocus(object userData) #else protected internal override void OnRefocus(object userData) #endif { base.OnRefocus(userData); } #if UNITY_2017_3_OR_NEWER protected override void OnUpdate(float elapseSeconds, float realElapseSeconds) #else protected internal override void OnUpdate(float elapseSeconds, float realElapseSeconds) #endif { base.OnUpdate(elapseSeconds, realElapseSeconds); } #if UNITY_2017_3_OR_NEWER protected override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup) #else protected internal override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup) #endif { int oldDepth = Depth; base.OnDepthChanged(uiGroupDepth, depthInUIGroup); int deltaDepth = UGuiGroupHelper.DepthFactor * uiGroupDepth + DepthFactor * depthInUIGroup - oldDepth + OriginalDepth; GetComponentsInChildren(true, _cachedCanvasContainer); for (int i = 0; i < _cachedCanvasContainer.Count; i++) { _cachedCanvasContainer[i].sortingOrder += deltaDepth; } _cachedCanvasContainer.Clear(); } private IEnumerator CloseCo(float duration) { yield return _canvasGroup.FadeToAlpha(0f, duration); GameEntry.UI.CloseUIForm(this); } } }