增加A2WToolBox工具集,增加Launch场景核LaunchPanel
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MonsterLove.StateMachine;
|
||||
|
||||
public class ColorChanger : MonoBehaviour
|
||||
{
|
||||
public enum States
|
||||
{
|
||||
Blue,
|
||||
Green,
|
||||
Red
|
||||
}
|
||||
|
||||
public float interval;
|
||||
|
||||
private Camera cam;
|
||||
|
||||
private StateMachine<States> fsm;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
cam = Camera.main;
|
||||
|
||||
fsm = StateMachine<States>.Initialize(this);
|
||||
}
|
||||
|
||||
IEnumerator Blue_Enter()
|
||||
{
|
||||
cam.backgroundColor = Color.blue * 0.5f;
|
||||
yield return new WaitForSeconds(interval);
|
||||
|
||||
fsm.ChangeState(States.Red);
|
||||
}
|
||||
|
||||
IEnumerator Red_Enter()
|
||||
{
|
||||
cam.backgroundColor = Color.red * 0.5f;
|
||||
yield return new WaitForSeconds(interval);
|
||||
|
||||
fsm.ChangeState(States.Green);
|
||||
}
|
||||
|
||||
IEnumerator Green_Enter()
|
||||
{
|
||||
cam.backgroundColor = Color.green * 0.5f;
|
||||
yield return new WaitForSeconds(interval);
|
||||
|
||||
fsm.ChangeState(States.Blue);
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
if(GUI.Button(new Rect(Screen.width - 150, 50, 100, 20), "Disco Time!"))
|
||||
{
|
||||
fsm.ChangeState(States.Blue, StateTransition.Overwrite);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a57cafd734b9e8d4da74fd1b24e6107e
|
||||
timeCreated: 1457603986
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,161 @@
|
||||
using System.Collections.Generic;
|
||||
using MonsterLove.StateMachine;
|
||||
using UnityEngine;
|
||||
|
||||
public class ExampleAdvanced : MonoBehaviour
|
||||
{
|
||||
public Item prefab;
|
||||
public float roundTime;
|
||||
|
||||
private List<Item> spawnedItems;
|
||||
private Item targetItem;
|
||||
private float playStartTime;
|
||||
|
||||
private StateMachine<States, Driver> fsm;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
//Initialize the state machine
|
||||
fsm = new StateMachine<States, Driver>(this);
|
||||
fsm.ChangeState(States.Idle); //Remember to set an initial state!
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
fsm.Driver.Update.Invoke(); //Tap the state machine into Unity's update loop. We could choose to call this from anywhere though!
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
fsm.Driver.OnGUI.Invoke(); //Tap into the OnGUI update loop.
|
||||
}
|
||||
|
||||
void DestroyItem(Item item)
|
||||
{
|
||||
item.Triggered -= fsm.Driver.OnItemSelected.Invoke; //Good hygiene, always remove your listeners when you are done!
|
||||
Destroy(item.gameObject);
|
||||
}
|
||||
|
||||
#region fsm
|
||||
|
||||
public enum States
|
||||
{
|
||||
Idle,
|
||||
Play,
|
||||
GameWin,
|
||||
GameLose,
|
||||
}
|
||||
|
||||
public class Driver
|
||||
{
|
||||
public StateEvent Update;
|
||||
public StateEvent OnGUI;
|
||||
public StateEvent<Item> OnItemSelected;
|
||||
}
|
||||
|
||||
void Idle_OnGUI()
|
||||
{
|
||||
if (GUI.Button(new Rect(20, 20, 100, 30), "Begin"))
|
||||
{
|
||||
fsm.ChangeState(States.Play);
|
||||
}
|
||||
}
|
||||
|
||||
void Play_Enter()
|
||||
{
|
||||
playStartTime = Time.time;
|
||||
|
||||
int count = 10;
|
||||
int targetIndex = Random.Range(0, count);
|
||||
spawnedItems = new List<Item>(count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var pos2D = Random.insideUnitCircle * 5;
|
||||
var item = (Item) Instantiate(prefab, new Vector3(pos2D.x, 0, pos2D.y), Quaternion.identity);
|
||||
item.isTarget = (i == targetIndex);
|
||||
item.Triggered += fsm.Driver.OnItemSelected.Invoke; //Pipe external events into the fsm - this is very powerful!
|
||||
spawnedItems.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
void Play_OnGUI()
|
||||
{
|
||||
float timeRemaining = roundTime - (Time.time - playStartTime);
|
||||
GUI.Label(new Rect(20, 20, 300, 30), "Click items to find the target");
|
||||
GUI.Label(new Rect(20, 50, 300, 30), $"Time Remaining: {timeRemaining:n3}");
|
||||
}
|
||||
|
||||
void Play_Update()
|
||||
{
|
||||
if (Time.time - playStartTime >= roundTime)
|
||||
{
|
||||
fsm.ChangeState(States.GameLose);
|
||||
}
|
||||
}
|
||||
|
||||
void Play_OnItemSelected(Item item) //Data driven events guarantee correctness - only the Play state responds to OnItemSelected events
|
||||
{
|
||||
if (item.isTarget)
|
||||
{
|
||||
targetItem = item;
|
||||
fsm.ChangeState(States.GameWin);
|
||||
}
|
||||
else
|
||||
{
|
||||
spawnedItems.Remove(item);
|
||||
DestroyItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void GameWin_Enter()
|
||||
{
|
||||
spawnedItems.Remove(targetItem);
|
||||
|
||||
for (int i = spawnedItems.Count - 1; i >= 0; i--) //Reverse order as we're modifying the list in place
|
||||
{
|
||||
var item = spawnedItems[i];
|
||||
spawnedItems.Remove(item);
|
||||
DestroyItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void GameWin_OnGUI()
|
||||
{
|
||||
GUI.Label(new Rect(20, 20, 300, 30), "Well done, you found it!");
|
||||
|
||||
if (GUI.Button(new Rect(20, 50, 100, 30), "Restart"))
|
||||
{
|
||||
fsm.ChangeState(States.Idle);
|
||||
}
|
||||
}
|
||||
|
||||
void GameWin_Exit()
|
||||
{
|
||||
DestroyItem(targetItem);
|
||||
targetItem = null;
|
||||
}
|
||||
|
||||
void GameLose_Enter()
|
||||
{
|
||||
for (int i = spawnedItems.Count - 1; i >= 0; i--) //Reverse order as we're modifying the list in place
|
||||
{
|
||||
var item = spawnedItems[i];
|
||||
spawnedItems.Remove(item);
|
||||
DestroyItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void GameLose_OnGUI()
|
||||
{
|
||||
GUI.Label(new Rect(20, 20, 300, 30), "Bad luck, you didn't find it in time!");
|
||||
|
||||
if (GUI.Button(new Rect(20, 50, 100, 30), "Restart"))
|
||||
{
|
||||
fsm.ChangeState(States.Idle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c086227322664a1ab09daecc44f8e5fa
|
||||
timeCreated: 1616755006
|
||||
@@ -0,0 +1,135 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using MonsterLove.StateMachine;
|
||||
|
||||
public class ExampleBasic : MonoBehaviour
|
||||
{
|
||||
//Declare which states we'd like use
|
||||
public enum States
|
||||
{
|
||||
Init,
|
||||
Countdown,
|
||||
Play,
|
||||
Win,
|
||||
Lose
|
||||
}
|
||||
|
||||
public float health = 100;
|
||||
public float damage = 20;
|
||||
|
||||
private float startHealth;
|
||||
|
||||
private StateMachine<States, StateDriverUnity> fsm;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
startHealth = health;
|
||||
|
||||
//Initialize State Machine Engine
|
||||
fsm = new StateMachine<States, StateDriverUnity>(this);
|
||||
fsm.ChangeState(States.Init);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
fsm.Driver.Update.Invoke();
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
GUILayout.BeginArea(new Rect(50,50,120,40));
|
||||
|
||||
fsm.Driver.OnGUI.Invoke();
|
||||
|
||||
GUILayout.EndArea();
|
||||
}
|
||||
|
||||
void Init_Enter()
|
||||
{
|
||||
Debug.Log("Waiting for start button to be pressed");
|
||||
}
|
||||
|
||||
void Init_OnGUI()
|
||||
{
|
||||
if(GUILayout.Button("Start"))
|
||||
{
|
||||
fsm.ChangeState(States.Countdown);
|
||||
}
|
||||
}
|
||||
|
||||
//We can return a coroutine, this is useful animations and the like
|
||||
IEnumerator Countdown_Enter()
|
||||
{
|
||||
health = startHealth;
|
||||
|
||||
Debug.Log("Starting in 3...");
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
Debug.Log("Starting in 2...");
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
Debug.Log("Starting in 1...");
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
|
||||
fsm.ChangeState(States.Play);
|
||||
}
|
||||
|
||||
void Countdown_OnGUI()
|
||||
{
|
||||
GUILayout.Label("Look at Console");
|
||||
}
|
||||
|
||||
void Play_Enter()
|
||||
{
|
||||
Debug.Log("FIGHT!");
|
||||
}
|
||||
|
||||
void Play_Update()
|
||||
{
|
||||
health -= damage * Time.deltaTime;
|
||||
|
||||
if(health < 0)
|
||||
{
|
||||
fsm.ChangeState(States.Lose);
|
||||
}
|
||||
}
|
||||
|
||||
void Play_OnGUI()
|
||||
{
|
||||
if(GUILayout.Button("Make me win!"))
|
||||
{
|
||||
fsm.ChangeState(States.Win);
|
||||
}
|
||||
|
||||
GUILayout.Label("Health: " + Mathf.Round(health).ToString());
|
||||
}
|
||||
|
||||
void Play_Exit()
|
||||
{
|
||||
Debug.Log("Game Over");
|
||||
}
|
||||
|
||||
void Lose_Enter()
|
||||
{
|
||||
Debug.Log("Lost");
|
||||
}
|
||||
|
||||
void Lose_OnGUI()
|
||||
{
|
||||
if(GUILayout.Button("Play Again"))
|
||||
{
|
||||
fsm.ChangeState(States.Countdown);
|
||||
}
|
||||
}
|
||||
|
||||
void Win_Enter()
|
||||
{
|
||||
Debug.Log("Won");
|
||||
}
|
||||
|
||||
void Win_OnGUI()
|
||||
{
|
||||
if(GUILayout.Button("Play Again"))
|
||||
{
|
||||
fsm.ChangeState(States.Countdown);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1b8c2ce0ae416a4ebf4865c20d2d12e
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
17
Assets/A2WToolBox/3rd/StateMachine/Samples/Scripts/Item.cs
Normal file
17
Assets/A2WToolBox/3rd/StateMachine/Samples/Scripts/Item.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class Item : MonoBehaviour
|
||||
{
|
||||
public event Action<Item> Triggered;
|
||||
|
||||
public bool isTarget;
|
||||
|
||||
public void OnMouseDown()
|
||||
{
|
||||
if (Triggered != null)
|
||||
{
|
||||
Triggered(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f92323e092843a7a057ef31db09f92d
|
||||
timeCreated: 1616756619
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "MonsterLove.StateMachine.Samples",
|
||||
"references": [
|
||||
"GUID:39f9fc2c98cf6654498562b4bdd17a1e"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64e8a9da2ff291b458d3a67fda350daa
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f34f140b3438bc944beff20f0b786636
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,82 @@
|
||||
|
||||
using System;
|
||||
using MonsterLove.StateMachine;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
public class StressTest : MonoBehaviour
|
||||
{
|
||||
enum States
|
||||
{
|
||||
State0,
|
||||
State1,
|
||||
State2,
|
||||
State3,
|
||||
State4,
|
||||
State5,
|
||||
State6,
|
||||
State7,
|
||||
State8,
|
||||
State9,
|
||||
State10,
|
||||
State11,
|
||||
State12,
|
||||
State13,
|
||||
State14,
|
||||
}
|
||||
|
||||
public class Driver
|
||||
{
|
||||
public StateEvent Update;
|
||||
public StateEvent<int> OnChanged;
|
||||
}
|
||||
|
||||
private StateMachine<States, Driver> fsm;
|
||||
|
||||
private int value = 0;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
fsm = new StateMachine<States, Driver>(this);
|
||||
fsm.ChangeState(States.State1);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Profiler.BeginSample("Fsm_Invoke");
|
||||
for (int i = 0; i < 10000; i++)
|
||||
{
|
||||
fsm.Driver.Update.Invoke();
|
||||
fsm.Driver.OnChanged.Invoke(i);
|
||||
}
|
||||
Profiler.EndSample();
|
||||
|
||||
Profiler.BeginSample("Fsm_Native");
|
||||
for (int i = 0; i < 10000; i++)
|
||||
{
|
||||
State1_Update();
|
||||
State1_OnChanged(i);
|
||||
}
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
void State1_Update()
|
||||
{
|
||||
value++;
|
||||
}
|
||||
|
||||
void State1_OnChanged(int value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
void State2_Update()
|
||||
{
|
||||
value++;
|
||||
}
|
||||
|
||||
void State2_OnChanged(int value)
|
||||
{
|
||||
this.value = -value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5feefe5bbb45416ca0f4953ecfff49da
|
||||
timeCreated: 1616777700
|
||||
Reference in New Issue
Block a user