diff --git a/UnityGame/Assets/Scenes/MainScene.unity b/UnityGame/Assets/Scenes/MainScene.unity index e7884fb..0c14952 100644 --- a/UnityGame/Assets/Scenes/MainScene.unity +++ b/UnityGame/Assets/Scenes/MainScene.unity @@ -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} diff --git a/UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs b/UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs index 7e1d964..f2ae0d1 100644 --- a/UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs +++ b/UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs @@ -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"; /// diff --git a/UnityGame/Assets/Scripts/GameLogic/GameLogic.cs b/UnityGame/Assets/Scripts/GameLogic/GameLogic.cs index 355d253..9b377fb 100644 --- a/UnityGame/Assets/Scripts/GameLogic/GameLogic.cs +++ b/UnityGame/Assets/Scripts/GameLogic/GameLogic.cs @@ -1,10 +1,25 @@ -namespace CreatGame +using System.Collections; +using CreatGame.UI; +using UnityEditor.VersionControl; + +namespace CreatGame { public class GameLogic : Singleton { - 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(UILayer.Main); } } } diff --git a/UnityGame/Assets/Scripts/GameLogic/Tools/AssetBundle/AssetBundleManager.cs b/UnityGame/Assets/Scripts/GameLogic/Tools/AssetBundle/AssetBundleManager.cs index 3ec70ea..d771b16 100644 --- a/UnityGame/Assets/Scripts/GameLogic/Tools/AssetBundle/AssetBundleManager.cs +++ b/UnityGame/Assets/Scripts/GameLogic/Tools/AssetBundle/AssetBundleManager.cs @@ -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 { + /// + /// 是否初始化完成 + /// + public bool IsInitializeAsync; /// /// 需要初始化Addressble系统 /// public AssetBundleManager() { + IsInitializeAsync = false; Addressables.InitializeAsync(); } @@ -54,7 +60,26 @@ namespace CreatGame.AssetBundle // } // } #endregion + + IsInitializeAsync = true; } } + + public void LoadGameObject(string assetBundleName, Action callback) + { + Addressables.LoadAssetAsync(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); + } + }; + } } } \ No newline at end of file diff --git a/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main.meta b/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main.meta new file mode 100644 index 0000000..6133c7c --- /dev/null +++ b/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d2e66ce219a441108094ae188afcc745 +timeCreated: 1752663372 \ No newline at end of file diff --git a/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main/UIMainView.cs b/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main/UIMainView.cs new file mode 100644 index 0000000..72c7348 --- /dev/null +++ b/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main/UIMainView.cs @@ -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() + { + + } + } +} \ No newline at end of file diff --git a/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main/UIMainView.cs.meta b/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main/UIMainView.cs.meta new file mode 100644 index 0000000..782c9d7 --- /dev/null +++ b/UnityGame/Assets/Scripts/GameLogic/UI/UILogic/Main/UIMainView.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 19a056c97ef74d2697f0bd0e7efc3651 +timeCreated: 1752663384 \ No newline at end of file diff --git a/UnityGame/Assets/Scripts/GameLogic/UI/UIManager.cs b/UnityGame/Assets/Scripts/GameLogic/UI/UIManager.cs index de8eb7a..b1483c6 100644 --- a/UnityGame/Assets/Scripts/GameLogic/UI/UIManager.cs +++ b/UnityGame/Assets/Scripts/GameLogic/UI/UIManager.cs @@ -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 { + private GameObject m_UIRoot; private Dictionary m_UILayers; private Dictionary> m_Windows; /// @@ -12,10 +15,33 @@ namespace CreatGame.UI /// public UIManager() { - m_UILayers = new Dictionary(); + m_UIRoot = GameObject.Find("UIRoot"); + InitLayer(); m_Windows = new Dictionary>(); - } + /// + /// 初始化layer层的预制件 + /// + private void InitLayer() + { + if (m_UIRoot == null) + { + return; + } + + m_UILayers = new Dictionary(); + + 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); + } + } + /// /// /// @@ -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; } } diff --git a/UnityGame/Assets/Scripts/GameLogic/UI/UIViewBase.cs b/UnityGame/Assets/Scripts/GameLogic/UI/UIViewBase.cs index 34c4e5b..6ee90c0 100644 --- a/UnityGame/Assets/Scripts/GameLogic/UI/UIViewBase.cs +++ b/UnityGame/Assets/Scripts/GameLogic/UI/UIViewBase.cs @@ -5,7 +5,8 @@ namespace CreatGame.UI public class UIViewBase { /// - /// 预制件的路径 + /// 预制件的Addressables地址 + /// 由导出代码自己添加 /// public virtual string PrefabPath { get; set; } /// @@ -17,13 +18,17 @@ namespace CreatGame.UI /// protected UIExportTool m_ExportTool; /// + /// 是否加载完成 + /// + public bool IsPreLoad = false; + /// /// 加载窗口的时候需要预先加载的东西 /// public virtual void PreLoad(GameObject viewObject) { m_ViewObject = viewObject; - m_ExportTool = viewObject.GetComponent(); + IsPreLoad = true; } /// /// 初始化界面 diff --git a/UnityGame/Assets/Scripts/GameStar.cs b/UnityGame/Assets/Scripts/GameStar.cs index 13dcc05..241e895 100644 --- a/UnityGame/Assets/Scripts/GameStar.cs +++ b/UnityGame/Assets/Scripts/GameStar.cs @@ -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