赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
-
- public class Player : MonoBehaviour
- {
- #region 定义Unity组件
- public Animator anim { get; private set; }//这样才能配合着拿到自己身上的animator的控制权
- #endregion
- #region 定义States
- public PlayerStateMachine stateMachine { get; private set; }
- public PlayerIdleState idleState { get; private set; }
- public PlayerMoveState moveState { get; private set; }
-
- #endregion
- private void Awake()
- {
- stateMachine = new PlayerStateMachine();
- //通过构造函数,在构造时传递信息
- idleState = new PlayerIdleState(this, stateMachine, "Idle");
- moveState = new PlayerMoveState(this, stateMachine, "Move");
- //this 就是 Player这个类本身
- }//Awake初始化所以State,为所有State传入各自独有的参数,及animBool,以判断是否调用此动画(与animatoin配合完成)
- private void Start()
- {
- anim = GetComponentInChildren<Animator>();//拿到自己身上的animator的控制权
- stateMachine.Initialize(idleState);
- }
- private void Update()//在mano中update会自动刷新但其他没有mano的不会故,需要在这个updata中调用其他脚本中的函数stateMachine.currentState.update以实现 //stateMachine中的update
-
- {
- stateMachine.currentState.Update();//反复调用CurrentState的Update函数
- }

- using System.Collections;
- using System.Collections.Generic;
- using System.Security.Authentication.ExtendedProtection;
- using UnityEngine;
- //被继承的总类,后面的所以state都需要继承它
- //实际上Player并没有进入过这个状态,没有调用此脚本,所有的调用均属于此脚本的子脚本
- public class PlayerState
- {
- protected PlayerStateMachine stateMachine;//创建PlayerStateMachine类,以对其进行控制
- protected Player player;//创建Player类,以对其进行控制
- private string animBoolName;//控制此时的anim的BOOL值
- public PlayerState(Player _player,PlayerStateMachine _stateMachine,string _animBoolName)
- {
- this.player = _player;
- this.stateMachine = _stateMachine;
- this.animBoolName = _animBoolName;
- }//构造函数,用于传递信息
-
- public virtual void Enter()
- {
- player.anim.SetBool(animBoolName, true);//通过animator自带的函数,将animator里的Bool值改变为想要的值
- }
- public virtual void Update()
- {
-
-
- }
- public virtual void Exit()
- {
- player.anim.SetBool(animBoolName, false);//通过animator自带的函数,将animator里的Bool值改变为想要的值
- }
-
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PlayerStateMachine
- {
- public PlayerState currentState { get; private set; }
- public void Initialize(PlayerState _startState)
- {
- currentState = _startState;
- currentState.Enter();
- }//初始化状态函数,及通过将idleState传送进来,以便于调用idleState中的Enter函数
-
- public void ChangeState(PlayerState _newState)
- {
- currentState.Exit();
- currentState = _newState;
- currentState.Enter();
- }//更改状态函数
- //1.调用当前状态的Exit函数,使动画为false
- //2.传入新的状态,替换原来的状态
- //3.调用新的状态的Enter函数
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PlayerIdleState : PlayerState//继承函数,获得PlayerState里的所有函数和参数
- {
- public PlayerIdleState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
- {
- }//构造函数,用于传递信息。
- //当外补New出对象时,New出的对象里传入参数
-
- public override void Enter()
- {
-
- base.Enter();
- }
-
- public override void Exit()
- {
- base.Exit();
- }
-
- public override void Update()
- {
- base.Update();
- if(Input.GetKeyDown(KeyCode.N))
- {
- stateMachine.ChangeState(player.moveState);//在这里我们能使用Player里的东西,主要是因为我们通过构造函数传过来Player实体,如果不传,则无法使用已经存在了的Player实体。
- }
- }
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PlayerMoveState : PlayerState
- {
- public PlayerMoveState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
- {
- }//构造函数,用于传递信息。
- //当外补New出对象时,New出的对象里传入参数
-
- public override void Enter()
- {
- base.Enter();
- }
-
- public override void Exit()
- {
- base.Exit();
- }
-
- public override void Update()
- {
- base.Update();
- if (Input.GetKeyDown(KeyCode.N))
- {
- stateMachine.ChangeState(player.idleState);//在这里我们能使用Player里的东西,主要是因为我们通过构造函数传过来Player实体,如果不传,则无法使用已经存在了的Player实体。
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。