From cb653cf75b17b9bdca0b5b9e6b88edf1ca72e90c Mon Sep 17 00:00:00 2001
From: hch <305670599@qq.com>
Date: 星期五, 17 十月 2025 09:54:11 +0800
Subject: [PATCH] 0312 删除旧的日常代码3

---
 Main/System/Hero/UIHeroController.cs |  161 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 140 insertions(+), 21 deletions(-)

diff --git a/Main/System/Hero/UIHeroController.cs b/Main/System/Hero/UIHeroController.cs
index c2291e3..2ad5cd7 100644
--- a/Main/System/Hero/UIHeroController.cs
+++ b/Main/System/Hero/UIHeroController.cs
@@ -2,8 +2,8 @@
 using System;
 using Spine.Unity;
 using UnityEngine;
-using UnityEngine.Events;
-using UnityEngine.EventSystems;
+using UnityEngine.UI;
+
 public class UIHeroController : MonoBehaviour
 {
 	private GameObjectPoolManager.GameObjectPool pool;
@@ -14,24 +14,71 @@
 	private GameObject instanceGO;
 
 	private Action onComplete;
-	public void Create(int _skinID, float scale = 1f, Action _onComplete = null)
+	public void Create(int _skinID, float scale = 0.8f, Action _onComplete = null, string motionName = "idle", bool isLh = false)
 	{
 		if (skinID == _skinID)
-		{ 
+		{
 			//閬垮厤閲嶅鍒涘缓
 			return;
 		}
 
 		skinID = _skinID;
+		var skinConfig = HeroSkinConfig.Get(skinID);
+		if (isLh)
+		{
+
+			//X杞村亸绉伙紝Y杞村亸绉伙紝缂╂斁锛屾槸鍚︽按骞崇炕杞紙0鍚�1鏄級
+			if (skinConfig.TachieParam.Length == 4)
+			{
+				this.transform.localPosition = new Vector3(skinConfig.TachieParam[0], skinConfig.TachieParam[1], 0);
+				this.transform.localScale = Vector3.one * skinConfig.TachieParam[2];
+				this.transform.localRotation = Quaternion.Euler(0, skinConfig.TachieParam[3] == 0 ? 0 : 180, 0);
+			}
+			else
+			{
+				this.transform.localPosition = Vector3.zero;
+				this.transform.localScale = Vector3.one;
+				this.transform.localRotation = Quaternion.identity;
+			}
+
+			//绔嬬粯鐗规畩澶勭悊锛屾病鏈塻pine鍔ㄧ敾鐨勬敼鐢ㄥ浘鐗�
+			var lhImg = this.AddMissingComponent<RawImage>();
+			if (!skinConfig.Tachie.Contains("SkeletonData"))
+			{
+				//鍥剧墖鏇挎崲
+				lhImg.SetTexture2DPNG(skinConfig.Tachie);
+				lhImg.SetNativeSize();
+				if (skeletonGraphic != null)
+				{
+					skeletonGraphic.enabled = false;
+				}
+				lhImg.enabled = true;
+				lhImg.raycastTarget = false;
+				return;
+			}
+			else
+			{
+				if (skeletonGraphic != null)
+				{
+					skeletonGraphic.enabled = true;
+				}
+				lhImg.enabled = false;
+			}
+		}
+		else
+		{
+			this.transform.localScale = Vector3.one * scale;
+		}
+
 		onComplete = _onComplete;
 		pool = GameObjectPoolManager.Instance.RequestPool(UILoader.LoadPrefab("UIHero"));
-		
+
 		if (!transform.gameObject.activeSelf)
 		{
 			transform.SetActive(true);
 		}
 		if (instanceGO == null)
-		{ 
+		{
 			instanceGO = pool.Request();
 			instanceGO.transform.SetParent(transform);
 			//transform 鐨凱ivot Y鏄�0锛岃instanceGO 灞呬腑
@@ -43,12 +90,32 @@
 		}
 
 		skeletonGraphic = instanceGO.GetComponentInChildren<SkeletonGraphic>(true);
-		var skinConfig = HeroSkinConfig.Get(skinID);
-		skeletonGraphic.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("Hero/SpineRes/", skinConfig.SpineRes);
+		if (isLh)
+		{
+			skeletonGraphic.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("Hero/SpineRes/", skinConfig.Tachie);
+		}
+		else
+		{
+			skeletonGraphic.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("Hero/SpineRes/", skinConfig.SpineRes);
+		}
+		if (skeletonGraphic.skeletonDataAsset == null)
+		{
+
+			transform.SetActive(false);
+			if (pool != null)
+				pool.Release(instanceGO);
+			skeletonGraphic = null;
+			Destroy(instanceGO);
+			Debug.LogError("鏈厤缃畇pine");
+			return;
+		}
 		skeletonGraphic.Initialize(true);
-		this.transform.localScale = Vector3.one * scale;
+
 		spineAnimationState = skeletonGraphic.AnimationState;
-		PlayAnimation(MotionName.idle, true);
+		spineAnimationState.Data.DefaultMix = 0f;
+		if (motionName == "")
+			motionName = GetFistSpineAnim();
+		PlayAnimation(motionName, true);
 		spineAnimationState.Complete -= OnAnimationComplete;
 		spineAnimationState.Complete += OnAnimationComplete;
 	}
@@ -58,24 +125,62 @@
 
 	protected void OnDestroy()
 	{
-        if (spineAnimationState != null)
-        {
-            spineAnimationState.Complete -= OnAnimationComplete;
-        }
+		if (spineAnimationState != null)
+		{
+			spineAnimationState.Complete -= OnAnimationComplete;
+		}
 		if (pool != null)
 			pool.Release(instanceGO);
 		skeletonGraphic = null;
 		pool = null;
 	}
 
+	/// <summary>
+	/// 鎾斁 Spine 鍔ㄧ敾
+	/// </summary>
+	/// <param name="motionName">鍔ㄤ綔鍚�</param>
+	/// <param name="loop">寰幆</param>
+	/// <param name="replay">濡傛灉鐩稿悓鍔ㄤ綔鏄惁鍐嶆閲嶆挱锛屾瘮濡傝窇姝ラ噸鎾氨浼氳烦甯т笉椤烘粦</param>
+	public virtual void PlayAnimation(string motionName, bool loop = false, bool replay=true)
+	{
+		if (spineAnimationState == null) return;
 
-    public virtual void PlayAnimation(MotionName motionName, bool loop = false)
-    {
-        if (spineAnimationState == null) return;
+		if (GetCurrentAnimationName() == motionName && !replay)
+			return;
 
-        // 鐩存帴浣跨敤 ToString() 鑰屼笉鏄皟鐢� GetAnimationName
-         spineAnimationState.SetAnimation(0, motionName.ToString(), loop);
-    }
+		// 鐩存帴浣跨敤 ToString() 鑰屼笉鏄皟鐢� GetAnimationName
+		spineAnimationState.SetAnimation(0, motionName.ToString(), loop);
+	}
+
+	// 鎾斁绗竴涓姩鐢伙紙浣滀负榛樿鍔ㄧ敾锛�
+	string GetFistSpineAnim()
+	{
+		var skeletonData = skeletonGraphic.Skeleton.Data;
+		if (skeletonData.Animations.Count > 0)
+		{
+			return skeletonData.Animations.Items[0].Name;
+		}
+		else
+		{
+			Debug.LogError("Spine 鏁版嵁涓病鏈夋壘鍒颁换浣曞姩鐢伙紒姝﹀皢鐨偆锛�" + skinID);
+		}
+		return "";
+	}
+
+	/// <summary>
+	/// 鑾峰彇褰撳墠姝e湪鎾斁鐨� Spine 鍔ㄧ敾鍚嶇О
+	/// </summary>
+	/// <returns>褰撳墠鍔ㄧ敾鍚嶇О锛屽鏋滄病鏈夊姩鐢诲垯杩斿洖绌哄瓧绗︿覆</returns>
+	public string GetCurrentAnimationName()
+	{
+		if (spineAnimationState == null || spineAnimationState.GetCurrent(0) == null)
+		{
+			return string.Empty;
+		}
+		return spineAnimationState.GetCurrent(0).Animation.Name;
+	}
+
+
 
 	/// <summary>
 	/// 鍔ㄧ敾瀹屾垚浜嬩欢澶勭悊
@@ -83,6 +188,20 @@
 	protected virtual void OnAnimationComplete(Spine.TrackEntry trackEntry)
 	{
 		onComplete?.Invoke();
-    }
+	}
 
+	//瓒婂ぇ瓒婂揩
+	public void SetSpeed(float speed)
+	{
+		spineAnimationState.TimeScale = speed;
+	}
+
+	public void SetEnabled(bool isEnable)
+	{ 
+		if (skeletonGraphic == null)
+		{
+			return;
+		}
+		skeletonGraphic.enabled = isEnable;
+	}
 }
\ No newline at end of file

--
Gitblit v1.8.0