using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.U2D; using UnityEngine.UI; using YooAsset; public class Game1Scene : MonoBehaviour { public GameObject CanvasRoot; private readonly List _cachedAssetOperationHandles = new List(1000); private readonly List _cachedSubAssetsOperationHandles = new List(1000); private int _npcIndex = 0; void Start() { YooAssets.UnloadUnusedAssets(); // 初始化窗口 InitWindow(); // 异步加载背景音乐 AsyncLoadMusic(); } void OnDestroy() { foreach (var handle in _cachedAssetOperationHandles) { handle.Release(); } _cachedAssetOperationHandles.Clear(); foreach (var handle in _cachedSubAssetsOperationHandles) { handle.Release(); } _cachedSubAssetsOperationHandles.Clear(); } void OnGUI() { GUIConsole.OnGUI(); } void InitWindow() { var resVersion = CanvasRoot.transform.Find("res_version/label").GetComponent(); resVersion.text = $"资源版本 : {YooAssets.GetResourceVersion()}"; var playMode = CanvasRoot.transform.Find("play_mode/label").GetComponent(); if (BootScene.GamePlayMode == YooAssets.EPlayMode.EditorSimulateMode) playMode.text = "编辑器下模拟模式"; else if (BootScene.GamePlayMode == YooAssets.EPlayMode.OfflinePlayMode) playMode.text = "离线运行模式"; else if (BootScene.GamePlayMode == YooAssets.EPlayMode.HostPlayMode) playMode.text = "网络运行模式"; else throw new NotImplementedException(); // 通过资源标签加载资源 { string assetTag = "sphere"; AssetInfo[] assetInfos = YooAssets.GetAssetInfos(assetTag); foreach (var assetInfo in assetInfos) { Debug.Log($"{assetInfo.AssetPath}"); } } // 同步加载背景图片 #if UNITY_WEBGL { var rawImage = CanvasRoot.transform.Find("background").GetComponent(); AssetOperationHandle handle = YooAssets.LoadAssetAsync("Texture/bg"); _cachedAssetOperationHandles.Add(handle); handle.Completed += (AssetOperationHandle obj) => { rawImage.texture = handle.AssetObject as Texture; }; } #else { var rawImage = CanvasRoot.transform.Find("background").GetComponent(); AssetOperationHandle handle = YooAssets.LoadAssetSync("Texture/bg"); _cachedAssetOperationHandles.Add(handle); rawImage.texture = handle.AssetObject as Texture; } #endif // 同步加载LOGO #if UNITY_WEBGL { var logoImage = CanvasRoot.transform.Find("title/logo").GetComponent(); AssetOperationHandle handle = YooAssets.LoadAssetAsync("Texture/logo.png"); _cachedAssetOperationHandles.Add(handle); handle.Completed += (AssetOperationHandle obj) => { logoImage.sprite = handle.AssetObject as Sprite; }; } #else { var logoImage = CanvasRoot.transform.Find("title/logo").GetComponent(); AssetOperationHandle handle = YooAssets.LoadAssetSync("Texture/logo.png"); _cachedAssetOperationHandles.Add(handle); logoImage.sprite = handle.AssetObject as Sprite; } #endif // 同步加载预制体 { string[] entityAssetNames = { "Level1/footman_Blue", "Level2/footman_Green", "Level3/footman_Red", "Level3/footman_Yellow" }; var btn = CanvasRoot.transform.Find("load_npc/btn").GetComponent