添加 BgForm

This commit is contained in:
SepComet
2026-04-18 20:27:12 +08:00
parent fbb9120a42
commit cc5d8edec4
23 changed files with 1128 additions and 166 deletions
@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UI
{
public class BgFormContext : UIContext
{
public Sprite Sprite;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 35695e29b2219844fa8bf1bae0c2bb3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,127 @@
using GameFramework.Event;
using UnityGameFramework.Runtime;
namespace UI
{
public class BgFormController : IFormController<BgFormContext>
{
private BgFormContext _context;
private BgForm _bgForm;
private int? _formSerialId;
private bool _pendingRefresh;
public BgFormController()
{
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, OnCloseUIFormComplete);
}
public int? OpenUI(BgFormContext context)
{
if (context == null)
{
Log.Warning("BgFormController open failed. context is null.");
return null;
}
_context = context;
if (_bgForm != null)
{
_bgForm.RefreshUI(_context);
return _formSerialId;
}
CloseUI();
_pendingRefresh = true;
_formSerialId = GameEntry.UI.OpenUIForm(UIFormId.BgForm, context);
return _formSerialId;
}
public void CloseUI()
{
_pendingRefresh = false;
if (_formSerialId.HasValue)
{
GameEntry.UI.CloseUIForm(_formSerialId.Value);
return;
}
if (_bgForm != null)
{
_bgForm.Close();
}
}
~BgFormController()
{
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, OnCloseUIFormComplete);
}
private void TryRefreshUI()
{
if (_context == null)
{
return;
}
if (_bgForm == null)
{
_pendingRefresh = true;
return;
}
_bgForm.RefreshUI(_context);
_pendingRefresh = false;
}
private void OnOpenUIFormSuccess(object sender, GameEventArgs e)
{
if (!(e is OpenUIFormSuccessEventArgs args))
{
return;
}
if (!_formSerialId.HasValue)
{
return;
}
if (args.UIForm == null || args.UIForm.SerialId != _formSerialId.Value || args.UserData != _context)
{
return;
}
_bgForm = args.UIForm.Logic as BgForm;
if (_bgForm == null)
{
Log.Warning("AIChatFormController open success but form logic is invalid.");
return;
}
if (_pendingRefresh)
{
TryRefreshUI();
}
}
private void OnCloseUIFormComplete(object sender, GameEventArgs e)
{
if (!(e is CloseUIFormCompleteEventArgs args))
{
return;
}
if (args.SerialId != _formSerialId)
{
return;
}
_bgForm = null;
_formSerialId = null;
_pendingRefresh = false;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5676b59e53ae51b449044f870ba39daa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+42
View File
@@ -0,0 +1,42 @@
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace UI
{
public class BgForm : UGuiForm
{
[SerializeField] private Image _bgImage;
private Sprite _currentSprite;
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
if (!(userData is BgFormContext context))
{
Log.Error("BgFormContext is invalid.");
return;
}
RefreshUI(context);
}
protected override void OnClose(bool isShutdown, object userData)
{
_currentSprite = null;
_bgImage.sprite = null;
base.OnClose(isShutdown, userData);
}
public void RefreshUI(BgFormContext context)
{
if (this._currentSprite == context.Sprite) return;
this._currentSprite = context.Sprite;
_bgImage.sprite = context.Sprite;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 956fafaa81c7caa4c840171e4e4488cd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: