|  |  |  | 
|---|
|  |  |  | using System; | 
|---|
|  |  |  | using Spine.Unity; | 
|---|
|  |  |  | using UnityEngine; | 
|---|
|  |  |  | using UnityEngine.UI; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public class UIHeroController : MonoBehaviour | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //立绘特殊处理,没有spine动画的改用图片 | 
|---|
|  |  |  | 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 的Pivot Y是0,让instanceGO 居中 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | skeletonGraphic = instanceGO.GetComponentInChildren<SkeletonGraphic>(true); | 
|---|
|  |  |  | var skinConfig = HeroSkinConfig.Get(skinID); | 
|---|
|  |  |  | 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) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | this.SetActive(false); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | transform.SetActive(false); | 
|---|
|  |  |  | if (pool != null) | 
|---|
|  |  |  | pool.Release(instanceGO); | 
|---|
|  |  |  | skeletonGraphic = null; | 
|---|
|  |  |  | Destroy(instanceGO); | 
|---|
|  |  |  | Debug.LogError("未配置spine"); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | skeletonGraphic.Initialize(true); | 
|---|
|  |  |  | this.transform.localScale = Vector3.one * scale; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | spineAnimationState = skeletonGraphic.AnimationState; | 
|---|
|  |  |  | spineAnimationState.Data.DefaultMix = 0f; | 
|---|
|  |  |  | if (motionName == "") | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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(string motionName, bool loop = false) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (spineAnimationState == null) return; | 
|---|
|  |  |  | if (GetCurrentAnimationName() == motionName && !replay) | 
|---|
|  |  |  | return; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 直接使用 ToString() 而不是调用 GetAnimationName | 
|---|
|  |  |  | spineAnimationState.SetAnimation(0, motionName.ToString(), loop); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | spineAnimationState.SetAnimation(0, motionName.ToString(), loop); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 播放第一个动画(作为默认动画) | 
|---|
|  |  |  | string GetFistSpineAnim() | 
|---|
|  |  |  | 
|---|
|  |  |  | Debug.LogError("Spine 数据中没有找到任何动画!武将皮肤:" + skinID); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ""; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 获取当前正在播放的 Spine 动画名称 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <returns>当前动画名称,如果没有动画则返回空字符串</returns> | 
|---|
|  |  |  | public string GetCurrentAnimationName() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (spineAnimationState == null || spineAnimationState.GetCurrent(0) == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return string.Empty; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return spineAnimationState.GetCurrent(0).Animation.Name; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 动画完成事件处理 | 
|---|
|  |  |  | 
|---|
|  |  |  | onComplete?.Invoke(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //越大越快 | 
|---|
|  |  |  | public void SetSpeed(float speed) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | spineAnimationState.TimeScale = speed; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void SetEnabled(bool isEnable) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (skeletonGraphic == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | skeletonGraphic.enabled = isEnable; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|