2025-08-20 02:24:48 +08:00

232 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace TH1_UI.View.Base
{
public class View : MonoBehaviour
{
public Canvas ViewCanvas { get { return _canvas; } }
//
public void Initialize(UIWindowProperty windowProperty)
{
_winProperty = windowProperty;
_canvas = transform.GetComponent<Canvas>();
//
PreInitStart();
InitStart();
//
if(_reSize)
{
_Resize();
}
}
//
protected virtual void PreInitStart() { }
//
protected virtual void InitStart()
{
_viewAnimation = gameObject.GetComponent<Animation>();
_viewAnimationManager = gameObject.GetComponent<AnimationManager>();
_canvasGroup = gameObject.GetComponent<CanvasGroup>();
if(!(this is ViewSub))
{
//ViDebuger.AssertError(_canvasGroup != null, "UI预制根节点必须有CanvasGroup");
}
}
//
void _Resize()
{
RectTransform rect = transform.Find("Content").GetComponent<RectTransform>();
CanvasScaler canvasScaler = GetComponent<CanvasScaler>();
RectTransform canvasRect = GetComponent<RectTransform>();
//
if (canvasScaler.matchWidthOrHeight == 0)
{
if (canvasRect.rect.height <= 1600)
{
canvasScaler.matchWidthOrHeight = 1;
}
}
else if (canvasScaler.matchWidthOrHeight - 1 < 0.01f)
{
if (canvasRect.rect.width <= 720)
{
canvasScaler.matchWidthOrHeight = 0;
}
}
//
float canvasRectHeight = canvasRect.rect.height;
float factRatio = canvasRectHeight / Screen.height;
float safeAreaY = Screen.safeArea.y * factRatio;
float safeAreaHeight = Screen.safeArea.height * factRatio;
//
var offset = canvasRectHeight - safeAreaHeight;
if (safeAreaY == 0)
{
if (offset > 0)
{
rect.offsetMax = new Vector2(rect.offsetMax.x, rect.offsetMax.y - offset);
}
}
else
{
if ((int)offset == (int)safeAreaY)
{
rect.offsetMin = new Vector2(rect.offsetMin.x, rect.offsetMin.y + offset);
}
else if ((int)offset > (int)safeAreaY)
{
var bottomOffset = safeAreaY;
var topOffset = canvasRectHeight - safeAreaHeight - bottomOffset;
rect.offsetMin = new Vector2(rect.offsetMin.x, rect.offsetMin.y + bottomOffset);
rect.offsetMax = new Vector2(rect.offsetMax.x, rect.offsetMax.y - topOffset);
}
}
}
//
public void SetSortingOrder(int depth)
{
_canvas.sortingOrder = depth;
//
//UnityComponentList<RelativeSortingOnCanvas>.Begin(gameObject, true);
//List<RelativeSortingOnCanvas> list = UnityComponentList<RelativeSortingOnCanvas>.List;
//for (int iter = 0, count = list.Count; iter < count; ++iter)
//{
//list[iter].UpdateSorting();
//}
//UnityComponentList<RelativeSortingOnCanvas>.End();
}
//
public virtual bool IsShow()
{
return _show && gameObject.activeInHierarchy && _canvasGroup.alpha > 0;
}
//
public virtual void Show()
{
_showTimeNode.Detach();
_hideTimeNode.Detach();
//UIUtil.SetViewTouchable(true);
//
_show = true;
//
if (_viewAnimation != null && !string.IsNullOrEmpty(_showAnimName))
{
_viewAnimation[_showAnimName].time = 0f;
_viewAnimation[_showAnimName].enabled = true;
_viewAnimation[_showAnimName].weight = 1f;
_viewAnimation.Sample();
_viewAnimation[_showAnimName].enabled = false;
//
_viewAnimation.Play(_showAnimName);
float animDuration = _viewAnimation[_showAnimName].length;
if (!_enableClick)
{
//UIUtil.SetViewTouchable(false);
}
ViTimerInstance.SetTime(_showTimeNode, animDuration, OnShowAnimFinished);
}
else if(_viewAnimationManager != null && _viewAnimationManager.GetGroup("enter") != null)
{
var hasEnterGroup = _viewAnimationManager.HasGroup("enter");
var enableClick = _enableClick || !hasEnterGroup;
if (!enableClick)
{
//UIUtil.SetViewTouchable(false);
}
_viewAnimationManager.Play("enter", true, OnShowAnimFinished);
}
//
//gameObject.SetActive(true);
_SetAlphaActive(true);
}
//
public virtual void Hide()
{
_showTimeNode.Detach();
_hideTimeNode.Detach();
//UIUtil.SetViewTouchable(true);
//
_show = false;
//
if (_viewAnimation != null && !string.IsNullOrEmpty(_hideAnimName))
{
_viewAnimation.Play(_hideAnimName);
float animDuration = _viewAnimation[_hideAnimName].length;
//UIUtil.SetViewTouchable(false);
ViTimerInstance.SetTime(_hideTimeNode, animDuration, _OnHideAnimFinished);
}
else if(_viewAnimationManager != null && _viewAnimationManager.GetGroup("end") != null)
{
var hasEnterGroup = _viewAnimationManager.HasGroup("end");
var enableClick = _enableClick || !hasEnterGroup;
if (!enableClick)
{
//UIUtil.SetViewTouchable(false);
}
_viewAnimationManager.Play("end", true, _OnHideAnimFinished);
}
else
{
_SetAlphaActive(false);
//gameObject.SetActive(false);
}
}
//
void _SetAlphaActive(bool value)
{
_canvasGroup.alpha = value ? 1.0f : 0;
_canvasGroup.interactable = value;
_canvasGroup.blocksRaycasts = value;
}
//
protected virtual void OnShowAnimFinished(ViTimeNodeInterface node)
{
//UIUtil.SetViewTouchable(true);
}
//
void _OnHideAnimFinished(ViTimeNodeInterface node)
{
_SetAlphaActive(false);
//gameObject.SetActive(false);
//UIUtil.SetViewTouchable(true);
}
//
public void SetEnableClick(bool value)
{
_enableClick = value;
}
//
protected virtual void OnDispose()
{
_showTimeNode.Detach();
_hideTimeNode.Detach();
_viewAnimationManager?.Dispose();
//UIUtil.SetViewTouchable(true);
}
//
private void OnDestroy()
{
OnDispose();
}
//
protected bool _show;
protected Canvas _canvas;
protected UIWindowProperty _winProperty;
protected string _showAnimName = string.Empty;
protected string _hideAnimName = string.Empty;
protected Animation _viewAnimation;
protected AnimationManager _viewAnimationManager;
protected ViTimeNode1 _showTimeNode = new ViTimeNode1();
protected ViTimeNode1 _hideTimeNode = new ViTimeNode1();
protected bool _reSize = false;
protected CanvasGroup _canvasGroup;
bool _enableClick = false;
}
}