VirtualFramework/Assets/Scripts/UI/UIModuleSelect.cs

155 lines
5.2 KiB
C#
Raw Normal View History

2024-12-14 18:27:59 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using TMPro;
using UnityEngine.SceneManagement;
using System;
namespace QFramework.Example
{
public class UIModuleSelectData : UIPanelData
{
}
public partial class UIModuleSelect : UIPanel
{
2025-03-24 17:18:29 +08:00
ResLoader loader;
2024-12-14 18:27:59 +08:00
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UIModuleSelectData ?? new UIModuleSelectData();
backBtn.onClick.AddListener(() =>
{
Hide();
UIKit.OpenPanelAsync<UIModeSelect>().ToAction().StartGlobal();
});
setBtn.onClick.AddListener(() =>
{
UIKit.OpenPanelAsync<UISetting>(canvasLevel: UILevel.PopUI).ToAction().Start(this);
});
closeBtn.onClick.AddListener(() =>
{
Application.Quit();
});
2025-03-24 17:18:29 +08:00
loader = ResLoader.Allocate();
2024-12-14 18:27:59 +08:00
}
protected override void OnOpen(IUIData uiData = null)
{
Content.RemoveAllChildren();
int moduleCount = 0;
int lastIndex = 0;
2025-06-26 14:07:29 +08:00
2024-12-14 18:27:59 +08:00
for (int i = 0; i < Global.Instance.appData.Modules.Count; i++)
{
var item = Global.Instance.appData.Modules[i];
Global.AppType curType = Global.AppType.All;
Enum.TryParse(item.type, out curType);
if (curType == Global.appTpe || curType == Global.AppType.All)
{
moduleCount++;
2024-12-14 18:27:59 +08:00
int index = i;
GameObject obj = GameObject.Instantiate(BtnItem.gameObject, Content);
obj.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item.ModuleName;
obj.GetComponent<Button>().onClick.AddListener(() =>
{
Global.Instance.curModule = Global.Instance.appData.Modules[index];
UIKit.OpenPanelAsync<UILoading>(canvasLevel: UILevel.PopUI).ToAction().StartGlobal(() =>
{
SceneManager.LoadSceneAsync(Global.Instance.curModule.Scene, LoadSceneMode.Single);
SceneManager.sceneLoaded += OnLoadFinished;
});
});
2025-03-24 17:18:29 +08:00
if (string.IsNullOrEmpty(item.Icon)==false)
{
var icon = obj.transform.Find("Icon").GetComponent<Image>();
var localImageUrl = Global.imagePath + item.Icon;
loader.Add2Load(localImageUrl.ToNetImageResName(),
(bool success, IRes res) =>
{
if (success)
{
icon.sprite = Utility.GetSprite(res.Asset as Texture2D);
}
});
icon.gameObject.SetActive(true);
}
lastIndex = index;
2024-12-14 18:27:59 +08:00
}
}
2024-12-14 18:27:59 +08:00
if (moduleCount == 1)
{
Global.Instance.curModule = Global.Instance.appData.Modules[lastIndex];
UIKit.OpenPanelAsync<UILoading>(canvasLevel: UILevel.PopUI).ToAction().StartGlobal(() =>
{
SceneManager.LoadSceneAsync(Global.Instance.curModule.Scene, LoadSceneMode.Single);
SceneManager.sceneLoaded += OnLoadFinished;
});
2024-12-14 18:27:59 +08:00
}
2025-03-24 17:18:29 +08:00
else
{
UIKit.OpenPanelAsync<UILoading>(canvasLevel: UILevel.PopUI).ToAction().StartGlobal();
loader.LoadAsync(() =>
{
TypeEventSystem.Global.Send<OnLoadingHide>(new OnLoadingHide());
});
}
2024-12-14 18:27:59 +08:00
2024-12-14 18:27:59 +08:00
}
private void OnLoadFinished(Scene arg0, LoadSceneMode arg1)
{
SceneManager.sceneLoaded -= OnLoadFinished;
2025-02-26 17:17:58 +08:00
if (Global.Instance.curModule.Devices != null && Global.Instance.curModule.Devices.Count > 0)
{
var dev = DeviceController.Instance;
}
if (Global.Instance.curModule.body3d != null)
{
var body3d = Body3DController.Instance;
}
if (Global.Instance.curModule.score != null)
{
ScoreController.Instance.Init();
}
2025-02-26 17:17:58 +08:00
if (Global.Instance.curModule.Operations != null && Global.Instance.curModule.Operations.Count > 0)
{
var op = OperationController.Instance;
}
if (Global.Instance.curModule.FSM.Count > 0)
{
var machin = StateMachineController.Instance;
}
2024-12-14 18:27:59 +08:00
UIKit.OpenPanelAsync<UIRightTop>().ToAction().StartGlobal(() =>
{
TypeEventSystem.Global.Send<OnModuleStart>();
TypeEventSystem.Global.Send<OnLoadingHide>();
Hide();
});
}
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
}
}
}