打开窗口框架

This commit is contained in:
2025-07-16 19:16:46 +08:00
parent a6f442044a
commit 5348216024
10 changed files with 125 additions and 15 deletions

View File

@@ -134,7 +134,7 @@ GameObject:
- component: {fileID: 225313992}
- component: {fileID: 225313991}
- component: {fileID: 225313990}
m_Layer: 0
m_Layer: 5
m_Name: Canvas
m_TagString: Untagged
m_Icon: {fileID: 0}
@@ -316,7 +316,7 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 519418525}
m_Layer: 0
m_Layer: 5
m_Name: UIRoot
m_TagString: Untagged
m_Icon: {fileID: 0}

View File

@@ -3,7 +3,7 @@ using UnityEngine.UI;
namespace CreatGame.UI
{
public class UIMainView : UIViewBase
public partial class UIMainView : UIViewBase
{
public override string PrefabPath => "Prefabs/UI/MainView";
/// <summary>

View File

@@ -1,10 +1,25 @@
namespace CreatGame
using System.Collections;
using CreatGame.UI;
using UnityEditor.VersionControl;
namespace CreatGame
{
public class GameLogic : Singleton<GameLogic>
{
public void Start()
public IEnumerator Start()
{
AssetBundle.AssetBundleManager.Instance.Initialize();
AssetBundle.AssetBundleManager.Instance.Initialize();
while (true)
{
if (AssetBundle.AssetBundleManager.Instance.IsInitializeAsync)
{
break;
}
yield return null;
}
UIManager.Instance.OpenView<UI.UIMainView>(UILayer.Main);
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
@@ -9,11 +10,16 @@ namespace CreatGame.AssetBundle
{
public class AssetBundleManager : Singleton<AssetBundleManager>
{
/// <summary>
/// 是否初始化完成
/// </summary>
public bool IsInitializeAsync;
/// <summary>
/// 需要初始化Addressble系统
/// </summary>
public AssetBundleManager()
{
IsInitializeAsync = false;
Addressables.InitializeAsync();
}
@@ -54,7 +60,26 @@ namespace CreatGame.AssetBundle
// }
// }
#endregion
IsInitializeAsync = true;
}
}
public void LoadGameObject(string assetBundleName, Action<GameObject> callback)
{
Addressables.LoadAssetAsync<GameObject>(assetBundleName).Completed += (handle) =>
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
var asset = handle.Result;
callback?.Invoke(GameObject.Instantiate(asset));
}
else
{
Debug.Log($"assetBundleName = {assetBundleName} 加载失败 Status = {handle.Status}");
callback?.Invoke(null);
}
};
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d2e66ce219a441108094ae188afcc745
timeCreated: 1752663372

View File

@@ -0,0 +1,18 @@
using UnityEngine;
namespace CreatGame.UI
{
public partial class UIMainView
{
public override void InitView()
{
base.InitView();
StarBtn.onClick.AddListener(OnStarBtnClick);
}
private void OnStarBtnClick()
{
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 19a056c97ef74d2697f0bd0e7efc3651
timeCreated: 1752663384

View File

@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using UnityEngine;
using CreatGame.AssetBundle;
namespace CreatGame.UI
{
public class UIManager : Singleton<UIManager>
{
private GameObject m_UIRoot;
private Dictionary<UILayer,GameObject> m_UILayers;
private Dictionary<UILayer, Queue<UIViewBase>> m_Windows;
/// <summary>
@@ -12,10 +15,33 @@ namespace CreatGame.UI
/// </summary>
public UIManager()
{
m_UILayers = new Dictionary<UILayer, GameObject>();
m_UIRoot = GameObject.Find("UIRoot");
InitLayer();
m_Windows = new Dictionary<UILayer, Queue<UIViewBase>>();
}
/// <summary>
/// 初始化layer层的预制件
/// </summary>
private void InitLayer()
{
if (m_UIRoot == null)
{
return;
}
m_UILayers = new Dictionary<UILayer, GameObject>();
foreach (var layer in Enum.GetValues(typeof(UILayer)))
{
var layerObj = new GameObject(Enum.GetName(typeof(UILayer), layer));
layerObj.transform.SetParent(m_UIRoot.transform);
layerObj.transform.localScale = Vector3.one;
layerObj.transform.localPosition = Vector3.zero;
layerObj.layer = LayerMask.NameToLayer("UI");
m_UILayers.Add((UILayer)layer,layerObj);
}
}
/// <summary>
///
/// </summary>
@@ -26,8 +52,23 @@ namespace CreatGame.UI
{
var view = new T();
//加载预制件
view.InitView();
AssetBundleManager.Instance.LoadGameObject(view.PrefabPath, (obj) =>
{
if (obj == null)
{
Debug.LogError($"窗口加载失败{typeof(T).Name}");
return;
}
view.PreLoad(obj);
if (m_UILayers.TryGetValue(layer, out var uiLayer))
{
obj.transform.SetParent(uiLayer.transform);
obj.transform.localPosition = Vector3.zero;
obj.transform.localScale = Vector3.one;
}
view.InitView();
});
return view;
}
}

View File

@@ -5,7 +5,8 @@ namespace CreatGame.UI
public class UIViewBase
{
/// <summary>
/// 预制件的路径
/// 预制件的Addressables地址
/// 由导出代码自己添加
/// </summary>
public virtual string PrefabPath { get; set; }
/// <summary>
@@ -17,13 +18,17 @@ namespace CreatGame.UI
/// </summary>
protected UIExportTool m_ExportTool;
/// <summary>
/// 是否加载完成
/// </summary>
public bool IsPreLoad = false;
/// <summary>
/// 加载窗口的时候需要预先加载的东西
/// </summary>
public virtual void PreLoad(GameObject viewObject)
{
m_ViewObject = viewObject;
m_ExportTool = viewObject.GetComponent<UIExportTool>();
IsPreLoad = true;
}
/// <summary>
/// 初始化界面

View File

@@ -8,7 +8,7 @@ public class GameStar : MonoBehaviour
// Start is called before the first frame update
void Start()
{
CreatGame.GameLogic.Instance.Start();
StartCoroutine(CreatGame.GameLogic.Instance.Start());
}
// Update is called once per frame