| | |
| | | |
| | | |
| | | using System; |
| | | using Spine; |
| | | using Spine.Unity; |
| | |
| | | private static int lastInitFrame = -1; // 上一次执行Initialize的帧号,用于确保每帧最多1次 |
| | | private static GameObjectPoolManager.GameObjectPool cachedUIHeroPool; // 缓存UIHero预制体池 |
| | | private RectTransform _instanceRect; // 缓存RectTransform避免重复GetComponent |
| | | |
| | | public Action onComplete; |
| | | #pragma warning disable CS1998 // 异步方法内部通过 DelayedInitializeAsync 异步执行,无需直接 await |
| | | public async UniTask Create(int _skinID, float scale = 0.8f, Action _onComplete = null, string motionName = "idle", bool isLh = false) |
| | | { |
| | | if (skinID == _skinID) |
| | | { |
| | | //避免重复创建 |
| | | |
| | | if (skeletonGraphic != null) |
| | | { |
| | | SetMaterialNone(); |
| | |
| | | // 使用 UniTask 进行异步初始化,将instanceGO创建和资源加载都移到异步处理 |
| | | DelayedInitializeAsync(skinConfig, motionName, isLh, loadCancellationToken.Token).Forget(); |
| | | } |
| | | #pragma warning restore CS1998 |
| | | |
| | | /// <summary> |
| | | /// 取消之前的加载任务 |
| | |
| | | private async UniTaskVoid DelayedInitializeAsync(HeroSkinConfig skinConfig, string motionName, bool isLh, System.Threading.CancellationToken cancellationToken) |
| | | { |
| | | isInitializing = true; |
| | | |
| | | try |
| | | { |
| | | // 检查是否已被取消 |
| | |
| | | |
| | | // 获取加载信号量 - 限制并发数,避免资源竞争 |
| | | await AcquireLoadSlotAsync(cancellationToken); |
| | | |
| | | // 异步创建instanceGO和加载资源(真正的异步,不阻塞) |
| | | await CreateInstanceAndLoadAssetsAsync(skinConfig, isLh, cancellationToken); |
| | | |
| | | // 再次检查是否已被取消 |
| | | cancellationToken.ThrowIfCancellationRequested(); |
| | | |