using System; using System.Collections; using System.Collections.Generic; using Spine.Unity; using UnityEngine; using Spine; using UnityEngine.UI; //UI特效播放器,spine特效直接加载无需提前做预制体 //UI特效大部分情况不会改变特效,无需销毁 public class UIEffectPlayer : EffectPlayer { [Header("是否循环播放spine特效")] public bool isPlaySpineLoop = false; [Header("是否在显示时播放")] public bool isPlayOnEnable = false; protected override void OnEnable() { if (isPlayOnEnable) { Play(false); } else if (spineComp != null) { //隐藏,会有静态显示问题 spineComp.enabled = false; } } public override void Play(bool showLog = true) { if (!isInit) { InitComponent(showLog); isInit = true; } else { //避免重复创建 if (!this.gameObject.activeSelf) { this.gameObject.SetActive(true); } return; } if (EffectMgr.IsNotShowBySetting(effectId)) { return; } if (null != effectTarget) { if (pool != null) pool.Release(effectTarget); effectTarget = null; } if (!this.gameObject.activeSelf) { this.gameObject.SetActive(true); } // 加载spine特效资源 if (effectConfig.isSpine != 0) { PlaySpineEffect(); } else { PlayerEffect(false); } } protected override void PlaySpineEffect() { if (spineComp.skeletonDataAsset == null) { //LoadAsset 已经有缓存SkeletonDataAsset spineComp.skeletonDataAsset = ResManager.Instance.LoadAsset("UIEffect/" + effectConfig.packageName, effectConfig.fxName + "_SkeletonData"); spineComp.raycastTarget = false; spineComp.Initialize(true); spineAnimationState = spineComp.AnimationState; spineAnimationState.Complete -= OnSpineAnimationComplete; spineAnimationState.Complete += OnSpineAnimationComplete; } spineComp.enabled = true; PlayerFistSpineAnim(); } // 播放第一个动画(作为默认动画) void PlayerFistSpineAnim() { spineComp.enabled = true; var skeletonData = spineComp.Skeleton.Data; if (skeletonData.Animations.Count > 0) { string defaultAnimationName = skeletonData.Animations.Items[0].Name; spineComp.AnimationState.SetAnimation(0, defaultAnimationName, isPlaySpineLoop); } else { Debug.LogError("Spine 数据中没有找到任何动画!" + effectConfig.id); } } //单次播放完毕就会触发,即使是循环 protected override void OnSpineAnimationComplete(Spine.TrackEntry trackEntry) { if (!isPlaySpineLoop) { spineComp.enabled = false; if (isReleaseImmediately) { Stop(); } else { onComplete?.Invoke(); } } } // 创建后的特效会自动隐藏 需要手动调用Play才能播放 public static UIEffectPlayer CreateEffect(int effectId, Transform parent, bool createNewChild = false) { UIEffectPlayer effectPlayer = null; if (createNewChild) { GameObject newGo = new GameObject("EffectPlayer_" + effectId); newGo.transform.SetParent(parent, false); effectPlayer = newGo.AddComponent(); } else { effectPlayer = parent.AddMissingComponent(); effectPlayer.effectId = effectId; } effectPlayer.SetActive(true); return effectPlayer; } }