Browse Source

Added GlobalVariables

-'Global' variable scope
-Access is routed at runtime through the GlobalVariables manager attached to the FungusManager
master
desktop-maesty/steve 7 years ago
parent
commit
a9dc553c49
  1. 13
      Assets/Fungus/Scripts/Components/FungusManager.cs
  2. 51
      Assets/Fungus/Scripts/Components/Variable.cs
  3. 102
      Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs
  4. 65
      Assets/Fungus/Scripts/Utils/GlobalVariables.cs
  5. 12
      Assets/Fungus/Scripts/Utils/GlobalVariables.cs.meta
  6. 9
      Assets/FungusExamples/GlobalVariable.meta
  7. 1382
      Assets/FungusExamples/GlobalVariable/GlobalVar.unity
  8. 8
      Assets/FungusExamples/GlobalVariable/GlobalVar.unity.meta

13
Assets/Fungus/Scripts/Components/FungusManager.cs

@ -12,7 +12,8 @@ namespace Fungus
[RequireComponent(typeof(CameraManager))]
[RequireComponent(typeof(MusicManager))]
[RequireComponent(typeof(EventDispatcher))]
#if UNITY_5_3_OR_NEWER
[RequireComponent(typeof(GlobalVariables))]
#if UNITY_5_3_OR_NEWER
[RequireComponent(typeof(SaveManager))]
#endif
public sealed class FungusManager : MonoBehaviour
@ -26,7 +27,8 @@ namespace Fungus
CameraManager = GetComponent<CameraManager>();
MusicManager = GetComponent<MusicManager>();
EventDispatcher = GetComponent<EventDispatcher>();
#if UNITY_5_3_OR_NEWER
GlobalVariables = GetComponent<GlobalVariables>();
#if UNITY_5_3_OR_NEWER
SaveManager = GetComponent<SaveManager>();
#endif
}
@ -61,7 +63,12 @@ namespace Fungus
/// </summary>
public EventDispatcher EventDispatcher { get; private set; }
#if UNITY_5_3_OR_NEWER
/// <summary>
/// Gets the global variable singleton instance.
/// </summary>
public GlobalVariables GlobalVariables { get; private set; }
#if UNITY_5_3_OR_NEWER
/// <summary>
/// Gets the save manager singleton instance.
/// </summary>

51
Assets/Fungus/Scripts/Components/Variable.cs

@ -33,7 +33,9 @@ namespace Fungus
/// <summary> Can only be accessed by commands in the same Flowchart. </summary>
Private,
/// <summary> Can be accessed from any command in any Flowchart. </summary>
Public
Public,
/// <summary> Creates and/or references a global variable of that name, all variables of this name and scope share the same underlying fungus variable and exist for the duration of the instance of Unity.</summary>
Global,
}
/// <summary>
@ -89,7 +91,7 @@ namespace Fungus
/// <summary>
/// Visibility scope for the variable.
/// </summary>
public virtual VariableScope Scope { get { return scope; } }
public virtual VariableScope Scope { get { return scope; } set { scope = value; } }
/// <summary>
/// String identifier for the variable.
@ -109,9 +111,50 @@ namespace Fungus
/// </summary>
public abstract class VariableBase<T> : Variable
{
//caching mechanism for global static variables
private VariableBase<T> _globalStaicRef;
private VariableBase<T> globalStaicRef
{
get
{
if (_globalStaicRef != null)
{
return _globalStaicRef;
}
else
{
return _globalStaicRef = FungusManager.Instance.GlobalVariables.GetOrAddVariable(Key, value, this.GetType());
}
}
}
[SerializeField] protected T value;
public virtual T Value { get { return this.value; } set { this.value = value; } }
public virtual T Value
{
get
{
if (scope != VariableScope.Global)
{
return this.value;
}
else
{
return globalStaicRef.value;
}
}
set
{
if (scope != VariableScope.Global)
{
this.value = value;
}
else
{
globalStaicRef.Value = value;
}
}
}
protected T startValue;
public override void OnReset()

102
Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

@ -12,7 +12,8 @@ using Rotorz.ReorderableList;
namespace Fungus.EditorUtils
{
public class VariableListAdaptor : IReorderableListAdaptor {
public class VariableListAdaptor : IReorderableListAdaptor
{
public static readonly int DefaultWidth = 80 + 100 + 140 + 60;
public static readonly int ScrollSpacer = 8;
@ -24,56 +25,67 @@ namespace Fungus.EditorUtils
public int widthOfList;
public SerializedProperty this[int index] {
public SerializedProperty this[int index]
{
get { return _arrayProperty.GetArrayElementAtIndex(index); }
}
public SerializedProperty arrayProperty {
public SerializedProperty arrayProperty
{
get { return _arrayProperty; }
}
public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList) {
public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList)
{
if (arrayProperty == null)
throw new ArgumentNullException("Array property was null.");
if (!arrayProperty.isArray)
throw new InvalidOperationException("Specified serialized propery is not an array.");
this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight;
this.widthOfList = widthOfList - ScrollSpacer;
}
public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f, DefaultWidth) {
public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f, DefaultWidth)
{
}
public int Count {
public int Count
{
get { return _arrayProperty.arraySize; }
}
public virtual bool CanDrag(int index) {
public virtual bool CanDrag(int index)
{
return true;
}
public virtual bool CanRemove(int index) {
public virtual bool CanRemove(int index)
{
return true;
}
public void Add() {
public void Add()
{
int newIndex = _arrayProperty.arraySize;
++_arrayProperty.arraySize;
_arrayProperty.GetArrayElementAtIndex(newIndex).ResetValue();
}
public void Insert(int index) {
public void Insert(int index)
{
_arrayProperty.InsertArrayElementAtIndex(index);
_arrayProperty.GetArrayElementAtIndex(index).ResetValue();
}
public void Duplicate(int index) {
public void Duplicate(int index)
{
_arrayProperty.InsertArrayElementAtIndex(index);
}
public void Remove(int index) {
public void Remove(int index)
{
// Remove the Fungus Variable component
Variable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Variable;
Undo.DestroyObjectImmediate(variable);
@ -82,26 +94,29 @@ namespace Fungus.EditorUtils
_arrayProperty.DeleteArrayElementAtIndex(index);
}
public void Move(int sourceIndex, int destIndex) {
public void Move(int sourceIndex, int destIndex)
{
if (destIndex > sourceIndex)
--destIndex;
_arrayProperty.MoveArrayElement(sourceIndex, destIndex);
}
public void Clear() {
public void Clear()
{
_arrayProperty.ClearArray();
}
public void BeginGUI()
{}
{ }
public void EndGUI()
{}
{ }
public virtual void DrawItemBackground(Rect position, int index) {
public virtual void DrawItemBackground(Rect position, int index)
{
}
public void DrawItem(Rect position, int index)
public void DrawItem(Rect position, int index)
{
Variable variable = this[index].objectReferenceValue as Variable;
@ -142,7 +157,7 @@ namespace Fungus.EditorUtils
{
return;
}
// Highlight if an active or selected command is referencing this variable
bool highlight = false;
if (flowchart.SelectedBlock != null)
@ -188,12 +203,36 @@ namespace Fungus.EditorUtils
key = EditorGUI.TextField(rects[1], variable.Key);
SerializedProperty keyProp = variableObject.FindProperty("key");
SerializedProperty defaultProp = variableObject.FindProperty("value");
SerializedProperty scopeProp = variableObject.FindProperty("scope");
keyProp.stringValue = flowchart.GetUniqueVariableKey(key, variable);
SerializedProperty defaultProp = variableObject.FindProperty("value");
EditorGUI.PropertyField(rects[2], defaultProp, new GUIContent(""));
bool isGlobal = scopeProp.enumValueIndex == (int)VariableScope.Global;
if (isGlobal && Application.isPlaying)
{
var res = FungusManager.Instance.GlobalVariables.GetVariable(keyProp.stringValue);
if (res != null)
{
SerializedObject globalValue = new SerializedObject(res);
var globalValProp = globalValue.FindProperty("value");
var prevEnabled = GUI.enabled;
GUI.enabled = false;
EditorGUI.PropertyField(rects[2], globalValProp, new GUIContent(""));
GUI.enabled = prevEnabled;
}
}
else
{
EditorGUI.PropertyField(rects[2], defaultProp, new GUIContent(""));
}
SerializedProperty scopeProp = variableObject.FindProperty("scope");
scope = (VariableScope)EditorGUI.EnumPopup(rects[3], variable.Scope);
scopeProp.enumValueIndex = (int)scope;
@ -202,7 +241,8 @@ namespace Fungus.EditorUtils
GUI.backgroundColor = Color.white;
}
public virtual float GetItemHeight(int index) {
public virtual float GetItemHeight(int index)
{
return fixedItemHeight != 0f
? fixedItemHeight
: EditorGUI.GetPropertyHeight(this[index], GUIContent.none, false)

65
Assets/Fungus/Scripts/Utils/GlobalVariables.cs

@ -0,0 +1,65 @@
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine;
using System.Collections.Generic;
using System;
namespace Fungus
{
/// <summary>
/// Storage for a collection of fungus variables that can then be accessed globally.
/// </summary>
public class GlobalVariables : MonoBehaviour
{
private Flowchart holder;
Dictionary<string, Variable> variables = new Dictionary<string, Variable>();
void Awake()
{
holder = new GameObject("GlobalVariables").AddComponent<Flowchart>();
holder.transform.parent = transform;
}
public Variable GetVariable(string variableKey)
{
Variable v = null;
variables.TryGetValue(variableKey, out v);
return v;
}
public VariableBase<T> GetOrAddVariable<T>(string variableKey, T defaultvalue, Type type)
{
Variable v = null;
VariableBase<T> vAsT = null;
var res = variables.TryGetValue(variableKey, out v);
if(res && v != null)
{
vAsT = v as VariableBase<T>;
if (vAsT != null)
{
return vAsT;
}
else
{
Debug.LogError("A fungus variable of name " + variableKey + " already exists, but of a different type");
}
}
else
{
//create the variable
vAsT = holder.gameObject.AddComponent(type) as VariableBase<T>;
vAsT.Value = defaultvalue;
vAsT.Key = variableKey;
vAsT.Scope = VariableScope.Public;
variables[variableKey] = vAsT;
holder.Variables.Add(vAsT);
}
return vAsT;
}
}
}

12
Assets/Fungus/Scripts/Utils/GlobalVariables.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 76be178414b4c9a4e91e79b0fa476ef4
timeCreated: 1507794792
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
Assets/FungusExamples/GlobalVariable.meta

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 7f3f86666c1a6e144a2c36aab642aec9
folderAsset: yes
timeCreated: 1507723272
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

1382
Assets/FungusExamples/GlobalVariable/GlobalVar.unity

File diff suppressed because it is too large Load Diff

8
Assets/FungusExamples/GlobalVariable/GlobalVar.unity.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ade6b5a778f23e74ab2c1b5837bb8199
timeCreated: 1507723277
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save