Files
Fishing2/Assets/Scripts/NBC/Runtime/UI/Component/UIPanel.cs
2025-10-29 22:41:47 +08:00

309 lines
7.9 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 System;
using System.Collections.Generic;
using FairyGUI;
using NBC.Entitas;
using NBC.Event;
using UnityEngine;
namespace NBC
{
public abstract class UIPanel //: Entity
{
public static Func<string, string> GetUIPackNameFunc = s => $"{s}/{s}";
/// <summary>
/// 是否显示在最顶层
/// </summary>
public bool IsTop => _ui.IsTop(this);
/// <summary>
/// 是否显示黑色背景蒙版
/// </summary>
public bool IsModal { get; protected set; }
public virtual bool IsShowing => ContentPane != null && ContentPane.parent != null;
public virtual bool IsCanVisible => ContentPane != null && ContentPane.parent != null && ContentPane.visible;
public bool IsDotDel { get; protected set; }
public bool IsDontBack { get; protected set; }
public virtual string UIPackRootUrl => string.Empty;
public virtual string UIPackName { get; set; }
public virtual string UIResName { get; set; }
/// <summary>
/// 模块id(可用于对模块编号实现一些特定功能,如新手引导)
/// </summary>
public virtual int Id { get; protected set; }
/// <summary>
/// 是否显示光标,屏蔽游戏内输入
/// </summary>
public virtual bool IsShowCursor { get; protected set; } = true;
/// <summary>
/// 面板打开动画
/// </summary>
public Func<FTask> ShowAnim;
/// <summary>
/// 面板关闭动画
/// </summary>
public Func<FTask> HideAnim;
private object _paramData;
private bool _isInited;
public GComponent ContentPane { get; protected set; }
protected UIComponent _ui;
public void SetUIManager(UIComponent manager)
{
_ui = manager;
}
public void SetData(object args)
{
_paramData = args;
}
public object GetData()
{
return _paramData;
}
public virtual string[] GetDependPackages()
{
return new string[] { };
}
public void Init()
{
try
{
var uiPackRootUrl = string.IsNullOrEmpty(UIPackRootUrl) ? UIConst.UIPackRootUrl : UIPackRootUrl;
//实例化预设
if (!_isInited)
{
var dependPackages = GetDependPackages();
if (dependPackages != null && dependPackages.Length > 0)
{
foreach (var package in dependPackages)
{
if (package != UIPackName)
{
_ui.AddPackage(uiPackRootUrl, GetUIPackNameFunc(package));
}
}
}
_ui.AddPackage(uiPackRootUrl, GetUIPackNameFunc(UIPackName));
GObject panelObj = UIPackage.CreateObject(UIPackName, UIResName);
if (panelObj == null)
{
throw new Exception("不存在包名:" + UIPackName + "/ResName=" + UIResName);
}
panelObj.SetSize(GRoot.inst.width, GRoot.inst.height);
panelObj.position = Vector3.zero;
panelObj.scale = Vector2.one;
panelObj.pivotX = 0.5f;
panelObj.pivotY = 0.5f;
ContentPane = panelObj.asCom;
ContentPane.name = UIResName;
this.AutoFindAllField();
OnInit();
// FairyBatching
GComponent panelObjCom = panelObj.asCom;
if (panelObjCom != null)
{
panelObjCom.fairyBatching = true;
}
_isInited = true;
}
}
catch (Exception e)
{
Log.Error(e);
throw;
}
}
internal async FTask Show()
{
try
{
if (!IsShowing)
{
GRoot.inst.AddChild(ContentPane);
_ui.AdjustModalLayer();
}
else
{
if (!IsTop) _ui.BringToFront(this);
}
OpenAnimBegin();
if (ShowAnim != null)
{
await ShowAnim();
}
OpenAnimFinished();
}
catch (Exception e)
{
Log.Error($"UIPackName={UIPackName} UIResName={UIResName} e={e}");
throw;
}
}
internal async FTask Hide()
{
if (!IsShowing) return;
if (HideAnim != null)
{
await HideAnim();
}
HideAnimFinished();
}
public void Update()
{
OnUpdate();
}
public void Refresh()
{
_ = Show();
}
/// <summary>
/// 设置刷新多语言
/// </summary>
public void SetLanguage()
{
_ui.TrySetPanelLanguage(ContentPane);
OnSetLanguage();
}
public void HideImmediately()
{
if (ContentPane.parent != null)
{
GRoot.inst.RemoveChild(ContentPane);
_ui.AdjustModalLayer();
var evData = new UIHideEvent()
{
panel = this
};
App.Main.EventComponent.Publish(evData);
App.Main.EventComponent.PublishAsync(evData).Coroutine();
}
OnHide();
}
public void Dispose()
{
if (!IsDotDel)
{
HideImmediately();
ContentPane.Dispose();
OnDestroy();
}
else
{
Log.Error("当前panel标记为不可删除name=" + UIResName);
}
}
private void OpenAnimBegin()
{
OnShow();
SetLanguage();
var evData = new UIShowEvent()
{
panel = this
};
App.Main.EventComponent.Publish(evData);
App.Main.EventComponent.PublishAsync(evData).Coroutine();
}
/// <summary>
/// 打开动画播放完成
/// </summary>
private void OpenAnimFinished()
{
OnShowed();
}
/// <summary>
/// 关闭动画播放完成
/// </summary>
private void HideAnimFinished()
{
HideImmediately();
}
#region
/// <summary>
/// 界面初始化的时候
/// </summary>
protected virtual void OnInit()
{
}
/// <summary>
/// 代码设置多语言OnShow后和语言变化时会调用
/// </summary>
protected virtual void OnSetLanguage()
{
}
/// <summary>
/// 显示界面显示
/// </summary>
/// <param name="param"></param>
protected virtual void OnShow()
{
}
/// <summary>
/// 显示界面完成(动画后)
/// </summary>
protected virtual void OnShowed()
{
}
protected virtual void OnUpdate()
{
}
/// <summary>
/// 界面隐藏的时候
/// </summary>
protected virtual void OnHide()
{
}
/// <summary>
/// 界面销毁的时候
/// </summary>
protected virtual void OnDestroy()
{
}
#endregion
}
}