//获取角色动画参数 public AnimationControlParamter GetAnimationParamter(string animationName) { AnimationControlParamter paramter = new AnimationControlParamter(); AnimationClip[] clips = anim.runtimeAnimatorController.animationClips; for (int i = 0; i < clips.Length; i++) { if (clips[i].name == animationName) { if (clips[i].wrapMode == WrapMode.Loop) { paramter.bLoop = true; } else { paramter.bLoop = false; } break; } } UnityEditor.Animations.AnimatorController ac = anim.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; //获取状态机 UnityEditor.Animations.AnimatorStateMachine asm = ac.layers[0].stateMachine; for (int i = 0; i < asm.states.Length; i++) { UnityEditor.Animations.ChildAnimatorState cs = asm.states[i]; if (cs.state.name == animationName) { paramter.timeScale = cs.state.speed; break; } } return(paramter); }
//设置角色动画参数 public void SetAnimationParamter(string animationName, AnimationControlParamter paramter) { //获取所有动画片段 AnimationClip[] clips = anim.runtimeAnimatorController.animationClips; for (int i = 0; i < clips.Length; i++) { if (clips[i].name == animationName) { if (paramter.bLoop) { clips[i].wrapMode = WrapMode.Loop; } else { clips[i].wrapMode = WrapMode.Default; } } } UnityEditor.Animations.AnimatorController ac = anim.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; //获取状态机 UnityEditor.Animations.AnimatorStateMachine asm = ac.layers[0].stateMachine; for (int i = 0; i < asm.states.Length; i++) { UnityEditor.Animations.ChildAnimatorState cs = asm.states[i]; if (cs.state.name == animationName) { cs.state.speed = paramter.timeScale; } } }
//改变攻击速度 public void changeAttackSpeed(float aspd) { attackSpeed = aspd; AnimationControlParamter acp = GetAnimationParamter("AttackNormal"); acp.timeScale = aspd; SetAnimationParamter("AttackNormal", acp); }
//改变移动速度 public void changeSpeed(float spd) { speed = spd; agent.speed = spd; AnimationControlParamter acp = GetAnimationParamter("Run"); acp.timeScale = speed / defaultData.speed; SetAnimationParamter("Run", acp); }
public override void run() { if (m_caster.GetState() != ActorState.DEAD) { //这里设置使用当前技能槽的索引,更新状态时会做判断 if (m_targetUnit != null) { m_caster.transform.LookAt(new Vector3(m_targetUnit.transform.position.x, m_caster.transform.position.y, m_targetUnit.transform.position.z)); m_caster.agent.SetDestination(m_targetUnit.transform.position); } else { return; } BaseSkill skill = null; m_caster.GetSkill(m_skillIndex, ref skill); if (skill == null) { return; } if (skill.attributes.cost > m_caster.mana) { Debug.Log(m_caster.creatureName + "魔法值不够!"); return; } m_caster.mana -= skill.attributes.cost; m_caster.SetCurUseSkillIndex(m_skillIndex); AnimationControlParamter paramter = m_caster.GetAnimationParamter("CastSpell"); if (paramter != null) { float AniTimeLen = m_caster.GetAnimationLength("CastSpell"); Debug.Log("施法动作长度" + AniTimeLen); float time = skill.attributes.castTime; if (time < Global.MinCastSpellTime) { time = Global.MinCastSpellTime; } paramter.timeScale = AniTimeLen / time; m_caster.SetAnimationParamter("CastSpell", paramter); } else { Debug.Log("没找到CastSpell动画参数!"); } skill.targetUnit = m_targetUnit.GetComponent <Actor>(); if (!skill.VerifyAllowType()) { return; } skill.CastSkill(); m_caster.SetState(new CastSpellActorState(m_caster)); } m_isRunning = true; }
public override void run() { if (m_caster.GetState() != ActorState.DEAD) { Debug.Log(m_caster.creatureName + "使用了一个无目标的技能"); BaseSkill skill = null; m_caster.GetSkill(m_skillIndex, ref skill); if (skill == null) { return; } if (skill.attributes.cost > m_caster.mana) { Debug.Log(m_caster.creatureName + "魔法值不够!"); return; } m_caster.mana -= skill.attributes.cost; m_caster.SetCurUseSkillIndex(m_skillIndex); AnimationControlParamter paramter = m_caster.GetAnimationParamter("CastSpell"); if (paramter != null) { float AniTimeLen = m_caster.GetAnimationLength("CastSpell"); Debug.Log("施法动作长度" + AniTimeLen); float time = skill.attributes.castTime; if (time < Global.MinCastSpellTime) { time = Global.MinCastSpellTime; } paramter.timeScale = AniTimeLen / time; m_caster.SetAnimationParamter("CastSpell", paramter); } else { Debug.Log("没找到CastSpell动画参数!"); } if (!skill.VerifyAllowType()) { return; } skill.CastSkill(); m_caster.SetState(new CastSpellActorState(m_caster)); } this.m_isRunning = true; }
// Use this for initialization public void Start() { targetUnit = null; curUseSkillIndex = -1; curSkillSlot = -1; state = ActorState.IDLE; agent.updateRotation = true; loadAsset(assetName); SetState(new IdleActorState(this)); //添加事件Handler BeginAttackHandler += OnBeginAttack; BeDamagedHandler += OnBeDamaged; RemoveBuffHandler += OnRemoveBuff; BeginMoveHandler += OnBeginMove; StopMoveHandler += OnStopMove; UnitDieHandler += OnDead; m_cmdMgr = new CommandManager(); defaultAnimationParamters = new Hashtable(); //将初始的动画参数保存为默认参数,以便修改后好还原 AnimationClip[] clips = anim.runtimeAnimatorController.animationClips; for (int i = 0; i < clips.Length; i++) { AnimationControlParamter parameter = GetAnimationParamter(clips[i].name); defaultAnimationParamters[clips[i].name] = parameter; } //设置技能 for (int i = 0; i < skillIDList.Count; i++) { int skillId = skillIDList[i]; if (Global.SkillData.ContainsKey(skillId)) { SetSkill(i, skillId); } } changeSpeed(speed); }