Browse Source

Merge pull request #720 from stevehalliwell/VariableReference

Variable reference struct
master
Chris Gregan 6 years ago committed by GitHub
parent
commit
d71ec2eb00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 67
      Assets/Fungus/Scripts/Editor/VariableReferenceDrawer.cs
  2. 11
      Assets/Fungus/Scripts/Editor/VariableReferenceDrawer.cs.meta
  3. 34
      Assets/Fungus/Scripts/Utils/VariableReference.cs
  4. 11
      Assets/Fungus/Scripts/Utils/VariableReference.cs.meta
  5. 157
      Assets/FungusExamples/FirstPerson/FirstPersonDemo.unity
  6. 15
      Assets/FungusExamples/FirstPerson/Scripts/LookingAtDoor.cs

67
Assets/Fungus/Scripts/Editor/VariableReferenceDrawer.cs

@ -0,0 +1,67 @@
using UnityEditor;
using UnityEngine;
namespace Fungus.EditorUtils
{
/// <summary>
/// Custom drawer for the VariableReference, allows for more easily selecting a target variable in external c#
/// scripts.
/// </summary>
[CustomPropertyDrawer(typeof(Fungus.VariableReference))]
public class VariableReferenceDrawer : PropertyDrawer
{
public Fungus.Flowchart lastFlowchart;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var l = EditorGUI.BeginProperty(position, label, property);
var startPos = position;
position = EditorGUI.PrefixLabel(position, l);
position.height = EditorGUIUtility.singleLineHeight;
var variable = property.FindPropertyRelative("variable");
Fungus.Variable v = variable.objectReferenceValue as Fungus.Variable;
if (variable.objectReferenceValue != null && lastFlowchart == null)
{
if (v != null)
{
lastFlowchart = v.GetComponent<Flowchart>();
}
}
lastFlowchart = EditorGUI.ObjectField(position, lastFlowchart, typeof(Fungus.Flowchart), true) as Fungus.Flowchart;
position.y += EditorGUIUtility.singleLineHeight;
if (lastFlowchart != null)
{
var ourPos = startPos;
ourPos.y = position.y;
var prefixLabel = new GUIContent(v != null ? v.GetType().Name : "No Var Selected");
EditorGUI.indentLevel++;
VariableEditor.VariableField(variable,
prefixLabel,
lastFlowchart,
"<None>",
null,
//lable, index, elements
(s, t, u) => (EditorGUI.Popup(ourPos, s, t, u)));
EditorGUI.indentLevel--;
}
else
{
EditorGUI.PrefixLabel(position, new GUIContent("Flowchart Required"));
}
variable.serializedObject.ApplyModifiedProperties();
property.serializedObject.ApplyModifiedProperties();
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight * 2;
}
}
}

11
Assets/Fungus/Scripts/Editor/VariableReferenceDrawer.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e5d801a1ae7a4c144b6c561f8b7f8852
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

34
Assets/Fungus/Scripts/Utils/VariableReference.cs

@ -0,0 +1,34 @@
namespace Fungus
{
/// <summary>
/// A simple struct wrapping a reference to a Fungus Variable. Allows for VariableReferenceDrawer.
/// This is the a way to directly reference a fungus variable in external c# scripts, it will
/// give you an inspector field that gives a drop down of all the variables on the targeted
/// flowchart, in a similar way to what you would expect from selecting a variable on a command.
/// </summary>
[System.Serializable]
public struct VariableReference
{
public Variable variable;
public T Get<T>()
{
T retval = default(T);
var asType = variable as VariableBase<T>;
if (asType != null)
return asType.Value;
return retval;
}
public void Set<T>(T val)
{
var asType = variable as VariableBase<T>;
if (asType != null)
asType.Value = val;
}
}
}

11
Assets/Fungus/Scripts/Utils/VariableReference.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ce82e02decf51ce439183c17e926422f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

157
Assets/FungusExamples/FirstPerson/FirstPersonDemo.unity

@ -1222,7 +1222,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3} m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
selectedFlowchart: {fileID: 1788221410} selectedFlowchart: {fileID: 1112454213}
--- !u!4 &428461411 --- !u!4 &428461411
Transform: Transform:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1
@ -1337,8 +1337,7 @@ MonoBehaviour:
height: 0 height: 0
selectedBlocks: selectedBlocks:
- {fileID: 640305181} - {fileID: 640305181}
selectedCommands: selectedCommands: []
- {fileID: 640305193}
variables: [] variables: []
description: description:
stepPause: 0 stepPause: 0
@ -2455,6 +2454,32 @@ GameObject:
m_CorrespondingSourceObject: {fileID: 1802049371907118, guid: 929d45c4d9c08874ba1eaf9ba09de51d, m_CorrespondingSourceObject: {fileID: 1802049371907118, guid: 929d45c4d9c08874ba1eaf9ba09de51d,
type: 2} type: 2}
m_PrefabInternal: {fileID: 128347548} m_PrefabInternal: {fileID: 128347548}
--- !u!114 &1112454205
MonoBehaviour:
m_ObjectHideFlags: 2
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1112454204}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 37
indentLevel: 2
storyText: '[~Still there|So enticing|But what does it mean?]'
description:
character: {fileID: 92124522}
portrait: {fileID: 21300000, guid: ab808050bc535a643afaf4755050339e, type: 3}
voiceOverClip: {fileID: 0}
showAlways: 1
showCount: 1
extendPrevious: 0
fadeWhenDone: 1
waitForClick: 1
stopVoiceover: 1
waitForVO: 0
setSayDialog: {fileID: 0}
--- !u!114 &1112454206 --- !u!114 &1112454206
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
@ -2642,10 +2667,12 @@ MonoBehaviour:
y: 0 y: 0
width: 0 width: 0
height: 0 height: 0
selectedBlocks: [] selectedBlocks:
- {fileID: 1112454222}
selectedCommands: [] selectedCommands: []
variables: variables:
- {fileID: 1112454209} - {fileID: 1112454209}
- {fileID: 1112454226}
description: description:
stepPause: 0 stepPause: 0
colorCommands: 1 colorCommands: 1
@ -2680,7 +2707,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 12 itemId: 12
indentLevel: 0 indentLevel: 2
--- !u!114 &1112454216 --- !u!114 &1112454216
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
@ -2745,7 +2772,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 8 itemId: 8
indentLevel: 1 indentLevel: 2
storyText: "Yes, a door John. \n\nBut how to reach it." storyText: "Yes, a door John. \n\nBut how to reach it."
description: description:
character: {fileID: 92124522} character: {fileID: 92124522}
@ -2834,7 +2861,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 6 itemId: 6
indentLevel: 0 indentLevel: 2
--- !u!114 &1112454222 --- !u!114 &1112454222
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
@ -2859,13 +2886,120 @@ MonoBehaviour:
description: description:
eventHandler: {fileID: 0} eventHandler: {fileID: 0}
commandList: commandList:
- {fileID: 1112454221}
- {fileID: 1112454220} - {fileID: 1112454220}
- {fileID: 1112454225}
- {fileID: 1112454221}
- {fileID: 1112454219} - {fileID: 1112454219}
- {fileID: 1112454215}
- {fileID: 1112454224}
- {fileID: 1112454205}
- {fileID: 1112454223}
- {fileID: 1112454218} - {fileID: 1112454218}
- {fileID: 1112454217} - {fileID: 1112454217}
- {fileID: 1112454216} - {fileID: 1112454216}
- {fileID: 1112454215} --- !u!114 &1112454223
MonoBehaviour:
m_ObjectHideFlags: 2
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1112454204}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 93cb9773f2ca04e2bbf7a68ccfc23267, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 36
indentLevel: 1
--- !u!114 &1112454224
MonoBehaviour:
m_ObjectHideFlags: 2
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1112454204}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3fa968f01a7f9496bb50e13dfe16760d, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 35
indentLevel: 1
--- !u!114 &1112454225
MonoBehaviour:
m_ObjectHideFlags: 2
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1112454204}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 70c5622b8a80845c980954170295f292, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 34
indentLevel: 1
compareOperator: 0
variable: {fileID: 1112454226}
booleanData:
booleanRef: {fileID: 0}
booleanVal: 0
integerData:
integerRef: {fileID: 0}
integerVal: 0
floatData:
floatRef: {fileID: 0}
floatVal: 0
stringData:
stringRef: {fileID: 0}
stringVal:
animatorData:
animatorRef: {fileID: 0}
animatorVal: {fileID: 0}
audioSourceData:
audioSourceRef: {fileID: 0}
audioSourceVal: {fileID: 0}
colorData:
colorRef: {fileID: 0}
colorVal: {r: 0, g: 0, b: 0, a: 0}
gameObjectData:
gameObjectRef: {fileID: 0}
gameObjectVal: {fileID: 0}
materialData:
materialRef: {fileID: 0}
materialVal: {fileID: 0}
objectData:
objectRef: {fileID: 0}
objectVal: {fileID: 0}
rigidbody2DData:
rigidbody2DRef: {fileID: 0}
rigidbody2DVal: {fileID: 0}
spriteData:
spriteRef: {fileID: 0}
spriteVal: {fileID: 0}
textureData:
textureRef: {fileID: 0}
textureVal: {fileID: 0}
transformData:
transformRef: {fileID: 0}
transformVal: {fileID: 0}
vector2Data:
vector2Ref: {fileID: 0}
vector2Val: {x: 0, y: 0}
vector3Data:
vector3Ref: {fileID: 0}
vector3Val: {x: 0, y: 0, z: 0}
--- !u!114 &1112454226
MonoBehaviour:
m_ObjectHideFlags: 2
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1112454204}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d02d9822eec54c98afe95bb497211b3, type: 3}
m_Name:
m_EditorClassIdentifier:
scope: 0
key: hasGazed
value: 0
--- !u!1 &1140326301 --- !u!1 &1140326301
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -3250,6 +3384,8 @@ MonoBehaviour:
runBlockWhenGazed: runBlockWhenGazed:
block: {fileID: 1112454222} block: {fileID: 1112454222}
eye: {fileID: 1353701873} eye: {fileID: 1353701873}
fungusBoolHasGazed:
variable: {fileID: 1112454226}
--- !u!114 &1377282264 --- !u!114 &1377282264
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -3806,8 +3942,7 @@ MonoBehaviour:
height: 0 height: 0
selectedBlocks: selectedBlocks:
- {fileID: 1788221409} - {fileID: 1788221409}
selectedCommands: selectedCommands: []
- {fileID: 1788221406}
variables: [] variables: []
description: description:
stepPause: 0 stepPause: 0

15
Assets/FungusExamples/FirstPerson/Scripts/LookingAtDoor.cs

@ -1,6 +1,4 @@
using System.Collections; using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
namespace Fungus.Examples namespace Fungus.Examples
{ {
@ -12,6 +10,8 @@ namespace Fungus.Examples
public BlockReference runBlockWhenGazed; public BlockReference runBlockWhenGazed;
public Transform eye; public Transform eye;
public VariableReference fungusBoolHasGazed;
public void ActivateNow() public void ActivateNow()
{ {
enabled = true; enabled = true;
@ -19,10 +19,11 @@ namespace Fungus.Examples
private void Update() private void Update()
{ {
var curCounter = gazeCounter;
RaycastHit hit; RaycastHit hit;
if(Physics.Raycast(eye.position, eye.forward, out hit)) if (Physics.Raycast(eye.position, eye.forward, out hit))
{ {
if(hit.collider == doorCol) if (hit.collider == doorCol)
{ {
gazeCounter += Time.deltaTime; gazeCounter += Time.deltaTime;
} }
@ -36,10 +37,10 @@ namespace Fungus.Examples
gazeCounter = 0; gazeCounter = 0;
} }
if(gazeCounter >= gazeTime) if (gazeCounter >= gazeTime && curCounter <= gazeTime)
{ {
runBlockWhenGazed.Execute(); runBlockWhenGazed.Execute();
enabled = false; fungusBoolHasGazed.Set(true);
} }
} }
} }

Loading…
Cancel
Save