| | |
| | | } |
| | | } |
| | | |
| | | private Bone followedBone; |
| | | |
| | | public bool isBindBone = false; |
| | | |
| | | public bool isRedCamp = true; |
| | | public EffectConfig effectConfig; |
| | | |
| | | public float speedRate = 1.5f; |
| | | |
| | | private float m_Alpha = 1f; |
| | | |
| | | public float scaleFactor = 1f;// 长期为1,只在需要跟模型一起缩放的时候更改 值为teamHero.ModelScale |
| | | |
| | | public float Alpha |
| | | { |
| | | get |
| | | { |
| | | return m_Alpha; |
| | | } |
| | | set |
| | | { |
| | | if (value == m_Alpha) |
| | | return; |
| | | |
| | | m_Alpha = value; |
| | | OnAlphaChanged(); |
| | | } |
| | | } |
| | | |
| | | private void OnAlphaChanged() |
| | | { |
| | | if (spineComp != null) |
| | | { |
| | | var skeleton = spineComp.Skeleton; |
| | | skeleton.A = Alpha; |
| | | spineComp.LateUpdate(); |
| | | } |
| | | |
| | | for (int i = 0; i < rendererList.Count; i++) |
| | | { |
| | | var renderer = rendererList[i]; |
| | | if (renderer != null && renderer.material != null && renderer.material.HasProperty("_Color")) |
| | | { |
| | | Color color = renderer.material.color; |
| | | color.a = Alpha; |
| | | renderer.material.color = color; |
| | | } |
| | | } |
| | | } |
| | | |
| | | [Header("播放完毕立即回收")] |
| | | public bool isReleaseImmediately = false; //界面特效一般不需要自我销毁,跟随界面或者父对象销毁就行 |
| | |
| | | protected SkeletonAnimation spineComp; |
| | | protected Spine.AnimationState spineAnimationState; |
| | | |
| | | protected int heroSetedSortingOrder; |
| | | |
| | | public GameObjectPoolManager.GameObjectPool pool; |
| | | |
| | | public BattleField battleField; |
| | | |
| | | public Action onComplete; |
| | | |
| | |
| | | |
| | | protected virtual void OnEnable() |
| | | { |
| | | if (spineComp != null) |
| | | { |
| | | //隐藏,会有静态显示问题 |
| | | spineComp.enabled = false; |
| | | } |
| | | // if (spineComp != null) |
| | | // { |
| | | // //隐藏,会有静态显示问题 |
| | | // spineComp.enabled = false; |
| | | // } |
| | | |
| | | ApplySortingOrder(); |
| | | } |
| | | |
| | | |
| | |
| | | // 有特效可能带spine又带unity特效的情况 |
| | | spineComp = gameObject.GetComponentInChildren<SkeletonAnimation>(true); |
| | | |
| | | |
| | | |
| | | if (effectConfig.effectPos != null && effectConfig.effectPos.Length >= 2) |
| | | { |
| | | rectTrans.anchoredPosition += new Vector2((isRedCamp ? 1f : -1f) * effectConfig.effectPos[0], effectConfig.effectPos[1]); |
| | |
| | | { |
| | | rectTrans.localScale *= effectConfig.effectScale; |
| | | } |
| | | |
| | | |
| | | spineComp.loop = effectConfig.isLoop != 0; |
| | | } |
| | | |
| | | protected virtual void Clear() |
| | |
| | | onComplete?.Invoke(); |
| | | } |
| | | |
| | | public void SetSortingOrder(int _heroSetedSortingOrder) |
| | | public Func<bool> funcIsHeroFront; |
| | | |
| | | public void SetSortingOrder(Func<bool> _isHeroFrontCallback) |
| | | { |
| | | heroSetedSortingOrder = _heroSetedSortingOrder; |
| | | funcIsHeroFront = _isHeroFrontCallback; |
| | | |
| | | int so = heroSetedSortingOrder; |
| | | ApplySortingOrder(); |
| | | } |
| | | |
| | | if (null != blocker && effectConfig != null) |
| | | |
| | | public void ApplySortingOrder() |
| | | { |
| | | if (null != blocker && effectConfig != null && funcIsHeroFront != null) |
| | | { |
| | | if (BattleConst.UnactiveHeroSortingOrder == heroSetedSortingOrder) |
| | | { |
| | | so = effectConfig.frontBack == 1 ? BattleConst.UnactiveHeroFrontSortingOrder : BattleConst.UnactiveHeroBackSortingOrder; |
| | | } |
| | | else |
| | | { |
| | | so = effectConfig.frontBack == 1 ? BattleConst.ActiveHeroFrontSortingOrder : BattleConst.ActiveHeroBackSortingOrder; |
| | | } |
| | | bool isEffectFront = effectConfig.frontBack == 1; |
| | | |
| | | blocker.SetSortingOrder(so); |
| | | bool isHeroFront = funcIsHeroFront(); |
| | | |
| | | int finalSortingOrder = isHeroFront ? |
| | | (isEffectFront ? BattleConst.ActiveHeroActionSortingOrder + 1 : BattleConst.ActiveHeroBackSortingOrder) : (isEffectFront ? BattleConst.UnactiveHeroFrontSortingOrder : BattleConst.UnactiveHeroBackSortingOrder); |
| | | |
| | | |
| | | blocker.SetSortingOrder(finalSortingOrder); |
| | | } |
| | | } |
| | | |
| | | public void FollowBoneXY() |
| | | { |
| | | if (followedBone == null || !isBindBone) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | Vector2 vector2 = Vector2.zero; |
| | | |
| | | if (effectConfig.effectPos != null && effectConfig.effectPos.Length >= 2) |
| | | { |
| | | vector2 = new Vector2((isRedCamp ? 1f : -1f) * effectConfig.effectPos[0], effectConfig.effectPos[1]); |
| | | } |
| | | |
| | | if (spineComp != null) |
| | | { |
| | | spineComp.transform.localPosition = new Vector3(followedBone.WorldX + vector2.x, followedBone.WorldY + vector2.y, 0); |
| | | } |
| | | |
| | | if (effectTarget != null) |
| | | { |
| | | effectTarget.transform.localPosition = new Vector3(followedBone.WorldX + vector2.x, followedBone.WorldY + vector2.y, 0); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | PlayUnityEffect(); |
| | | } |
| | | |
| | | battleField?.soundManager.PlayEffectSound(effectConfig.audio); |
| | | |
| | | OnAlphaChanged(); |
| | | } |
| | | |
| | | protected void PlaySpineEffect() |
| | | { |
| | | // 这里是纯spine的逻辑 |
| | | |
| | | if (spineComp == null) |
| | | { |
| | | Debug.LogError("BattleEffectPlayer spineComp is null, effect id is " + effectId); |
| | | return; |
| | | } |
| | | |
| | | SkeletonDataAsset skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("UIEffect/" + effectConfig.packageName, effectConfig.fxName); |
| | | // ===== 简化:直接从缓存获取,缓存内部会自动加载 ===== |
| | | string directory = "UIEffect/" + effectConfig.packageName; |
| | | SkeletonDataAsset skeletonDataAsset = BattleResManager.Instance.GetSpineResource( |
| | | directory, |
| | | effectConfig.fxName, |
| | | battleField?.guid |
| | | ); |
| | | |
| | | if (skeletonDataAsset == null) |
| | | { |
| | | Debug.LogError($"BattleEffectPlayer: Failed to load effect spine {effectConfig.fxName}"); |
| | | return; |
| | | } |
| | | // ================================ |
| | | |
| | | spineComp.skeletonDataAsset = skeletonDataAsset; |
| | | spineComp.Initialize(true); |
| | | spineComp.timeScale = speedRate; |
| | | |
| | | if (scaleX != 0f && spineComp != null && spineComp.skeleton != null) |
| | | { |
| | | spineComp.skeleton.ScaleX = scaleX; |
| | | } |
| | | |
| | | spineComp.skeleton.A = Alpha; |
| | | |
| | | spineAnimationState = spineComp.state; |
| | | spineAnimationState.Complete -= OnSpineAnimationComplete; |
| | |
| | | |
| | | blocker.onSortingChanged = OnSortingChanged; |
| | | |
| | | if (0 != heroSetedSortingOrder) |
| | | { |
| | | if (BattleConst.UnactiveHeroSortingOrder == heroSetedSortingOrder) |
| | | { |
| | | heroSetedSortingOrder = effectConfig.frontBack == 1 ? BattleConst.UnactiveHeroFrontSortingOrder : BattleConst.UnactiveHeroBackSortingOrder; |
| | | } |
| | | else |
| | | { |
| | | heroSetedSortingOrder = effectConfig.frontBack == 1 ? BattleConst.ActiveHeroFrontSortingOrder : BattleConst.ActiveHeroBackSortingOrder; |
| | | } |
| | | |
| | | blocker.SetSortingOrder(heroSetedSortingOrder); |
| | | } |
| | | ApplySortingOrder(); |
| | | |
| | | spineComp.enabled = true; |
| | | |
| | | spineComp.timeScale = speedRate; |
| | | spineAnimationState.TimeScale = speedRate; |
| | | |
| | | spineComp.LateUpdate(); |
| | | |
| | | |
| | | Spine.Animation animation = spineAnimationState.Data.SkeletonData.Animations.First(); |
| | | spineAnimationState.SetAnimation(0, animation, effectConfig.isLoop != 0); |
| | | |
| | | SoundPlayer.Instance.PlayUIAudio(effectConfig.audio); |
| | | // SoundPlayer.Instance.PlayUIAudio(effectConfig.audio); |
| | | |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // 从特效预制体池获取特效 |
| | | pool = GameObjectPoolManager.Instance.RequestPool(effectPrefab); |
| | | pool = GameObjectPoolManager.Instance.GetPool(effectPrefab); |
| | | effectTarget = pool.Request(); |
| | | // 设置父节点和位置 |
| | | effectTarget.transform.SetParent(transform); |
| | |
| | | |
| | | blocker.onSortingChanged = OnSortingChanged; |
| | | |
| | | SoundPlayer.Instance.PlayUIAudio(effectConfig.audio); |
| | | // SoundPlayer.Instance.PlayUIAudio(effectConfig.audio); |
| | | |
| | | } |
| | | |
| | |
| | | |
| | | |
| | | // 创建后的特效会自动隐藏 需要手动调用Play才能播放 |
| | | public static BattleEffectPlayer Create(int effectId, Transform parent, bool isRedCamp) |
| | | public static BattleEffectPlayer Create(int effectId, BattleField _battleField, Transform parent, bool isRedCamp, float _scaleRatio) |
| | | { |
| | | // 直接创建特效播放器,不使用对象池 |
| | | BattleEffectPlayer battleEffectPlayer = null; |
| | |
| | | GameObject newGo = new GameObject("BattleEffectPlayer_" + effectId); |
| | | newGo.transform.SetParent(parent, false); |
| | | battleEffectPlayer = newGo.AddComponent<BattleEffectPlayer>(); |
| | | battleEffectPlayer.battleField = _battleField; |
| | | battleEffectPlayer.rectTrans = newGo.AddMissingComponent<RectTransform>(); |
| | | |
| | | battleEffectPlayer.effectId = effectId; |
| | | |
| | | battleEffectPlayer.scaleFactor = _scaleRatio; |
| | | |
| | | // 设置阵营 |
| | | battleEffectPlayer.isRedCamp = isRedCamp; |
| | |
| | | spineComp.timeScale = 0f; |
| | | } |
| | | |
| | | if (spineAnimationState != null) |
| | | { |
| | | spineAnimationState.TimeScale = 0f; |
| | | } |
| | | |
| | | // Animator动画 |
| | | foreach (var animator in animatorList) |
| | | { |
| | |
| | | if (spineComp != null) |
| | | { |
| | | spineComp.timeScale = speedRate; |
| | | } |
| | | |
| | | if (spineAnimationState != null) |
| | | { |
| | | spineAnimationState.TimeScale = speedRate; |
| | | } |
| | | |
| | | // Animator动画 |
| | |
| | | animator.speed = speedRate; |
| | | } |
| | | } |
| | | |
| | | public void BindBone(SkeletonAnimation skeletonAnim, string v) |
| | | { |
| | | Bone bone = skeletonAnim.skeleton.FindBone(v); |
| | | isBindBone = false; |
| | | followedBone = null; |
| | | if (null == bone) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | rectTrans.localScale *= scaleFactor; |
| | | |
| | | isBindBone = true; |
| | | followedBone = bone; |
| | | |
| | | BoneFollower boneFollower = gameObject.AddMissingComponent<BoneFollower>(); |
| | | boneFollower.boneName = v; |
| | | boneFollower.skeletonRenderer = skeletonAnim; |
| | | |
| | | boneFollower.followBoneRotation = false; |
| | | boneFollower.followXYPosition = true; |
| | | boneFollower.followZPosition = false; |
| | | boneFollower.followLocalScale = false; |
| | | boneFollower.followParentWorldScale = false; |
| | | boneFollower.followSkeletonFlip = false; |
| | | |
| | | boneFollower.Initialize(); |
| | | boneFollower.LateUpdate(); |
| | | } |
| | | |
| | | private float scaleX = 0f; |
| | | |
| | | public void SetScaleX(float v) |
| | | { |
| | | if (spineComp != null && spineComp.skeleton != null) |
| | | { |
| | | spineComp.skeleton.ScaleX = v; |
| | | } |
| | | else |
| | | { |
| | | scaleX = v; |
| | | } |
| | | } |
| | | } |