Browse Source

Change #66 User extendible variables system

Added Other category for non-core variable types.
Wider variable window to fit long type names.
Use internal editor component paste instead of reflection to paste
commands
SetVariable only supports 4 basic variable types.
Simplified definition for new variable types.
Added new variable types to support core Unity types.
master
chrisgregan 10 years ago
parent
commit
e07a227aa0
  1. 84
      Assets/Fungus/FungusScript/Editor/FungusScriptEditor.cs
  2. 2
      Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs
  3. 26
      Assets/Fungus/FungusScript/Editor/SequenceEditor.cs
  4. 7
      Assets/Fungus/FungusScript/Editor/SetVariableEditor.cs
  5. 295
      Assets/Fungus/FungusScript/Editor/VariableEditor.cs
  6. 22
      Assets/Fungus/FungusScript/Editor/VariableListAdaptor.cs
  7. 2
      Assets/Fungus/FungusScript/Scripts/Commands/SetVariable.cs
  8. 3
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  9. 52
      Assets/Fungus/FungusScript/Scripts/Variable.cs
  10. 5
      Assets/Fungus/FungusScript/Scripts/VariableTypes.meta
  11. 26
      Assets/Fungus/FungusScript/Scripts/VariableTypes/BooleanVariable.cs
  12. 0
      Assets/Fungus/FungusScript/Scripts/VariableTypes/BooleanVariable.cs.meta
  13. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/ColorVariable.cs
  14. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/ColorVariable.cs.meta
  15. 27
      Assets/Fungus/FungusScript/Scripts/VariableTypes/FloatVariable.cs
  16. 0
      Assets/Fungus/FungusScript/Scripts/VariableTypes/FloatVariable.cs.meta
  17. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/GameObjectVariable.cs
  18. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/GameObjectVariable.cs.meta
  19. 27
      Assets/Fungus/FungusScript/Scripts/VariableTypes/IntegerVariable.cs
  20. 0
      Assets/Fungus/FungusScript/Scripts/VariableTypes/IntegerVariable.cs.meta
  21. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/MaterialVariable.cs
  22. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/MaterialVariable.cs.meta
  23. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/ObjectVariable.cs
  24. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/ObjectVariable.cs.meta
  25. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/SpriteVariable.cs
  26. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/SpriteVariable.cs.meta
  27. 26
      Assets/Fungus/FungusScript/Scripts/VariableTypes/StringVariable.cs
  28. 0
      Assets/Fungus/FungusScript/Scripts/VariableTypes/StringVariable.cs.meta
  29. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/TextureVariable.cs
  30. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/TextureVariable.cs.meta
  31. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector2Variable.cs
  32. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector2Variable.cs.meta
  33. 38
      Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector3Variable.cs
  34. 8
      Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector3Variable.cs.meta

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

@ -4,12 +4,19 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Rotorz.ReorderableList; using Rotorz.ReorderableList;
using System.Linq; using System.Linq;
using System.Reflection;
namespace Fungus namespace Fungus
{ {
[CustomEditor (typeof(FungusScript))] [CustomEditor (typeof(FungusScript))]
public class FungusScriptEditor : Editor public class FungusScriptEditor : Editor
{ {
protected class AddVariableInfo
{
public FungusScript fungusScript;
public System.Type variableType;
}
protected SerializedProperty descriptionProp; protected SerializedProperty descriptionProp;
protected SerializedProperty colorCommandsProp; protected SerializedProperty colorCommandsProp;
protected SerializedProperty hideComponentsProp; protected SerializedProperty hideComponentsProp;
@ -117,11 +124,46 @@ namespace Fungus
GUI.Button(plusRect, FungusEditorResources.texAddButton)) GUI.Button(plusRect, FungusEditorResources.texAddButton))
{ {
GenericMenu menu = new GenericMenu (); GenericMenu menu = new GenericMenu ();
menu.AddItem(new GUIContent ("Boolean"), false, AddVariable<BooleanVariable>, t); List<System.Type> types = FindAllDerivedTypes<Variable>();
menu.AddItem (new GUIContent ("Integer"), false, AddVariable<IntegerVariable>, t);
menu.AddItem (new GUIContent ("Float"), false, AddVariable<FloatVariable>, t); // Add variable types without a category
menu.AddItem (new GUIContent ("String"), false, AddVariable<StringVariable>, t); foreach (System.Type type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category != "")
{
continue;
}
AddVariableInfo addVariableInfo = new AddVariableInfo();
addVariableInfo.fungusScript = t;
addVariableInfo.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, addVariableInfo);
}
// Add types with a category
foreach (System.Type type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category == "")
{
continue;
}
AddVariableInfo info = new AddVariableInfo();
info.fungusScript = t;
info.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, info);
}
menu.ShowAsContext (); menu.ShowAsContext ();
} }
@ -129,20 +171,40 @@ namespace Fungus
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }
protected virtual void AddVariable<T>(object obj) where T : Variable protected virtual void AddVariable(object obj)
{ {
FungusScript fungusScript = obj as FungusScript; AddVariableInfo addVariableInfo = obj as AddVariableInfo;
if (fungusScript == null) if (addVariableInfo == null)
{ {
return; return;
} }
FungusScript fungusScript = addVariableInfo.fungusScript;
System.Type variableType = addVariableInfo.variableType;
Undo.RecordObject(fungusScript, "Add Variable"); Undo.RecordObject(fungusScript, "Add Variable");
T newVariable = fungusScript.gameObject.AddComponent<T>(); Variable newVariable = fungusScript.gameObject.AddComponent(variableType) as Variable;
newVariable.key = fungusScript.GetUniqueVariableKey(""); newVariable.key = fungusScript.GetUniqueVariableKey("");
fungusScript.variables.Add(newVariable); fungusScript.variables.Add(newVariable);
} }
public static List<System.Type> FindAllDerivedTypes<T>()
{
return FindAllDerivedTypes<T>(Assembly.GetAssembly(typeof(T)));
}
public static List<System.Type> FindAllDerivedTypes<T>(Assembly assembly)
{
var derivedType = typeof(T);
return assembly
.GetTypes()
.Where(t =>
t != derivedType &&
derivedType.IsAssignableFrom(t)
).ToList();
}
} }
} }

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

@ -161,7 +161,7 @@ namespace Fungus
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
GUILayout.BeginVertical(GUILayout.Width(400)); GUILayout.BeginVertical(GUILayout.Width(440));
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();

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

@ -1,4 +1,5 @@
using UnityEditor; using UnityEditor;
using UnityEditorInternal;
using UnityEngine; using UnityEngine;
using System; using System;
using System.Collections; using System.Collections;
@ -580,18 +581,23 @@ namespace Fungus
foreach (Command command in commandCopyBuffer.GetCommands()) foreach (Command command in commandCopyBuffer.GetCommands())
{ {
System.Type type = command.GetType(); // Using the Editor copy / paste functionality instead instead of reflection
Command newCommand = Undo.AddComponent(fungusScript.selectedSequence.gameObject, type) as Command; // because this does a deep copy of the command properties.
newCommand.parentSequence = fungusScript.selectedSequence; if (ComponentUtility.CopyComponent(command))
System.Reflection.FieldInfo[] fields = type.GetFields();
foreach (System.Reflection.FieldInfo field in fields)
{ {
field.SetValue(newCommand, field.GetValue(command)); if (ComponentUtility.PasteComponentAsNew(fungusScript.gameObject))
{
Command[] commands = fungusScript.GetComponents<Command>();
Command pastedCommand = commands.Last<Command>();
if (pastedCommand != null)
{
fungusScript.selectedSequence.commandList.Insert(pasteIndex++, pastedCommand);
}
}
// This stops the user pasting the command manually into another game object.
ComponentUtility.CopyComponent(fungusScript.transform);
} }
Undo.RecordObject(fungusScript.selectedSequence, "Paste");
fungusScript.selectedSequence.commandList.Insert(pasteIndex++, newCommand);
} }
Repaint(); Repaint();

7
Assets/Fungus/FungusScript/Editor/SetVariableEditor.cs

@ -40,8 +40,11 @@ namespace Fungus
VariableEditor.VariableField(variableProp, VariableEditor.VariableField(variableProp,
new GUIContent("Set Variable", "Variable to set"), new GUIContent("Set Variable", "Variable to set"),
fungusScript); fungusScript,
(v) => (v.GetType() == typeof(BooleanVariable) ||
v.GetType() == typeof(IntegerVariable) ||
v.GetType() == typeof(FloatVariable) ||
v.GetType() == typeof(StringVariable)));
if (variableProp.objectReferenceValue == null) if (variableProp.objectReferenceValue == null)
{ {

295
Assets/Fungus/FungusScript/Editor/VariableEditor.cs

@ -17,6 +17,21 @@ namespace Fungus
t.hideFlags = HideFlags.HideInInspector; t.hideFlags = HideFlags.HideInInspector;
} }
public static VariableInfoAttribute GetVariableInfo(System.Type variableType)
{
object[] attributes = variableType.GetCustomAttributes(typeof(VariableInfoAttribute), false);
foreach (object obj in attributes)
{
VariableInfoAttribute variableInfoAttr = obj as VariableInfoAttribute;
if (variableInfoAttr != null)
{
return variableInfoAttr;
}
}
return null;
}
static public void VariableField(SerializedProperty property, GUIContent label, FungusScript fungusScript, Func<Variable, bool> filter = null) static public void VariableField(SerializedProperty property, GUIContent label, FungusScript fungusScript, Func<Variable, bool> filter = null)
{ {
List<string> variableKeys = new List<string>(); List<string> variableKeys = new List<string>();
@ -81,168 +96,34 @@ namespace Fungus
} }
} }
[CustomPropertyDrawer (typeof(BooleanData))] public class VariableDataDrawer<T> : PropertyDrawer where T : Variable
public class BooleanDataDrawer : PropertyDrawer
{ {
protected enum BooleanState
{
True,
False
}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{ {
EditorGUI.BeginProperty(position, label, property); EditorGUI.BeginProperty(position, label, property);
SerializedProperty referenceProp = property.FindPropertyRelative("booleanRef");
SerializedProperty valueProp = property.FindPropertyRelative("booleanVal");
const int popupWidth = 65; // The variable reference and data properties must follow the naming convention 'typeRef', 'typeVal'
Rect controlRect = EditorGUI.PrefixLabel(position, label);
Rect valueRect = controlRect;
valueRect.width = controlRect.width - popupWidth - 5;
Rect popupRect = controlRect;
if (referenceProp.objectReferenceValue == null) VariableInfoAttribute typeInfo = VariableEditor.GetVariableInfo(typeof(T));
if (typeInfo == null)
{ {
valueProp.boolValue = ((BooleanState)EditorGUI.EnumPopup(valueRect, valueProp.boolValue ? BooleanState.True : BooleanState.False) == BooleanState.True); return;
popupRect.x += valueRect.width + 5;
popupRect.width = popupWidth;
}
FungusScript fungusScript = property.serializedObject.targetObject as FungusScript;
if (fungusScript == null)
{
Command command = property.serializedObject.targetObject as Command;
if (command != null)
{
fungusScript = command.GetFungusScript();
}
} }
if (fungusScript != null) string propNameBase = typeInfo.VariableType;
{ propNameBase = Char.ToLowerInvariant(propNameBase[0]) + propNameBase.Substring(1);
BooleanVariable selectedBooleanVariable = referenceProp.objectReferenceValue as BooleanVariable;
List<string> variableKeys = new List<string>(); SerializedProperty referenceProp = property.FindPropertyRelative(propNameBase + "Ref");
List<Variable> variableObjects = new List<Variable>(); SerializedProperty valueProp = property.FindPropertyRelative(propNameBase + "Val");
variableKeys.Add("<Value>");
variableObjects.Add(null);
int index = 0;
int selectedIndex = 0;
foreach (Variable v in fungusScript.variables)
{
if (v.GetType() != typeof(BooleanVariable))
{
continue;
}
variableKeys.Add(v.key); if (referenceProp == null || valueProp == null)
variableObjects.Add(v);
index++;
if (v == selectedBooleanVariable)
{
selectedIndex = index;
}
}
selectedIndex = EditorGUI.Popup(popupRect, selectedIndex, variableKeys.ToArray());
referenceProp.objectReferenceValue = variableObjects[selectedIndex];
}
EditorGUI.EndProperty();
}
}
[CustomPropertyDrawer (typeof(IntegerData))]
public class IntegerDataDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
SerializedProperty referenceProp = property.FindPropertyRelative("integerRef");
SerializedProperty valueProp = property.FindPropertyRelative("integerVal");
const int popupWidth = 65;
Rect controlRect = EditorGUI.PrefixLabel(position, label);
Rect valueRect = controlRect;
valueRect.width = controlRect.width - popupWidth - 5;
Rect popupRect = controlRect;
if (referenceProp.objectReferenceValue == null)
{ {
valueProp.intValue = EditorGUI.IntField(valueRect, valueProp.intValue); return;
popupRect.x += valueRect.width + 5;
popupRect.width = popupWidth;
} }
FungusScript fungusScript = property.serializedObject.targetObject as FungusScript;
if (fungusScript == null)
{
Command command = property.serializedObject.targetObject as Command;
if (command != null)
{
fungusScript = command.GetFungusScript();
}
}
if (fungusScript != null)
{
IntegerVariable selectedVariable = referenceProp.objectReferenceValue as IntegerVariable;
List<string> variableKeys = new List<string>();
List<Variable> variableObjects = new List<Variable>();
variableKeys.Add("<Value>");
variableObjects.Add(null);
int index = 0;
int selectedIndex = 0;
foreach (Variable v in fungusScript.variables)
{
if (v.GetType() != typeof(IntegerVariable))
{
continue;
}
variableKeys.Add(v.key);
variableObjects.Add(v);
index++;
if (v == selectedVariable)
{
selectedIndex = index;
}
}
selectedIndex = EditorGUI.Popup(popupRect, selectedIndex, variableKeys.ToArray());
referenceProp.objectReferenceValue = variableObjects[selectedIndex];
}
EditorGUI.EndProperty();
}
}
[CustomPropertyDrawer (typeof(FloatData))]
public class FloatDataDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
SerializedProperty referenceProp = property.FindPropertyRelative("floatRef");
SerializedProperty valueProp = property.FindPropertyRelative("floatVal");
const int popupWidth = 65; const int popupWidth = 65;
Rect controlRect = EditorGUI.PrefixLabel(position, label); Rect controlRect = EditorGUI.PrefixLabel(position, label);
Rect valueRect = controlRect; Rect valueRect = controlRect;
valueRect.width = controlRect.width - popupWidth - 5; valueRect.width = controlRect.width - popupWidth - 5;
@ -250,7 +131,7 @@ namespace Fungus
if (referenceProp.objectReferenceValue == null) if (referenceProp.objectReferenceValue == null)
{ {
valueProp.floatValue = EditorGUI.FloatField(valueRect, valueProp.floatValue); EditorGUI.PropertyField(valueRect, valueProp, new GUIContent(""));
popupRect.x += valueRect.width + 5; popupRect.x += valueRect.width + 5;
popupRect.width = popupWidth; popupRect.width = popupWidth;
} }
@ -267,7 +148,7 @@ namespace Fungus
if (fungusScript != null) if (fungusScript != null)
{ {
FloatVariable selectedVariable = referenceProp.objectReferenceValue as FloatVariable; T selectedVariable = referenceProp.objectReferenceValue as T;
List<string> variableKeys = new List<string>(); List<string> variableKeys = new List<string>();
List<Variable> variableObjects = new List<Variable>(); List<Variable> variableObjects = new List<Variable>();
@ -279,7 +160,7 @@ namespace Fungus
int selectedIndex = 0; int selectedIndex = 0;
foreach (Variable v in fungusScript.variables) foreach (Variable v in fungusScript.variables)
{ {
if (v.GetType() != typeof(FloatVariable)) if (v.GetType() != typeof(T))
{ {
continue; continue;
} }
@ -303,77 +184,51 @@ namespace Fungus
} }
} }
[CustomPropertyDrawer (typeof(BooleanData))]
public class BooleanDataDrawer : VariableDataDrawer<BooleanVariable>
{}
[CustomPropertyDrawer (typeof(IntegerData))]
public class IntegerDataDrawer : VariableDataDrawer<IntegerVariable>
{}
[CustomPropertyDrawer (typeof(FloatData))]
public class FloatDataDrawer : VariableDataDrawer<FloatVariable>
{}
[CustomPropertyDrawer (typeof(StringData))] [CustomPropertyDrawer (typeof(StringData))]
public class StringDataDrawer : PropertyDrawer public class StringDataDrawer : VariableDataDrawer<StringVariable>
{ {}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{ [CustomPropertyDrawer (typeof(ColorData))]
EditorGUI.BeginProperty(position, label, property); public class ColorDataDrawer : VariableDataDrawer<ColorVariable>
{}
SerializedProperty referenceProp = property.FindPropertyRelative("stringRef");
SerializedProperty valueProp = property.FindPropertyRelative("stringVal"); [CustomPropertyDrawer (typeof(Vector2Data))]
public class Vector2DataDrawer : VariableDataDrawer<Vector2Variable>
const int popupWidth = 65; {}
Rect controlRect = EditorGUI.PrefixLabel(position, label); [CustomPropertyDrawer (typeof(Vector3Data))]
Rect valueRect = controlRect; public class Vector3DataDrawer : VariableDataDrawer<Vector3Variable>
valueRect.width = controlRect.width - popupWidth - 5; {}
Rect popupRect = controlRect;
[CustomPropertyDrawer (typeof(MaterialData))]
if (referenceProp.objectReferenceValue == null) public class MaterialDataDrawer : VariableDataDrawer<MaterialVariable>
{ {}
// StringData stringData = valueProp.serializedObject
valueProp.stringValue = EditorGUI.TextField(valueRect, valueProp.stringValue); [CustomPropertyDrawer (typeof(TextureData))]
popupRect.x += valueRect.width + 5; public class TextureDataDrawer : VariableDataDrawer<TextureVariable>
popupRect.width = popupWidth; {}
}
[CustomPropertyDrawer (typeof(SpriteData))]
FungusScript fungusScript = property.serializedObject.targetObject as FungusScript; public class SpriteDataDrawer : VariableDataDrawer<SpriteVariable>
if (fungusScript == null) {}
{
Command command = property.serializedObject.targetObject as Command; [CustomPropertyDrawer (typeof(GameObjectData))]
if (command != null) public class GameObjectDataDrawer : VariableDataDrawer<GameObjectVariable>
{ {}
fungusScript = command.GetFungusScript();
}
}
if (fungusScript != null)
{
StringVariable selectedVariable = referenceProp.objectReferenceValue as StringVariable;
List<string> variableKeys = new List<string>();
List<Variable> variableObjects = new List<Variable>();
variableKeys.Add("<Value>");
variableObjects.Add(null);
int index = 0;
int selectedIndex = 0;
foreach (Variable v in fungusScript.variables)
{
if (v.GetType() != typeof(StringVariable))
{
continue;
}
variableKeys.Add(v.key);
variableObjects.Add(v);
index++;
if (v == selectedVariable)
{
selectedIndex = index;
}
}
selectedIndex = EditorGUI.Popup(popupRect, selectedIndex, variableKeys.ToArray());
referenceProp.objectReferenceValue = variableObjects[selectedIndex];
}
EditorGUI.EndProperty();
}
}
[CustomPropertyDrawer (typeof(ObjectData))]
public class ObjectDataDrawer : VariableDataDrawer<ObjectVariable>
{}
} }

22
Assets/Fungus/FungusScript/Editor/VariableListAdaptor.cs

@ -91,7 +91,7 @@ namespace Fungus
return; return;
} }
float[] widths = { 50, 100, 130, 60 }; float[] widths = { 80, 100, 140, 60 };
Rect[] rects = new Rect[4]; Rect[] rects = new Rect[4];
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
@ -105,22 +105,10 @@ namespace Fungus
} }
} }
string type = ""; VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(variable.GetType());
if (variable.GetType() == typeof(BooleanVariable)) if (variableInfo == null)
{ {
type = "Boolean"; return;
}
else if (variable.GetType() == typeof(IntegerVariable))
{
type = "Integer";
}
else if (variable.GetType() == typeof(FloatVariable))
{
type = "Float";
}
else if (variable.GetType() == typeof(StringVariable))
{
type = "String";
} }
FungusScript fungusScript = FungusScriptWindow.GetFungusScript(); FungusScript fungusScript = FungusScriptWindow.GetFungusScript();
@ -165,7 +153,7 @@ namespace Fungus
variableObject.Update(); variableObject.Update();
GUI.Label(rects[0], type); GUI.Label(rects[0], variableInfo.VariableType);
key = EditorGUI.TextField(rects[1], variable.key); key = EditorGUI.TextField(rects[1], variable.key);
SerializedProperty keyProp = variableObject.FindProperty("key"); SerializedProperty keyProp = variableObject.FindProperty("key");

2
Assets/Fungus/FungusScript/Scripts/Commands/SetVariable.cs

@ -5,7 +5,7 @@ namespace Fungus
{ {
[CommandInfo("Scripting", [CommandInfo("Scripting",
"Set Variable", "Set Variable",
"Sets a variable to a new value using simple arithmetic operations. The value can be a constant or another variable.")] "Sets a Boolean, Integer, Float or String variable to a new value using a simple arithmetic operation. The value can be a constant or reference another variable of the same type.")]
public class SetVariable : Command public class SetVariable : Command
{ {
public enum SetOperator public enum SetOperator

3
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -200,7 +200,8 @@ namespace Fungus
bool collision = false; bool collision = false;
foreach(Variable variable in variables) foreach(Variable variable in variables)
{ {
if (variable == ignoreVariable || if (variable == null ||
variable == ignoreVariable ||
variable.key == null) variable.key == null)
{ {
continue; continue;

52
Assets/Fungus/FungusScript/Scripts/Variable.cs

@ -1,30 +1,60 @@
using UnityEngine; using UnityEngine;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
namespace Fungus namespace Fungus
{ {
public enum VariableType
{
Boolean,
Integer,
Float,
String
}
public enum VariableScope public enum VariableScope
{ {
Private, Private,
Public Public
} }
public class VariableInfoAttribute : Attribute
{
public VariableInfoAttribute(string category, string variableType, int order = 0)
{
this.Category = category;
this.VariableType = variableType;
this.Order = order;
}
public string Category { get; set; }
public string VariableType { get; set; }
public int Order { get; set; }
}
[RequireComponent(typeof(FungusScript))] [RequireComponent(typeof(FungusScript))]
public class Variable : MonoBehaviour public abstract class Variable : MonoBehaviour
{ {
public VariableScope scope; public VariableScope scope;
public string key = ""; public string key = "";
public virtual void OnReset() public abstract void OnReset();
{} }
public abstract class VariableBase<T> : Variable
{
public T value;
protected T startValue;
public override void OnReset()
{
value = startValue;
}
public override string ToString()
{
return value.ToString();
}
protected virtual void Start()
{
// Remember the initial value so we can reset later on
startValue = value;
}
} }
} }

5
Assets/Fungus/FungusScript/Scripts/VariableTypes.meta

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

26
Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs → Assets/Fungus/FungusScript/Scripts/VariableTypes/BooleanVariable.cs

@ -5,30 +5,12 @@ using System.Collections;
namespace Fungus namespace Fungus
{ {
public class BooleanVariable : Variable [VariableInfo("", "Boolean")]
{ public class BooleanVariable : VariableBase<bool>
public bool value; {}
protected bool startValue;
public override void OnReset()
{
value = startValue;
}
public override string ToString()
{
return value.ToString();
}
protected virtual void Start()
{
startValue = value;
}
}
[System.Serializable] [System.Serializable]
public struct BooleanData public class BooleanData
{ {
[SerializeField] [SerializeField]
public BooleanVariable booleanRef; public BooleanVariable booleanRef;

0
Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs.meta → Assets/Fungus/FungusScript/Scripts/VariableTypes/BooleanVariable.cs.meta

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/ColorVariable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "Color")]
public class ColorVariable : VariableBase<Color>
{}
[System.Serializable]
public class ColorData
{
[SerializeField]
public ColorVariable colorRef;
[SerializeField]
public Color colorVal;
public Color Value
{
get { return (colorRef == null) ? colorVal : colorRef.value; }
set { if (colorRef == null) { colorVal = value; } else { colorRef.value = value; } }
}
public string GetDescription()
{
if (colorRef == null)
{
return colorVal.ToString();
}
else
{
return colorRef.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/ColorVariable.cs.meta

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

27
Assets/Fungus/FungusScript/Scripts/FloatVariable.cs → Assets/Fungus/FungusScript/Scripts/VariableTypes/FloatVariable.cs

@ -3,31 +3,12 @@ using System.Collections;
namespace Fungus namespace Fungus
{ {
[VariableInfo("", "Float")]
public class FloatVariable : Variable public class FloatVariable : VariableBase<float>
{ {}
public float value;
protected float startValue;
public override void OnReset()
{
value = startValue;
}
public override string ToString()
{
return value.ToString();
}
protected virtual void Start()
{
startValue = value;
}
}
[System.Serializable] [System.Serializable]
public struct FloatData public class FloatData
{ {
[SerializeField] [SerializeField]
public FloatVariable floatRef; public FloatVariable floatRef;

0
Assets/Fungus/FungusScript/Scripts/FloatVariable.cs.meta → Assets/Fungus/FungusScript/Scripts/VariableTypes/FloatVariable.cs.meta

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/GameObjectVariable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "GameObject")]
public class GameObjectVariable : VariableBase<GameObject>
{}
[System.Serializable]
public class GameObjectData
{
[SerializeField]
public GameObjectVariable gameObjectRef;
[SerializeField]
public GameObject gameObjectVal;
public GameObject Value
{
get { return (gameObjectRef == null) ? gameObjectVal : gameObjectRef.value; }
set { if (gameObjectRef == null) { gameObjectVal = value; } else { gameObjectRef.value = value; } }
}
public string GetDescription()
{
if (gameObjectRef == null)
{
return gameObjectVal.ToString();
}
else
{
return gameObjectRef.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/GameObjectVariable.cs.meta

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

27
Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs → Assets/Fungus/FungusScript/Scripts/VariableTypes/IntegerVariable.cs

@ -3,31 +3,12 @@ using System.Collections;
namespace Fungus namespace Fungus
{ {
[VariableInfo("", "Integer")]
public class IntegerVariable : Variable public class IntegerVariable : VariableBase<int>
{ {}
public int value;
protected int startValue;
public override void OnReset()
{
value = startValue;
}
public override string ToString()
{
return value.ToString();
}
protected virtual void Start()
{
startValue = value;
}
}
[System.Serializable] [System.Serializable]
public struct IntegerData public class IntegerData
{ {
[SerializeField] [SerializeField]
public IntegerVariable integerRef; public IntegerVariable integerRef;

0
Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs.meta → Assets/Fungus/FungusScript/Scripts/VariableTypes/IntegerVariable.cs.meta

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/MaterialVariable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "Material")]
public class MaterialVariable : VariableBase<Material>
{}
[System.Serializable]
public class MaterialData
{
[SerializeField]
public MaterialVariable materialRef;
[SerializeField]
public Material materialVal;
public Material Value
{
get { return (materialRef == null) ? materialVal : materialRef.value; }
set { if (materialRef == null) { materialVal = value; } else { materialRef.value = value; } }
}
public string GetDescription()
{
if (materialRef == null)
{
return materialVal.ToString();
}
else
{
return materialRef.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/MaterialVariable.cs.meta

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

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/ObjectVariable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "Object")]
public class ObjectVariable : VariableBase<Object>
{}
[System.Serializable]
public class ObjectData
{
[SerializeField]
public ObjectVariable objectRef;
[SerializeField]
public Object objectVal;
public Object Value
{
get { return (objectRef == null) ? objectVal : objectRef.value; }
set { if (objectRef == null) { objectVal = value; } else { objectRef.value = value; } }
}
public string GetDescription()
{
if (objectRef == null)
{
return objectVal.ToString();
}
else
{
return objectRef.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/ObjectVariable.cs.meta

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

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/SpriteVariable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "Sprite")]
public class SpriteVariable : VariableBase<Sprite>
{}
[System.Serializable]
public class SpriteData
{
[SerializeField]
public SpriteVariable spriteRef;
[SerializeField]
public Sprite spriteVal;
public Sprite Value
{
get { return (spriteRef == null) ? spriteVal : spriteRef.value; }
set { if (spriteRef == null) { spriteVal = value; } else { spriteRef.value = value; } }
}
public string GetDescription()
{
if (spriteRef == null)
{
return spriteVal.ToString();
}
else
{
return spriteRef.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/SpriteVariable.cs.meta

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

26
Assets/Fungus/FungusScript/Scripts/StringVariable.cs → Assets/Fungus/FungusScript/Scripts/VariableTypes/StringVariable.cs

@ -4,30 +4,12 @@ using System.Collections;
namespace Fungus namespace Fungus
{ {
public class StringVariable : Variable [VariableInfo("", "String")]
{ public class StringVariable : VariableBase<string>
public string value = ""; {}
protected string startValue = "";
public override void OnReset()
{
value = startValue;
}
public override string ToString()
{
return value.ToString();
}
protected virtual void Start()
{
startValue = value;
}
}
[System.Serializable] [System.Serializable]
public struct StringData public class StringData
{ {
[SerializeField] [SerializeField]
public StringVariable stringRef; public StringVariable stringRef;

0
Assets/Fungus/FungusScript/Scripts/StringVariable.cs.meta → Assets/Fungus/FungusScript/Scripts/VariableTypes/StringVariable.cs.meta

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/TextureVariable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "Texture")]
public class TextureVariable : VariableBase<Texture>
{}
[System.Serializable]
public class TextureData
{
[SerializeField]
public TextureVariable textureRef;
[SerializeField]
public Texture textureVal;
public Texture Value
{
get { return (textureRef == null) ? textureVal : textureRef.value; }
set { if (textureRef == null) { textureVal = value; } else { textureRef.value = value; } }
}
public string GetDescription()
{
if (textureRef == null)
{
return textureVal.ToString();
}
else
{
return textureRef.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/TextureVariable.cs.meta

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

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector2Variable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "Vector2")]
public class Vector2Variable : VariableBase<Vector2>
{}
[System.Serializable]
public class Vector2Data
{
[SerializeField]
public Vector2Variable vector2Ref;
[SerializeField]
public Vector2 vector2Val;
public Vector2 Value
{
get { return (vector2Ref == null) ? vector2Val : vector2Ref.value; }
set { if (vector2Ref == null) { vector2Val = value; } else { vector2Ref.value = value; } }
}
public string GetDescription()
{
if (vector2Ref == null)
{
return vector2Val.ToString();
}
else
{
return vector2Ref.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector2Variable.cs.meta

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

38
Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector3Variable.cs

@ -0,0 +1,38 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "Vector3")]
public class Vector3Variable : VariableBase<Vector3>
{}
[System.Serializable]
public class Vector3Data
{
[SerializeField]
public Vector3Variable vector3Ref;
[SerializeField]
public Vector3 vector3Val;
public Vector3 Value
{
get { return (vector3Ref == null) ? vector3Val : vector3Ref.value; }
set { if (vector3Ref == null) { vector3Val = value; } else { vector3Ref.value = value; } }
}
public string GetDescription()
{
if (vector3Ref == null)
{
return vector3Val.ToString();
}
else
{
return vector3Ref.key;
}
}
}
}

8
Assets/Fungus/FungusScript/Scripts/VariableTypes/Vector3Variable.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4d769ea3513ad47aabf805db5252341d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
Loading…
Cancel
Save