Browse Source

Added Event Handler system.

Can set the ‘Execute On Event’ property to define when a sequence
starts executing.
Added a ‘Start Game’ event handler.
Removed old system of specifying a start sequence on the Fungus Script.
master
chrisgregan 10 years ago
parent
commit
e6000eb33b
  1. 143
      Assets/Examples/TheHunter/TheHunter.unity
  2. 7
      Assets/Fungus/FungusScript/Commands/RunScript.cs
  3. 2
      Assets/Fungus/FungusScript/Editor/CommandEditor.cs
  4. 2
      Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs
  5. 60
      Assets/Fungus/FungusScript/Editor/EventHandlerEditor.cs
  6. 8
      Assets/Fungus/FungusScript/Editor/EventHandlerEditor.cs.meta
  7. 35
      Assets/Fungus/FungusScript/Editor/FungusScriptEditor.cs
  8. 1
      Assets/Fungus/FungusScript/Editor/FungusScriptMenuItems.cs
  9. 16
      Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs
  10. 95
      Assets/Fungus/FungusScript/Editor/SequenceEditor.cs
  11. 2
      Assets/Fungus/FungusScript/Editor/SequenceInspector.cs
  12. 5
      Assets/Fungus/FungusScript/EventHandlers.meta
  13. 18
      Assets/Fungus/FungusScript/EventHandlers/StartGame.cs
  14. 8
      Assets/Fungus/FungusScript/EventHandlers/StartGame.cs.meta
  15. 50
      Assets/Fungus/FungusScript/Scripts/EventHandler.cs
  16. 8
      Assets/Fungus/FungusScript/Scripts/EventHandler.cs.meta
  17. 93
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  18. 8
      Assets/Fungus/FungusScript/Scripts/Sequence.cs

143
Assets/Examples/TheHunter/TheHunter.unity

@ -985,6 +985,7 @@ MonoBehaviour:
width: 100
height: 40
sequenceName: New Sequence
eventHandler: {fileID: 0}
commandList: []
--- !u!4 &330427400
Transform:
@ -1474,6 +1475,7 @@ MonoBehaviour:
width: 100
height: 20
sequenceName: New Sequence
eventHandler: {fileID: 0}
commandList: []
--- !u!4 &518300788
Transform:
@ -1695,6 +1697,7 @@ MonoBehaviour:
width: 120
height: 30
sequenceName: Check for fish
eventHandler: {fileID: 0}
commandList:
- {fileID: 541676553}
- {fileID: 541676556}
@ -2672,6 +2675,7 @@ MonoBehaviour:
width: 240
height: 68
sequenceName: New Sequence
eventHandler: {fileID: 0}
commandList: []
--- !u!114 &891057087
MonoBehaviour:
@ -2691,6 +2695,7 @@ MonoBehaviour:
width: 240
height: 68
sequenceName: New Sequence
eventHandler: {fileID: 0}
commandList: []
--- !u!114 &891057088
MonoBehaviour:
@ -2716,13 +2721,10 @@ MonoBehaviour:
selectedSequence: {fileID: 891057086}
selectedCommands: []
variables: []
startSequence: {fileID: 891057087}
executeOnStart: 1
settings:
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
--- !u!4 &891057089
Transform:
m_ObjectHideFlags: 1
@ -2789,6 +2791,7 @@ MonoBehaviour:
width: 120
height: 30
sequenceName: Bear
eventHandler: {fileID: 0}
commandList:
- {fileID: 899740689}
- {fileID: 899740688}
@ -3492,11 +3495,12 @@ MonoBehaviour:
m_EditorClassIdentifier:
nodeRect:
serializedVersion: 2
x: 106.621155
x: 105.621155
y: -319.003174
width: 120
height: 30
sequenceName: Rescued
eventHandler: {fileID: 0}
commandList:
- {fileID: 1196017816}
- {fileID: 1196017815}
@ -3813,6 +3817,7 @@ MonoBehaviour:
width: 240
height: 68
sequenceName: New Sequence
eventHandler: {fileID: 0}
commandList: []
--- !u!114 &1247920061
MonoBehaviour:
@ -3838,13 +3843,10 @@ MonoBehaviour:
selectedSequence: {fileID: 1247920060}
selectedCommands: []
variables: []
startSequence: {fileID: 1247920060}
executeOnStart: 1
settings:
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
--- !u!4 &1247920062
Transform:
m_ObjectHideFlags: 1
@ -4286,6 +4288,22 @@ CanvasGroup:
m_Interactable: 1
m_BlocksRaycasts: 1
m_IgnoreParentGroups: 0
--- !u!115 &1433226136
MonoScript:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name:
serializedVersion: 4
m_Script:
m_DefaultReferences: {}
m_Icon: {fileID: 0}
m_EditorGraphData: {fileID: 0}
m_ExecutionOrder: 0
m_ClassName: AppStart
m_Namespace: Fungus
m_AssemblyName: Assembly-CSharp.dll
m_IsEditorScript: 0
--- !u!1 &1440444951
GameObject:
m_ObjectHideFlags: 0
@ -4456,6 +4474,9 @@ GameObject:
- 114: {fileID: 1507034369}
- 114: {fileID: 1507034368}
- 114: {fileID: 1507034370}
- 114: {fileID: 1507034372}
- 114: {fileID: 1507034371}
- 114: {fileID: 1507034373}
m_Layer: 0
m_Name: Start
m_TagString: Untagged
@ -4492,11 +4513,12 @@ MonoBehaviour:
m_EditorClassIdentifier:
nodeRect:
serializedVersion: 2
x: 105.537018
y: -501.022339
x: 106.537018
y: -503.022339
width: 120
height: 30
sequenceName: Start
eventHandler: {fileID: 1507034373}
commandList:
- {fileID: 1507034370}
- {fileID: 1507034368}
@ -4581,6 +4603,42 @@ MonoBehaviour:
indentLevel: 0
musicClip: {fileID: 8300000, guid: ae3aa0e9b0f0248a59548b5d9e77a6c1, type: 3}
atTime: 0
--- !u!114 &1507034371
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1507034363}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1433226136}
m_Name:
m_EditorClassIdentifier:
parentSequence: {fileID: 1507034365}
--- !u!114 &1507034372
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1507034363}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cbebb04788538436198f5292e6a3d835, type: 3}
m_Name:
m_EditorClassIdentifier:
parentSequence: {fileID: 1507034365}
--- !u!114 &1507034373
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1507034363}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d2f6487d21a03404cb21b245f0242e79, type: 3}
m_Name:
m_EditorClassIdentifier:
parentSequence: {fileID: 1507034365}
--- !u!1 &1509015466
GameObject:
m_ObjectHideFlags: 0
@ -5236,10 +5294,11 @@ MonoBehaviour:
nodeRect:
serializedVersion: 2
x: 108
y: -439.890259
y: -437.890259
width: 120
height: 30
sequenceName: Intro
eventHandler: {fileID: 0}
commandList:
- {fileID: 1759345003}
- {fileID: 1759345004}
@ -5561,6 +5620,7 @@ MonoBehaviour:
width: 120
height: 30
sequenceName: Shout out
eventHandler: {fileID: 0}
commandList:
- {fileID: 1801800853}
- {fileID: 1801800859}
@ -5879,7 +5939,6 @@ GameObject:
- 114: {fileID: 1831099570}
- 114: {fileID: 1831099569}
- 114: {fileID: 1831099571}
- 114: {fileID: 1831099572}
m_Layer: 0
m_Name: FungusScript
m_TagString: Untagged
@ -5919,7 +5978,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7a334fe2ffb574b3583ff3b18b4792d3, type: 3}
m_Name:
m_EditorClassIdentifier:
scrollPos: {x: 255.192505, y: 540.002808}
scrollPos: {x: 204.192505, y: 518.002808}
variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 0
zoom: 1
@ -5929,17 +5988,15 @@ MonoBehaviour:
y: -904.006714
width: 1939.01758
height: 1993.50671
selectedSequence: {fileID: 1759344996}
selectedCommands: []
selectedSequence: {fileID: 1507034365}
selectedCommands:
- {fileID: 1507034370}
variables:
- {fileID: 1831099569}
startSequence: {fileID: 1507034365}
executeOnStart: 1
settings:
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
--- !u!114 &1831099569
MonoBehaviour:
m_ObjectHideFlags: 2
@ -5980,21 +6037,6 @@ MonoBehaviour:
m_EditorClassIdentifier:
scope: 0
key: Var
--- !u!114 &1831099572
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1831099566}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1902400ccc99b45d69ad01cb86b57d0f, type: 3}
m_Name:
m_EditorClassIdentifier:
errorMessage:
indentLevel: 0
musicClip: {fileID: 0}
atTime: 0
--- !u!1 &1846387297
GameObject:
m_ObjectHideFlags: 1
@ -6030,6 +6072,7 @@ MonoBehaviour:
width: 240
height: 68
sequenceName: New Sequence
eventHandler: {fileID: 0}
commandList: []
--- !u!114 &1846387299
MonoBehaviour:
@ -6055,13 +6098,10 @@ MonoBehaviour:
selectedSequence: {fileID: 1846387298}
selectedCommands: []
variables: []
startSequence: {fileID: 1846387298}
executeOnStart: 1
settings:
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
runSlowInEditor: 1
runSlowDuration: .25
colorCommands: 1
hideComponents: 1
--- !u!4 &1846387300
Transform:
m_ObjectHideFlags: 1
@ -6436,6 +6476,7 @@ MonoBehaviour:
width: 120
height: 30
sequenceName: Exit
eventHandler: {fileID: 0}
commandList:
- {fileID: 2111357060}
- {fileID: 2111357059}

7
Assets/Fungus/FungusScript/Commands/RunScript.cs

@ -12,7 +12,10 @@ namespace Fungus
[Tooltip("Reference to another Fungus Script to execute")]
public FungusScript targetFungusScript;
[Tooltip("Stop executing current script before executing the new Fungus Script")]
[Tooltip("Name of sequence to execute in target Fungus Script")]
public string targetSequenceName;
[Tooltip("Stop executing the current sequence before executing the new Fungus Script")]
public bool stopCurrentScript = true;
public override void OnEnter()
@ -24,7 +27,7 @@ namespace Fungus
Stop();
}
targetFungusScript.Execute();
targetFungusScript.ExecuteSequence(targetSequenceName);
if (!stopCurrentScript)
{

2
Assets/Fungus/FungusScript/Editor/CommandEditor.cs

@ -53,7 +53,7 @@ namespace Fungus
if (t.enabled)
{
if (fungusScript.settings.colorCommands)
if (fungusScript.colorCommands)
{
GUI.backgroundColor = t.GetButtonColor();
}

2
Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs

@ -247,7 +247,7 @@ namespace Fungus
}
Color commandLabelColor = Color.white;
if (fungusScript.settings.colorCommands)
if (fungusScript.colorCommands)
{
commandLabelColor = command.GetButtonColor();
}

60
Assets/Fungus/FungusScript/Editor/EventHandlerEditor.cs

@ -0,0 +1,60 @@
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Fungus
{
[CustomEditor (typeof(EventHandler), true)]
public class EventHandlerEditor : Editor
{
public virtual void DrawInspectorGUI()
{
// Users should not be able to change the MonoScript for the command using the usual Script field.
// Doing so could cause sequence.commandList to contain null entries.
// To avoid this we manually display all properties, except for m_Script.
serializedObject.Update();
SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren))
{
enterChildren = false;
if (iterator.name == "m_Script")
{
continue;
}
EditorGUILayout.PropertyField(iterator, true, new GUILayoutOption[0]);
}
EventHandler t = target as EventHandler;
EventHandlerInfoAttribute info = GetEventHandlerInfo(t.GetType());
if (info != null &&
info.HelpText.Length > 0)
{
EditorGUILayout.HelpBox(info.HelpText, MessageType.Info);
}
serializedObject.ApplyModifiedProperties();
}
public static EventHandlerInfoAttribute GetEventHandlerInfo(System.Type eventHandlerType)
{
object[] attributes = eventHandlerType.GetCustomAttributes(typeof(EventHandlerInfoAttribute), false);
foreach (object obj in attributes)
{
EventHandlerInfoAttribute eventHandlerInfoAttr = obj as EventHandlerInfoAttribute;
if (eventHandlerInfoAttr != null)
{
return eventHandlerInfoAttr;
}
}
return null;
}
}
}

8
Assets/Fungus/FungusScript/Editor/EventHandlerEditor.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 20c4bb770f7fd424c84b0e6a835ba45b
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

35
Assets/Fungus/FungusScript/Editor/FungusScriptEditor.cs

@ -10,16 +10,18 @@ namespace Fungus
[CustomEditor (typeof(FungusScript))]
public class FungusScriptEditor : Editor
{
protected SerializedProperty startSequenceProp;
protected SerializedProperty executeOnStartProp;
protected SerializedProperty settingsProp;
protected SerializedProperty runSlowInEditorProp;
protected SerializedProperty runSlowDurationProp;
protected SerializedProperty colorCommandsProp;
protected SerializedProperty hideComponentsProp;
protected SerializedProperty variablesProp;
protected virtual void OnEnable()
{
startSequenceProp = serializedObject.FindProperty("startSequence");
executeOnStartProp = serializedObject.FindProperty("executeOnStart");
settingsProp = serializedObject.FindProperty("settings");
runSlowInEditorProp = serializedObject.FindProperty("runSlowInEditor");
runSlowDurationProp = serializedObject.FindProperty("runSlowDuration");
colorCommandsProp = serializedObject.FindProperty("colorCommands");
hideComponentsProp = serializedObject.FindProperty("hideComponents");
variablesProp = serializedObject.FindProperty("variables");
}
@ -31,23 +33,10 @@ namespace Fungus
fungusScript.UpdateHideFlags();
SequenceEditor.SequenceField(startSequenceProp,
new GUIContent("Start Sequence", "First sequence to execute when the Fungus Script executes"),
new GUIContent("<None>"),
fungusScript);
if (fungusScript.startSequence == null)
{
GUIStyle style = new GUIStyle(GUI.skin.label);
style.normal.textColor = new Color(1,0,0);
EditorGUILayout.LabelField(new GUIContent("Error: Please select a Start Sequence"), style);
}
EditorGUILayout.PropertyField(executeOnStartProp, new GUIContent("Execute On Start", "Execute this Fungus Script when the scene starts playing"));
EditorGUILayout.PropertyField(settingsProp, new GUIContent("Settings", "Configution options for the Fungus Script"), true);
EditorGUILayout.Separator();
EditorGUILayout.PropertyField(runSlowInEditorProp);
EditorGUILayout.PropertyField(runSlowDurationProp);
EditorGUILayout.PropertyField(colorCommandsProp);
EditorGUILayout.PropertyField(hideComponentsProp);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();

1
Assets/Fungus/FungusScript/Editor/FungusScriptMenuItems.cs

@ -16,7 +16,6 @@ namespace Fungus
Sequence sequence = Undo.AddComponent<Sequence>(newFungusScriptGO);
sequence.nodeRect.x += 60;
sequence.nodeRect.y += 60;
fungusScript.startSequence = sequence;
fungusScript.selectedSequence = sequence;
fungusScript.scrollPos = Vector2.zero;
Undo.RegisterCreatedObjectUndo(newFungusScriptGO, "Create Fungus Script");

16
Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs

@ -374,7 +374,21 @@ namespace Fungus
nodeStyle = new GUIStyle("flow node 4");
}
GUILayout.Box(sequence.sequenceName, nodeStyle, GUILayout.Width(sequence.nodeRect.width), GUILayout.Height(sequence.nodeRect.height));
nodeStyle.wordWrap = true;
string nodeName = "";
if (sequence.eventHandler != null)
{
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(sequence.eventHandler.GetType());
if (info != null)
{
nodeName = "(" + info.EventHandlerName + ")\n";
nodeStyle.padding.top = 23; // Adjust label to fit on two lines
}
}
nodeName += sequence.sequenceName;
GUILayout.Box(nodeName, nodeStyle, GUILayout.Width(sequence.nodeRect.width), GUILayout.Height(sequence.nodeRect.height));
if (Event.current.type == EventType.ContextClick)
{

95
Assets/Fungus/FungusScript/Editor/SequenceEditor.cs

@ -12,6 +12,12 @@ namespace Fungus
[CustomEditor (typeof(Sequence))]
public class SequenceEditor : Editor
{
protected class SetEventHandlerOperation
{
public Sequence sequence;
public Type eventHandlerType;
}
protected class AddCommandOperation
{
public Sequence sequence;
@ -19,11 +25,14 @@ namespace Fungus
public int index;
}
public virtual void DrawCommandListGUI(FungusScript fungusScript)
public virtual void DrawSequenceGUI(FungusScript fungusScript)
{
serializedObject.Update();
Sequence sequence = target as Sequence;
DrawEventHandlerGUI(fungusScript);
UpdateIndentLevels(sequence);
SerializedProperty sequenceNameProperty = serializedObject.FindProperty("sequenceName");
@ -82,6 +91,90 @@ namespace Fungus
serializedObject.ApplyModifiedProperties();
}
protected void DrawEventHandlerGUI(FungusScript fungusScript)
{
// Show available Event Handlers in a drop down list with type of current
// event handler selected.
List<System.Type> eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList();
Sequence sequence = target as Sequence;
System.Type currentType = null;
if (sequence.eventHandler != null)
{
currentType = sequence.eventHandler.GetType();
}
string currentHandlerName = "None";
if (currentType != null)
{
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(currentType);
currentHandlerName = info.EventHandlerName;
}
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel(new GUIContent("Execute On Event"));
if (GUILayout.Button(new GUIContent(currentHandlerName), EditorStyles.popup))
{
SetEventHandlerOperation noneOperation = new SetEventHandlerOperation();
noneOperation.sequence = sequence;
noneOperation.eventHandlerType = null;
GenericMenu eventHandlerMenu = new GenericMenu();
eventHandlerMenu.AddItem(new GUIContent("None"), false, OnSelectEventHandler, noneOperation);
foreach (System.Type type in eventHandlerTypes)
{
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type);
string typeName = "";
if (info.Category.Length > 0)
{
typeName = info.Category + "/";
}
typeName += info.EventHandlerName;
SetEventHandlerOperation operation = new SetEventHandlerOperation();
operation.sequence = sequence;
operation.eventHandlerType = type;
eventHandlerMenu.AddItem(new GUIContent(typeName), false, OnSelectEventHandler, operation);
}
eventHandlerMenu.ShowAsContext();
}
EditorGUILayout.EndHorizontal();
if (sequence.eventHandler != null)
{
EventHandlerEditor eventHandlerEditor = Editor.CreateEditor(sequence.eventHandler) as EventHandlerEditor;
eventHandlerEditor.DrawInspectorGUI();
DestroyImmediate(eventHandlerEditor);
}
}
protected void OnSelectEventHandler(object obj)
{
SetEventHandlerOperation operation = obj as SetEventHandlerOperation;
Sequence sequence = operation.sequence;
System.Type selectedType = operation.eventHandlerType;
if (sequence == null)
{
return;
}
Undo.RecordObject(sequence, "Set Start Event");
if (sequence.eventHandler != null)
{
Undo.DestroyObjectImmediate(sequence.eventHandler);
}
if (selectedType != null)
{
EventHandler newHandler = Undo.AddComponent(sequence.gameObject, selectedType) as EventHandler;
newHandler.parentSequence = sequence;
sequence.eventHandler = newHandler;
}
}
protected virtual void UpdateIndentLevels(Sequence sequence)
{
int indentLevel = 0;

2
Assets/Fungus/FungusScript/Editor/SequenceInspector.cs

@ -32,7 +32,7 @@ namespace Fungus
FungusScript fungusScript = sequence.GetFungusScript();
SequenceEditor sequenceEditor = Editor.CreateEditor(sequence) as SequenceEditor;
sequenceEditor.DrawCommandListGUI(fungusScript);
sequenceEditor.DrawSequenceGUI(fungusScript);
DestroyImmediate(sequenceEditor);
Command inspectCommand = null;

5
Assets/Fungus/FungusScript/EventHandlers.meta

@ -0,0 +1,5 @@
fileFormatVersion: 2
guid: cb76d2003b2004cd3ace7c8a0b5fd276
folderAsset: yes
DefaultImporter:
userData:

18
Assets/Fungus/FungusScript/EventHandlers/StartGame.cs

@ -0,0 +1,18 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Fungus
{
[EventHandlerInfo("",
"Start Game",
"The sequence will execute when the game starts playing.")]
public class StartGame : EventHandler
{
protected virtual void Start()
{
ExecuteSequence();
}
}
}

8
Assets/Fungus/FungusScript/EventHandlers/StartGame.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d2f6487d21a03404cb21b245f0242e79
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

50
Assets/Fungus/FungusScript/Scripts/EventHandler.cs

@ -0,0 +1,50 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Fungus
{
public class EventHandlerInfoAttribute : Attribute
{
public EventHandlerInfoAttribute(string category, string eventHandlerName, string helpText)
{
this.Category = category;
this.EventHandlerName = eventHandlerName;
this.HelpText = helpText;
}
public string Category { get; set; }
public string EventHandlerName { get; set; }
public string HelpText { get; set; }
}
/**
* A Sequence may have an associated Event Handler which starts executing the sequence when
* a specific event occurs.
* To create a custom Event Handler, simply subclass EventHandler and call the ExecuteSequence() method
* when the event occurs.
* Add an EventHandlerInfo attibute and your new EventHandler class will automatically appear in the
* 'Start Event' dropdown menu when a sequence is selected.
*/
public class EventHandler : MonoBehaviour
{
[HideInInspector]
public Sequence parentSequence;
/**
* The Event Handler should call this method when the event is detected.
*/
public virtual bool ExecuteSequence()
{
if (parentSequence == null)
{
return false;
}
FungusScript fungusScript = parentSequence.GetFungusScript();
return fungusScript.ExecuteSequence(parentSequence);
}
}
}

8
Assets/Fungus/FungusScript/Scripts/EventHandler.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 33263015c61344280b425bd04afb7ff3
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

93
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -59,60 +59,30 @@ namespace Fungus
public List<Variable> variables = new List<Variable>();
/**
* First sequence to execute when the Fungus Script executes.
* Slow down execution when playing in the editor to make it easier to visualise program flow.
*/
[Tooltip("First sequence to execute when the Fungus Script executes")]
public Sequence startSequence;
[Tooltip("Slow down execution in the editor to make it easier to visualise program flow")]
public bool runSlowInEditor = true;
/**
* Execute this Fungus Script when the scene starts.
*/
[Tooltip("Execute this Fungus Script when the scene starts playing")]
public bool executeOnStart = true;
[System.Serializable]
public class Settings
{
/**
* Slow down execution when playing in the editor to make it easier to visualise program flow.
*/
[Tooltip("Slow down execution in the editor to make it easier to visualise program flow")]
public bool runSlowInEditor = true;
/**
* Minimum time for each command to execute when runSlowInEditor is enabled.
*/
[Range(0f, 5f)]
[Tooltip("Minimum time that each command will take to execute when Run Slow In Editor is enabled")]
public float runSlowDuration = 0.25f;
/**
* Use command color when displaying the command list in the Fungus Editor window.
*/
[Tooltip("Use command color when displaying the command list in the Fungus Editor window")]
public bool colorCommands = true;
/**
* Hides the Fungus Script sequence and command components in the inspector.
* Deselect to inspect the sequence and command components that make up the Fungus Script.
*/
[Tooltip("Hides the Fungus Script sequence and command components in the inspector")]
public bool hideComponents = true;
}
* Minimum time for each command to execute when runSlowInEditor is enabled.
*/
[Range(0f, 5f)]
[Tooltip("Minimum time that each command will take to execute when Run Slow In Editor is enabled")]
public float runSlowDuration = 0.25f;
/**
* Advanced configuration options for the Fungus Script.
* Use command color when displaying the command list in the Fungus Editor window.
*/
[Tooltip("Advanced configuration options for the Fungus Script")]
public Settings settings;
protected virtual void Start()
{
if (executeOnStart)
{
Execute();
}
}
[Tooltip("Use command color when displaying the command list in the Fungus Editor window")]
public bool colorCommands = true;
/**
* Hides the Fungus Script sequence and command components in the inspector.
* Deselect to inspect the sequence and command components that make up the Fungus Script.
*/
[Tooltip("Hides the Fungus Script sequence and command components in the inspector")]
public bool hideComponents = true;
protected virtual Sequence CreateSequenceComponent(GameObject parent)
{
@ -132,23 +102,26 @@ namespace Fungus
}
/**
* Start running the Fungus Script by executing the startSequence.
* Start running another Fungus Script by executing a specific child sequence.
* The sequence must be in an idle state to be executed.
* Returns true if the Sequence started execution.
*/
public virtual void Execute()
public virtual bool ExecuteSequence(string sequenceName)
{
if (startSequence == null)
Sequence [] sequences = GetComponentsInChildren<Sequence>();
foreach (Sequence sequence in sequences)
{
return;
if (sequence.sequenceName == sequenceName)
{
return ExecuteSequence(sequence);
}
}
// Show start sequence by default
selectedSequence = startSequence;
ExecuteSequence(startSequence);
return false;
}
/**
* Start running the Fungus Script by executing a specific child sequence.
* Start running another Fungus Script by executing a specific child sequence.
* The sequence must be in an idle state to be executed.
* Returns true if the Sequence started execution.
*/
@ -398,17 +371,17 @@ namespace Fungus
Sequence[] sequences = GetComponentsInChildren<Sequence>();
foreach (Sequence sequence in sequences)
{
sequence.hideFlags = settings.hideComponents ? HideFlags.HideInInspector : HideFlags.None;
sequence.hideFlags = hideComponents ? HideFlags.HideInInspector : HideFlags.None;
if (sequence.gameObject != gameObject)
{
sequence.gameObject.hideFlags = settings.hideComponents ? HideFlags.HideInHierarchy : HideFlags.None;
sequence.gameObject.hideFlags = hideComponents ? HideFlags.HideInHierarchy : HideFlags.None;
}
}
Command[] commands = GetComponentsInChildren<Command>();
foreach (Command command in commands)
{
command.hideFlags = settings.hideComponents ? HideFlags.HideInInspector : HideFlags.None;
command.hideFlags = hideComponents ? HideFlags.HideInInspector : HideFlags.None;
}
}

8
Assets/Fungus/FungusScript/Scripts/Sequence.cs

@ -12,9 +12,13 @@ namespace Fungus
{
public string sequenceName = "New Sequence";
public EventHandler eventHandler;
[HideInInspector]
[System.NonSerialized]
public Command activeCommand;
[HideInInspector]
public List<Command> commandList = new List<Command>();
protected int executionCount;
@ -108,14 +112,14 @@ namespace Fungus
{
FungusScript fungusScript = GetFungusScript();
if (!fungusScript.settings.runSlowInEditor)
if (!fungusScript.runSlowInEditor)
{
activeCommand = nextCommand;
nextCommand.Execute();
}
else
{
StartCoroutine(ExecuteAfterDelay(nextCommand, fungusScript.settings.runSlowDuration));
StartCoroutine(ExecuteAfterDelay(nextCommand, fungusScript.runSlowDuration));
}
}

Loading…
Cancel
Save