using System.Collections; using AOT.Boot; using UnityEngine; using YooAsset; namespace HotUpdate { /// /// 加载人物角色 Explorer 的独立预览(地址 CaoCao)。 /// 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(PrefabAddress); yield return handle; if (handle.Status == EOperationStatus.Succeeded) { prefab = handle.GetAssetObject(); 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(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}"); } } }