From 0fa617a09eedf6bdb25eda55fac1d3344859fd93 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期二, 31 三月 2026 19:46:31 +0800
Subject: [PATCH] webgl

---
 Main/System/Hero/UIHeroController.cs |   90 ++++++++++++++++++++++++++++++--------------
 1 files changed, 61 insertions(+), 29 deletions(-)

diff --git a/Main/System/Hero/UIHeroController.cs b/Main/System/Hero/UIHeroController.cs
index 687b490..fbf85d8 100644
--- a/Main/System/Hero/UIHeroController.cs
+++ b/Main/System/Hero/UIHeroController.cs
@@ -23,7 +23,8 @@
 	private const int MAX_CONCURRENT_LOADS = 4;
 	private static int currentLoadingCount = 0;
 	private static readonly object loadLock = new object();
-	private static int initializationOrder = 0; // 鐢ㄤ簬鍒嗗抚寤惰繜鐨勫簭鍙�
+	private static int lastInitFrame = -1; // 涓婁竴娆℃墽琛孖nitialize鐨勫抚鍙凤紝鐢ㄤ簬纭繚姣忓抚鏈�澶�1娆�
+	private static GameObjectPoolManager.GameObjectPool cachedUIHeroPool; // 缂撳瓨UIHero棰勫埗浣撴睜
 
 	public Action onComplete;
 	public async UniTask Create(int _skinID, float scale = 0.8f, Action _onComplete = null, string motionName = "idle", bool isLh = false)
@@ -81,12 +82,10 @@
 			{
 				//鍥剧墖鏇挎崲
 				lhImg.SetTexture2DPNG(skinConfig.Tachie);
-				lhImg.SetNativeSize();
 				if (skeletonGraphic != null)
 				{
 					skeletonGraphic.enabled = false;
 				}
-				lhImg.enabled = true;
 				lhImg.raycastTarget = false;
 				return;
 			}
@@ -201,7 +200,9 @@
 		}
 
 		onComplete = _onComplete;
-		pool = GameObjectPoolManager.Instance.GetPool(await UILoader.LoadPrefabAsync("UIHero"));
+		if (cachedUIHeroPool == null)
+			cachedUIHeroPool = GameObjectPoolManager.Instance.GetPool(await UILoader.LoadPrefabAsync("UIHero"));
+		pool = cachedUIHeroPool;
 		if (this == null) return;
 
 		if (!transform.gameObject.activeSelf)
@@ -251,6 +252,10 @@
 		}
 
 		skeletonGraphic.enabled = true;
+		// 娉ㄥ唽鍒癝pineUpdateManager锛屾壒閲忔洿鏂板噺灏戣法璇█璋冪敤寮�閿�
+		SpineUpdateManager.Instance.Register(skeletonGraphic);
+		// 涓嶅彲瑙佹椂瀹屽叏鍋滄鏇存柊锛屾粴鍔ㄥ垪琛ㄥ満鏅敹鐩婂緢澶�
+		skeletonGraphic.updateWhenInvisible = Spine.Unity.UpdateMode.Nothing;
 		SetMaterialNone();
 
 		spineAnimationState = skeletonGraphic.AnimationState;
@@ -262,7 +267,7 @@
 		spineAnimationState.Complete -= OnAnimationComplete;
 		spineAnimationState.Complete += OnAnimationComplete;
 
-#if UNITY_EIDTOR
+#if UNITY_EDITOR
 		await UniTask.Delay(100);
 		if (skeletonGraphic != null && skeletonGraphic.material != null)
 		{
@@ -292,6 +297,9 @@
 		{
 			spineAnimationState.Complete -= OnAnimationComplete;
 		}
+		// 浠嶴pineUpdateManager涓Щ闄�
+		if (skeletonGraphic != null)
+			SpineUpdateManager.Instance.Unregister(skeletonGraphic);
 		if (pool != null)
 			pool.Release(instanceGO);
 		skeletonGraphic = null;
@@ -452,7 +460,35 @@
 			Debug.LogWarning("skeletonGraphic is null, cannot set material to none");
 			return;
 		}
-		skeletonGraphic.material = null;
+		skeletonGraphic.material = GetDefaultSpineMaterial();
+	}
+
+	/// <summary>
+	/// 鑾峰彇榛樿鐨� Spine 娓叉煋鏉愯川銆�
+	/// 瀵逛簬鍖呭惈闈� Normal 娣峰悎妯″紡锛圓dditive/Multiply/Screen锛夋彃妲界殑瑙掕壊锛�
+	/// 杩斿洖 atlas 鍘熷鏉愯川锛圔lend One OneMinusSrcAlpha锛夛紝淇濊瘉 PMA 娣峰悎姝g‘锛�
+	/// 瀵逛簬鏅�氳鑹诧紝杩斿洖 null锛堜娇鐢� Canvas 榛樿鏉愯川锛岃涓轰笉鍙橈級銆�
+	/// </summary>
+	private Material GetDefaultSpineMaterial()
+	{
+		if (skeletonGraphic == null || skeletonGraphic.Skeleton == null)
+			return null;
+
+		var slotsData = skeletonGraphic.Skeleton.Data.Slots;
+		for (int i = 0; i < slotsData.Count; i++)
+		{
+			if (slotsData.Items[i].BlendMode != Spine.BlendMode.Normal)
+			{
+				// 瀛樺湪闈� Normal 娣峰悎妯″紡鐨勬彃妲斤紝蹇呴』浣跨敤 Spine 鏉愯川
+				// Canvas 榛樿鏉愯川鐨� Blend SrcAlpha OneMinusSrcAlpha 浼氬鑷�
+				// PMA Additive 鏁堟灉锛坴ertex alpha=0锛夊畬鍏ㄦ秷澶�
+				var dataAsset = skeletonGraphic.skeletonDataAsset;
+				if (dataAsset != null && dataAsset.atlasAssets.Length > 0)
+					return dataAsset.atlasAssets[0].PrimaryMaterial;
+				break;
+			}
+		}
+		return null;
 	}
 
 	/// <summary>
@@ -475,30 +511,16 @@
 			// 寮傛鍒涘缓instanceGO鍜屽姞杞借祫婧愶紙鐪熸鐨勫紓姝ワ紝涓嶉樆濉烇級
 			await CreateInstanceAndLoadAssetsAsync(skinConfig, isLh, cancellationToken);
 
-			// 鑾峰彇褰撳墠搴忓彿鐢ㄤ簬鍒嗗抚寤惰繜
-			int myOrder;
-			lock (loadLock)
-			{
-				myOrder = initializationOrder++;
-			}
-
 			// 鍐嶆妫�鏌ユ槸鍚﹀凡琚彇娑�
 			cancellationToken.ThrowIfCancellationRequested();
 
-			// 鍦� skeletonGraphic.Initialize() 鍓嶈繘琛屽垎甯у欢杩�
-			// 鏍规嵁 MAX_CONCURRENT_LOADS 璋冩暣寤惰繜锛岄伩鍏嶆墍鏈夊璞″悓鏃舵墽琛� Initialize
-			int delayFrames = (myOrder % MAX_CONCURRENT_LOADS);
-			if (delayFrames > 0)
+			// 纭繚姣忓抚鏈�澶氭墽琛�1娆nitialize(true)锛岄伩鍏嶅悓甯уぇ閲忛楠艰В鏋愬鑷村崱椤�
+			while (Time.frameCount == lastInitFrame)
 			{
-				for (int i = 0; i < delayFrames; i++)
-				{
-					cancellationToken.ThrowIfCancellationRequested();
-					await UniTask.NextFrame(cancellationToken);
-				}
+				cancellationToken.ThrowIfCancellationRequested();
+				await UniTask.NextFrame(cancellationToken);
 			}
-
-			// 鍐嶆妫�鏌ユ槸鍚﹀凡琚彇娑堬紙鍙兘鍦ㄥ欢杩熸湡闂磋鍙栨秷锛�
-			cancellationToken.ThrowIfCancellationRequested();
+			lastInitFrame = Time.frameCount;
 
 			if (skeletonGraphic == null || skeletonGraphic.skeletonDataAsset == null)
 			{
@@ -508,6 +530,7 @@
 
 			skeletonGraphic.initialSkinName = skinConfig.InitialSkinName;
 			skeletonGraphic.Initialize(true);
+			Debug.Log($"[UIHeroController] Spine Initialize瀹屾垚, skeleton valid: {skeletonGraphic.IsValid}, enabled: {skeletonGraphic.enabled}");
 
 			// 鍒濆鍖栧畬鎴愬悗璁剧疆鐨偆
 			if (!string.IsNullOrEmpty(skinConfig.InitialSkinName))
@@ -524,13 +547,18 @@
 			// 鍒濆鍖栧畬鎴愬悗鎵嶆樉绀烘ā鍨�
 			skeletonGraphic.enabled = pendingEnabled;
 
+			// 娉ㄥ唽鍒癝pineUpdateManager锛屾壒閲忔洿鏂板噺灏戣法璇█璋冪敤寮�閿�
+			SpineUpdateManager.Instance.Register(skeletonGraphic);
+			// 涓嶅彲瑙佹椂瀹屽叏鍋滄鏇存柊锛屾粴鍔ㄥ垪琛ㄥ満鏅敹鐩婂緢澶�
+			skeletonGraphic.updateWhenInvisible = Spine.Unity.UpdateMode.Nothing;
+
 			if (pendingGray)
 			{
 				skeletonGraphic.material = MaterialUtility.GetDefaultSpriteGrayMaterial();
 			}
 			else
 			{
-				skeletonGraphic.material = null;
+				skeletonGraphic.material = GetDefaultSpineMaterial();
 			}
 
 			// 妫�鏌ユ槸鍚︽湁寰呰缃殑閫熷害锛屽鏋滄湁鍒欒缃�
@@ -573,7 +601,7 @@
 		}
 		catch (System.Exception e)
 		{
-			Debug.LogError($"鑻遍泟鍒濆鍖栧紓甯�: {e.Message}");
+			Debug.LogError($"鑻遍泟鍒濆鍖栧紓甯�: {e.Message}\n{e.StackTrace}");
 			isInitializing = false;
 		}
 		finally
@@ -633,8 +661,10 @@
 		// 妫�鏌ユ槸鍚﹀凡琚彇娑�
 		cancellationToken.ThrowIfCancellationRequested();
 
-		// 鍒涘缓pool鍜宨nstanceGO
-		pool = GameObjectPoolManager.Instance.GetPool(UILoader.LoadPrefab("UIHero"));
+		// 鍒涘缓pool鍜宨nstanceGO锛堜娇鐢ㄧ紦瀛橀伩鍏嶉噸澶嶅姞杞介鍒朵綋锛�
+		if (cachedUIHeroPool == null)
+			cachedUIHeroPool = GameObjectPoolManager.Instance.GetPool(await UILoader.LoadPrefabAsync("UIHero"));
+		pool = cachedUIHeroPool;
 
 		if (instanceGO == null)
 		{
@@ -650,6 +680,7 @@
 
 		// 鐪熸鐨勫紓姝ュ姞杞借祫婧� - 涓嶉樆濉炰富绾跨▼
 		string assetName = isLh ? skinConfig.Tachie : skinConfig.SpineRes;
+		Debug.Log($"[UIHeroController] 寮�濮嬪姞杞絪pine 璧勬簮: {assetName}");
 		SkeletonDataAsset loadedAsset = await ResManager.Instance.LoadAssetAsync<SkeletonDataAsset>("Hero/SpineRes/", assetName);
 
 		// 鍐嶆妫�鏌ユ槸鍚﹀凡琚彇娑�
@@ -658,6 +689,7 @@
 		if (loadedAsset != null)
 		{
 			skeletonGraphic.skeletonDataAsset = loadedAsset;
+			Debug.Log($"[UIHeroController] Spine璧勬簮鍔犺浇鎴愬姛: {assetName}, atlas count: {loadedAsset.atlasAssets?.Length}");
 		}
 		else
 		{

--
Gitblit v1.8.0