加了动画控制器

This commit is contained in:
liuliang 2024-12-12 17:06:08 +08:00
parent 2c416146ad
commit b109b30ae1
6 changed files with 266 additions and 36 deletions

8
Aniatioon.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6d134a9b58103a6438abdb8e5c36165b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

217
Aniatioon/SpriteAnimator.cs Normal file
View File

@ -0,0 +1,217 @@
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 表示一个动画序列,包括一系列精灵和每帧的显示时间。
/// </summary>
[System.Serializable]
public class AnimationSequence
{
[Tooltip("动画序列中的精灵列表")]
public List<Sprite> sprites = new List<Sprite>();
[Tooltip("每一帧的时间,单位秒")]
public float frameTime = 0.1f; // 每帧的持续时间(秒)
}
/// <summary>
/// 控制精灵动画播放的类,使用 Dictionary 进行精灵缓存管理。
/// </summary>
public class SpriteAnimator : MonoBehaviour
{
/// <summary>
/// 定义动画的循环类型。
/// </summary>
public enum LoopType
{
Loop, // 无限循环
Once, // 播放一次
PingPong // 来回播放
}
[Header("动画序列列表")]
[Tooltip("所有动画序列")]
public List<AnimationSequence> animationSequences = new List<AnimationSequence>();
[Header("SpriteRenderer组件")]
[Tooltip("用于显示动画的SpriteRenderer")]
public SpriteRenderer spriteRenderer;
[Header("当前动画编号")]
[Tooltip("当前播放的动画编号")]
public int currentAnimationIndex = 0;
[Header("循环类型")]
[Tooltip("选择动画的循环类型")]
public LoopType loopType = LoopType.Loop;
// 内部变量
private Dictionary<int, AnimationSequence> animationDictionary = new Dictionary<int, AnimationSequence>();
private AnimationSequence currentAnimation;
private int currentFrameIndex = 0;
private float frameTimer = 0f;
private bool isPlaying = true;
private bool pingPongForward = true;
/// <summary>
/// 初始化动画缓存。
/// </summary>
private void Awake()
{
if (animationSequences == null || animationSequences.Count == 0)
{
Debug.LogError("动画序列列表为空。请在 Inspector 中添加动画序列。");
return;
}
for (int i = 0; i < animationSequences.Count; i++)
{
if (!animationDictionary.ContainsKey(i))
{
animationDictionary.Add(i, animationSequences[i]);
}
else
{
Debug.LogWarning($"发现重复的动画索引 {i}。跳过重复项。");
}
}
SetAnimation(currentAnimationIndex);
}
/// <summary>
/// 在开始时进行初始化。
/// </summary>
private void Start()
{
if (spriteRenderer == null)
{
Debug.LogError("SpriteRenderer 'spriteRenderer' 为空。请在 Inspector 中分配 SpriteRenderer。");
return;
}
if (currentAnimation != null && currentAnimation.sprites.Count > 0)
{
frameTimer = currentAnimation.frameTime;
spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
}
else
{
Debug.LogError("当前动画序列为空或不包含精灵。");
}
}
/// <summary>
/// 每帧更新动画播放。
/// </summary>
private void Update()
{
if (!isPlaying || currentAnimation == null || currentAnimation.sprites.Count == 0)
return;
frameTimer -= Time.deltaTime;
if (frameTimer <= 0f)
{
AdvanceFrame();
frameTimer = currentAnimation.frameTime;
}
}
/// <summary>
/// 设置当前播放的动画序列。
/// </summary>
/// <param name="index">动画编号</param>
public void SetAnimation(int index)
{
if (animationDictionary.TryGetValue(index, out AnimationSequence selectedAnimation))
{
currentAnimation = selectedAnimation;
currentFrameIndex = 0;
pingPongForward = true;
frameTimer = currentAnimation.frameTime;
if (currentAnimation.sprites.Count > 0)
{
spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
}
else
{
Debug.LogWarning($"选中的动画索引 {index} 没有包含任何精灵。");
}
}
else
{
Debug.LogError($"SetAnimation: 动画索引 {index} 在 animationDictionary 中未找到。");
}
}
/// <summary>
/// 控制动画的播放和暂停。
/// </summary>
/// <param name="play">true: 播放动画, false: 暂停动画</param>
public void PlayPauseAnimation(bool play)
{
isPlaying = play;
if (isPlaying && currentAnimation != null && currentAnimation.sprites.Count > 0)
{
// 确保当前帧显示正确
spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
frameTimer = currentAnimation.frameTime;
}
}
/// <summary>
/// 前进到下一帧或根据循环类型处理帧索引。
/// </summary>
private void AdvanceFrame()
{
switch (loopType)
{
case LoopType.Loop:
currentFrameIndex = (currentFrameIndex + 1) % currentAnimation.sprites.Count;
break;
case LoopType.Once:
currentFrameIndex = Mathf.Min(currentFrameIndex + 1, currentAnimation.sprites.Count - 1);
if (currentFrameIndex == currentAnimation.sprites.Count - 1)
{
isPlaying = false; // 动画播放完毕后停止
}
break;
case LoopType.PingPong:
if (pingPongForward)
{
currentFrameIndex++;
if (currentFrameIndex >= currentAnimation.sprites.Count - 1)
{
currentFrameIndex = currentAnimation.sprites.Count - 1;
pingPongForward = false;
}
}
else
{
currentFrameIndex--;
if (currentFrameIndex <= 0)
{
currentFrameIndex = 0;
pingPongForward = true;
}
}
break;
}
spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
}
/// <summary>
/// 在销毁对象时清理缓存。
/// </summary>
private void OnDestroy()
{
animationDictionary.Clear();
animationSequences = null;
currentAnimation = null;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0c9ace101ec275a4e9bdbca5aa9934d8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -62,11 +62,21 @@ public class Attack : MonoBehaviour
}
public void attack(Role targetRole)
{
if (bulletPrefab==null)
{
Debug.LogError("子弹预制体为空");
return;
}
/*if(bulletPrefab.GetComponent<Bullet>().myBulletType==BulletType.Spraying)
{
return;
}
*/
Vector2 direction = (targetRole.transform.position - transform.position).normalized;
List<GameObject> bulltes= new List<GameObject>();

View File

@ -9,7 +9,9 @@ using UnityEngine.UIElements;
public enum BulletType
{
Bullet,
Lightning
Lightning,
Spraying //原地喷射
}
public enum BulletMoveType
@ -20,7 +22,8 @@ public enum BulletMoveType
/// <summary>
/// 方向移动
/// </summary>
PointToDirection
PointToDirection,
Scope
}
@ -62,10 +65,10 @@ public class Bullet : MonoBehaviour
{
[Header("子弹发射角色对象")] public Role role;
[Header("子弹发射范围对象")] public Attack attackObj;
[Header("子弹类型")]public BulletType myBulletType;
[Header("攻击类型")]public BulletMoveType bulletMoveType;
[Header("子弹数据")] public BulletData bulletData =new BulletData();
[Header("子弹类型")] public BulletType myBulletType;
[Header("攻击类型")] public BulletMoveType bulletMoveType;
[Header("子弹数据")] public BulletData bulletData = new BulletData();
[Header("销毁时间,默认是10f")] public float BulletDeadTimer=10f;
private float timer = 0;
private void Update()
{
@ -74,16 +77,19 @@ public class Bullet : MonoBehaviour
//更具子弹的移动方式来移动
case BulletMoveType.PeerToPeer:
this.gameObject.transform.Translate(Vector3.up * Time.deltaTime * bulletData.BulletSpeed);
timer += Time.deltaTime;
if (timer > BulletDeadTimer)
{
Destroy(this.gameObject);
}
break;
case BulletMoveType.PointToDirection:
break;
case BulletMoveType.Scope:
break;
}
timer += Time.deltaTime;
if (timer>10f)
{
Destroy(this.gameObject);
}
}

View File

@ -20,12 +20,6 @@ public enum DamageType
noAttributeDamage,
}
//public class prefabObjects
//{
// public string targetAudience = "targetAudience";
// public string myPalye = "myPalye";
//}
public class BUff
{
@ -39,25 +33,7 @@ public class BUff
public class Fun : Base
{
//// 通过方法名调用对应的方法
//public void CallMethodByName(string methodName)
//{
// // 获取当前类的Type
// Type type = this.GetType();
// // 获取方法信息
// MethodInfo methodInfo = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
// if (methodInfo != null)
// {
// // 调用该方法
// methodInfo.Invoke(this,null ); // null代表无参数
// }
// else
// {
// Debug.LogError("方法未找到: " + methodName);
// }
//}
/// <summary>
/// 中毒 //目标 // 自己
/// </summary>
@ -69,6 +45,8 @@ public class Fun : Base
Debug.Log("触发中毒");
Debug.Log(myPalye.name);
}
/// <summary>
/// 扣血 传入参数 目标,伤害值,攻击类型,使用者
/// </summary>