[Unity基础]一个简单的状态机
2015-05-26 19:12
260 查看
状态机一般情况下只有两个类:状态基类以及管理状态的类。
状态机主要用于AI或者场景切换(不同场景对应的就是游戏的不同状态),当然,这只是本人的想法。
当编写具体的一个状态时,可以这样:
测试代码:
效果图:
状态机主要用于AI或者场景切换(不同场景对应的就是游戏的不同状态),当然,这只是本人的想法。
using UnityEngine; using System.Collections; public abstract class State { public int ID { set; get; } public Transform TargetTra { set; get; } public StateMachine StateMac { set; get; } public virtual void Enter() { } public virtual void Execute() { } public virtual void Exit() { } }
using UnityEngine; using System.Collections; using System.Collections.Generic; public class StateMachine { public Dictionary<int, State> dictionary = new Dictionary<int,State>(); public State CurrentState { set; get; } public State GetState(int id) { if (dictionary.ContainsKey(id)) return dictionary[id]; else return null; } public void AddState(State state) { int id = state.ID; if (!dictionary.ContainsKey(id)) dictionary.Add(id, state); } public void RemoveState(int id) { if (dictionary.ContainsKey(id)) dictionary.Remove(id); } public void InitState(int id) { if (CurrentState == null) { CurrentState = dictionary[id]; CurrentState.Enter(); } } public void ChangeState(int id) { if (CurrentState.ID != id) { CurrentState.Exit(); CurrentState = dictionary[id]; CurrentState.Enter(); } } public void ExecuteState() { CurrentState.Execute(); } public bool CheckState(int id) { return CurrentState.ID == id; } }
当编写具体的一个状态时,可以这样:
using UnityEngine; using System.Collections; public class RotateState : State { public RotateState(Transform targetTra, StateMachine sm) { ID = (int)CharacterState.rotate; TargetTra = targetTra; StateMac = sm; } public override void Enter() { base.Enter(); Debug.Log("进入旋转状态"); TargetTra.GetComponent<MeshRenderer>().material.color = Color.green; } public override void Execute() { base.Execute(); TargetTra.transform.Rotate(Vector3.up); } public override void Exit() { base.Exit(); Debug.Log("退出旋转状态"); TargetTra.transform.localScale = new Vector3(2, 2, 2); } }
测试代码:
using UnityEngine; using System.Collections; public class Player : MonoBehaviour { StateMachine stateMachine = new StateMachine(); void Start () { stateMachine.AddState(new RotateState(transform, stateMachine)); stateMachine.AddState(new RunState(transform, stateMachine)); stateMachine.InitState(((int)CharacterState.rotate)); } void Update () { stateMachine.ExecuteState(); } void OnGUI() { if (GUILayout.Button("旋转状态")) { stateMachine.ChangeState((int)CharacterState.rotate); } if (GUILayout.Button("行走状态")) { stateMachine.ChangeState((int)CharacterState.run); } } }
效果图:
相关文章推荐
- 【个人Unity笔记】{基础} 2D小游戏一个简单的自动刷怪脚本
- Win32编程API 基础篇 -- 2.一个简单的窗口 根据英文教程翻译
- 三、JMeter基础之----- 一个简单的性能测试
- Prism安装、MVVM基础概念及一个简单的样例
- unity 一个简单的有限状态机
- SpringMVC基础——一个简单的例子
- [unity基础教程]史上最简单效果最好的unity3D分辨率适应教程!(版本已经更新2013.5.3
- JMeter基础之一 一个简单的性能测试
- Unity中如何利用一个协程和www类实现简单的图片下载
- dom4j-2.1基础 addElement 添加操作-在最后一个指定复杂元素中添加一个简单元素
- 基础学习笔记之opencv(21):一个简单有趣的皮肤检测代码
- Java基础查漏补缺:(String篇)一个面试题问倒了我,原来String并不简单
- JMeter基础之一 一个简单的性能测试
- python---》客户端与服务端的基础(做一个简单的客户端与服务端)
- Android 一个判断网络连接状态的简单方法
- 使用Unity制作一个简单的聊天室
- Apache Storm 的安装、配置及入门基础(三):一个简单的 topology
- Magento入门基础 - 在magento后台增加一个自定义订单状态
- 单片机基础强化第一课 简单设计一个LED流水灯
- JMeter基础之一 一个简单的性能测试