144 lines
4.9 KiB
C#
144 lines
4.9 KiB
C#
using System;
|
||
using Cysharp.Threading.Tasks;
|
||
using Immersal.AR;
|
||
using Stary.Evo;
|
||
using Stary.Evo.InformationSave;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
using YooAsset;
|
||
|
||
namespace Stary.Evo
|
||
{
|
||
public class LoadResState : AbstractFSMIState
|
||
{
|
||
private DomainConfig.LoadResType loadResType;
|
||
public GameObject mainPrefab;
|
||
private DomainConfig domainConfig;
|
||
public LoadResState(IFsmSystem system) : base(system)
|
||
{
|
||
}
|
||
|
||
|
||
public override UniTask OnEnterAsync()
|
||
{
|
||
return UniTask.CompletedTask;
|
||
}
|
||
|
||
public override UniTask OnEnterAsync<T>(T param)
|
||
{
|
||
return UniTask.CompletedTask;
|
||
}
|
||
|
||
public override async UniTask OnEnterAsync<T1, T2>(T1 param1, T2 param2)
|
||
{
|
||
Debug.Log("加载资源...");
|
||
domainConfig = param1 as DomainConfig;
|
||
loadResType = domainConfig.loadResType;
|
||
Type type = param2 as Type;
|
||
var package = YooAssets.GetPackage(domainConfig.domain);
|
||
if (loadResType == DomainConfig.LoadResType.Prefab)
|
||
{
|
||
// 加载热更资源
|
||
var loadOperation = package.LoadAssetAsync<GameObject>(domainConfig.mainPrefab);
|
||
|
||
await loadOperation;
|
||
if (loadOperation.Status == EOperationStatus.Succeed)
|
||
{
|
||
ARSpace arSpace = GameObject.FindObjectOfType<ARSpace>();
|
||
if (arSpace != null)
|
||
{
|
||
Debug.Log("UnityEvo:找到ARSpace,开始加载点云运行环境...");
|
||
mainPrefab = loadOperation.InstantiateSync(arSpace.transform);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("UnityEvo:未找到ARSpace,开始加载普通运行环境,通过语音唤醒...");
|
||
mainPrefab = loadOperation.InstantiateSync();
|
||
}
|
||
if(domainConfig.domain =="Main")
|
||
AppConfig.SetDefaultMainInstance(mainPrefab);
|
||
}
|
||
}
|
||
else if (loadResType == DomainConfig.LoadResType.Scene)
|
||
{
|
||
var sceneMode = domainConfig.loadSceneMode;
|
||
var physicsMode = LocalPhysicsMode.None;
|
||
SceneHandle handle = package.LoadSceneAsync(domainConfig.mainScene, sceneMode, physicsMode);
|
||
await handle;
|
||
mainPrefab = GameObject.Find(domainConfig.mainPrefab);
|
||
}
|
||
|
||
LocalTransformInfo info = mainPrefab.GetComponentInChildren<LocalTransformInfo>();
|
||
FsmLoadSystem fsmLoadSystem = FsmSystem as FsmLoadSystem;
|
||
|
||
if (info._list.Count >= 2)
|
||
{
|
||
if (fsmLoadSystem.GetOpenDomainType() == OpenDomainType.DEFAULT)
|
||
{
|
||
info.Switch(0);
|
||
}
|
||
else if (fsmLoadSystem.GetOpenDomainType() == OpenDomainType.VIOICE)
|
||
{
|
||
info.Switch(1);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"UnityEvo:{mainPrefab.name}的TransformInfo长度小于2,无法继续运行,请排查");
|
||
}
|
||
|
||
if (mainPrefab != null)
|
||
{
|
||
DomainBase hotfixInstance = mainPrefab.GetComponent(type) as DomainBase;
|
||
if (hotfixInstance == null)
|
||
{
|
||
hotfixInstance = mainPrefab.AddComponent(type) as DomainBase;
|
||
}
|
||
|
||
hotfixInstance.DomainName = domainConfig.domain;
|
||
if (hotfixInstance == null)
|
||
{
|
||
Debug.LogError($"热更类{type.Name}实例创建失败!必须继承MonoBehaviour");
|
||
return;
|
||
}
|
||
|
||
|
||
// 原有调用逻辑修改为使用实例
|
||
hotfixInstance.OnEnter("");
|
||
hotfixInstance.OnEnterAsync("");
|
||
}
|
||
}
|
||
|
||
public override void OnUpdate()
|
||
{
|
||
base.OnUpdate();
|
||
}
|
||
|
||
|
||
public override async UniTask OnExitAsync()
|
||
{
|
||
Debug.Log("UnityEvo:Domain退出...");
|
||
if (domainConfig.domain != "Main")
|
||
{
|
||
DomainBase domainBase = mainPrefab.GetComponent<DomainBase>();
|
||
if (domainBase == null)
|
||
{
|
||
Debug.LogError($"UnityEvo:{mainPrefab.name}的DomainBase为空,无法退出,请排查");
|
||
}
|
||
else
|
||
{
|
||
domainBase.OnExit();
|
||
await domainBase.OnExitAsync();
|
||
}
|
||
|
||
if (loadResType == DomainConfig.LoadResType.Scene)
|
||
{
|
||
await SceneManager.UnloadSceneAsync("server");
|
||
}
|
||
|
||
if (domainBase != null)
|
||
GameObject.Destroy(domainBase.gameObject);
|
||
}
|
||
}
|
||
}
|
||
} |