提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
unity开发者模式__状态模式
state目录下的脚本就是我们在不改变整体项目结构的情况下可以添加自己想要的状态
** 接下来看看这几个脚本分别代表什么,其中是如何去构建的**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface Istate
{
void Way();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Context : MonoBehaviour
{
private Istate curState;
public void SetState(Istate state)
{
curState = state;
}
public void Show()
{
curState.Way();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Run : Istate
{
private Context _context;
public Run(Context c)
{
_context = c;
}
public void Way()
{
_context.SetState(new Walk(_context));
Debug.Log("当前状态切换到------>走");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Walk : Istate
{
private Context _context;
public Walk(Context c)
{
_context = c;
}
public void Way()
{
_context.SetState(new Run(_context));
Debug.Log("切换状态到----->跑");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main: MonoBehaviour
{
void Start()
{
Context context = new Context();
context.SetState(new Walk(context));
context.Show();
context.SetState(new Run(context));
context.Show();
}
}
输出后的结果:
|