97 lines
3.3 KiB
C#
97 lines
3.3 KiB
C#
using System.Collections;
|
|
using AOT.Boot;
|
|
using UnityEngine;
|
|
using YooAsset;
|
|
|
|
namespace HotUpdate
|
|
{
|
|
/// <summary>
|
|
/// 加载人物角色 Explorer 的独立预览(地址 CaoCao)。
|
|
/// </summary>
|
|
public sealed class CharacterSpawner : MonoBehaviour
|
|
{
|
|
public const string PrefabAddress = "Explorer";
|
|
public const string EditorPrefabPath = "Assets/Bundles/Characters/Human/Explorer/Explorer.prefab";
|
|
public const string ArtPackageName = "Art";
|
|
|
|
BootContext _ctx;
|
|
GameObject _instance;
|
|
|
|
public void Bind(BootContext ctx) => _ctx = ctx;
|
|
|
|
public void Spawn()
|
|
{
|
|
StartCoroutine(SpawnCo());
|
|
}
|
|
|
|
IEnumerator SpawnCo()
|
|
{
|
|
GameObject prefab = null;
|
|
|
|
// 1) YooAsset Art 包(Offline/Host 已初始化时)
|
|
if (YooAssets.IsInitialized && YooAssets.TryGetPackage(ArtPackageName, out var art) &&
|
|
art.InitializeStatus == EOperationStatus.Succeeded)
|
|
{
|
|
var handle = art.LoadAssetAsync<GameObject>(PrefabAddress);
|
|
yield return handle;
|
|
if (handle.Status == EOperationStatus.Succeeded)
|
|
{
|
|
prefab = handle.GetAssetObject<GameObject>();
|
|
Debug.Log($"[CharacterSpawner] loaded from YooAsset Art address={PrefabAddress}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[CharacterSpawner] Art load failed: {handle.Error}");
|
|
handle.Release();
|
|
}
|
|
}
|
|
|
|
// 2) 编辑器直通:AssetDatabase
|
|
#if UNITY_EDITOR
|
|
if (prefab == null)
|
|
{
|
|
prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(EditorPrefabPath);
|
|
if (prefab != null)
|
|
Debug.Log($"[CharacterSpawner] loaded via AssetDatabase {EditorPrefabPath}");
|
|
}
|
|
#endif
|
|
|
|
if (prefab == null)
|
|
{
|
|
Debug.LogError("[CharacterSpawner] Explorer prefab not found");
|
|
yield break;
|
|
}
|
|
|
|
if (_instance != null)
|
|
Destroy(_instance);
|
|
|
|
_instance = Instantiate(prefab);
|
|
_instance.name = "Explorer_Runtime";
|
|
var y = GroundSit.Height(Vector3.zero, null);
|
|
_instance.transform.position = new Vector3(0f, y, 0f);
|
|
DontDestroyOnLoad(_instance);
|
|
|
|
var billboardTf = _instance.transform.Find("Billboard");
|
|
if (billboardTf != null && Camera.main != null)
|
|
{
|
|
var cam = Camera.main.transform;
|
|
var dir = cam.position - billboardTf.position;
|
|
dir.y = 0f;
|
|
if (dir.sqrMagnitude > 0.001f)
|
|
billboardTf.rotation = Quaternion.LookRotation(-dir.normalized);
|
|
}
|
|
|
|
if (Camera.main != null)
|
|
{
|
|
var cam = Camera.main;
|
|
cam.transform.position = new Vector3(0f, 1.2f, -2.2f);
|
|
cam.transform.rotation = Quaternion.Euler(8f, 0f, 0f);
|
|
cam.fieldOfView = 50f;
|
|
cam.backgroundColor = new Color(0.45f, 0.62f, 0.78f);
|
|
}
|
|
|
|
Debug.Log($"[CharacterSpawner] spawned ok res={_ctx?.ResVersion}");
|
|
}
|
|
}
|
|
}
|