TH1/Unity/Assets/Scripts/TH1_UI/Controller/Base/ViewLoadProperty.cs
2025-10-25 18:53:24 +08:00

136 lines
5.0 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 TH1_Core.Managers;
using TH1_UI.Core;
using UnityEngine;
//
public enum LoadWindowState
{
NONE,
LOADING,
LOADED,
}
//
public class ViewLoadProperty
{
public string SubFolderPath = string.Empty;
public string ResourceName = string.Empty;
public string ControllerName = string.Empty;
//创建在哪个UIRoot下目前有None/Presentation/Tip/Info四种
public UIRootType UIRootType = UIRootType.None;
//public string LoadResPath = ResourcePath.UI_PATH;
public LoadWindowState LoadState = LoadWindowState.NONE;
public bool NessaryResource = false;
//
public void Start(Action onLoadedCallback)
{
// step #1 先检查状态,如果已经在加载,就退出
if (LoadState != LoadWindowState.NONE)
{
ViDebuger.Warning("ViewLoadProperty LoadPacket is Busy, then don't Start");
return;
}
//抛弃异步资源请求
//_viewResource.Start(LoadResPath + SubFolderPath, ResourceName, !NessaryResource, this._OnResourceLoaded, UnityAssisstant.GROUP_PRO);
// step #2 设置自身为加载状态
LoadState = LoadWindowState.LOADING;
// step #3 拼接prefab路径
string path = "Prefab/UI/" + (string.IsNullOrEmpty(SubFolderPath) ? "" : SubFolderPath + "/") + ResourceName;
//Debug.Log(path);
// step #4 加载prefab并处理失败情况
GameObject prefab = Resources.Load<GameObject>(path);
if (prefab == null)
{
Debug.LogError($"[ViewLoadProperty] 加载UI Prefab失败! 请检查路径是否正确: Assets/Resources/{path}");
LoadState = LoadWindowState.NONE; // 加载失败,重置状态
return;
}
// step #5 将资源放到场景中的UIRoot下
// 这里改了原来的异步加载的方案,执行了原本在回调函数 `_OnResourceLoaded` 中做的事情
// 注意这里需要一个UI的根节点来挂载。我们从 UIResourceController 中获取。
Transform uiRoot = UIResourceController.Instance.GetUIRoot(UIRootType);
if (uiRoot == null)
{
Debug.LogError($"[ViewLoadProperty] UI Root尚未设置! UI无法实例化。请确保在游戏开始时调用了 UIResourceController.Instance.SetUIRoot()");
}
GameObject loadedObj = GameObject.Instantiate(prefab, uiRoot);
loadedObj.transform.localPosition = Vector3.zero;
loadedObj.transform.localScale = Vector3.one;
loadedObj.name = ResourceName;
// step #6 设置CanvasGroup使其初始不可见这和原逻辑保持一致
var canvasGroup = loadedObj.GetComponent<CanvasGroup>();
if (canvasGroup == null) canvasGroup = loadedObj.AddComponent<CanvasGroup>();
canvasGroup.alpha = 0;
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
// step #7 这一步至关重要,它让 ViewController 能够找到这个新创建的窗口
UIResourceController.Instance.AddWindow(ResourceName, loadedObj);
// step #8
LoadState = LoadWindowState.LOADED;
// step #9 手动调用完成事件,通知 ViewController "我加载好了!"
//Invoke();不知道为什么原来的用Vi事件系统的代码不生效我改成直接调用一个回调函数
onLoadedCallback?.Invoke();
}
//异步加载资源用的,现在先用不着
/*
void _OnResourceLoaded(ResourceRequestInterface loader)
{
GameObject loadedObj = UnityAssisstant.InstantiateAsChild(_viewResource.TObject, GlobalGameObject.Instance.UIRoot.transform);
loadedObj.transform.localPosition = Vector3.zero;
loadedObj.transform.localScale = Vector3.one;
loadedObj.name = ResourceName;
var canvasGroup = loadedObj.GetComponent<CanvasGroup>();
canvasGroup.alpha = 0;
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
//loadedObj.SetActive(false);
//
//Canvas canvas = loadedObj.GetComponent<Canvas>();
//canvas.worldCamera = GlobalGameObject.Instance.UICamera;
//
UIResourceController.Instance.AddWindow(ControllerName, loadedObj);
//
LoadState = LoadWindowState.LOADED;
Invoke();
}*/
//
public void End()
{
LoadState = LoadWindowState.NONE;
//_viewResource.End();
}
public virtual void Attach(ViAsynCallback cb, ViAsynCallback.Callback func)
{
_eventLoaded.Attach(cb, func);
}
//
public virtual void Attach(ViCallback cb, ViCallback.Callback func)
{
_eventLoaded.Attach(cb, func);
}
//
public void Invoke()
{
_eventLoaded.Invoke(0);
}
//
ViEventList _eventLoaded = new ViEventList();
//先不用这个异步资源请求对象了
//ResourceRequest<GameObject> _viewResource = new ResourceRequest<GameObject>();
}