Chris Gregan
6 years ago
committed by
GitHub
6 changed files with 277 additions and 18 deletions
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: e5d801a1ae7a4c144b6c561f8b7f8852 |
||||||
|
MonoImporter: |
||||||
|
externalObjects: {} |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
||||||
|
assetBundleName: |
||||||
|
assetBundleVariant: |
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: ce82e02decf51ce439183c17e926422f |
||||||
|
MonoImporter: |
||||||
|
externalObjects: {} |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
||||||
|
assetBundleName: |
||||||
|
assetBundleVariant: |
Loading…
Reference in new issue