diff --git a/Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs b/Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs new file mode 100644 index 000000000..71e020d4a --- /dev/null +++ b/Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs @@ -0,0 +1,53 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; + +namespace Fungus.EditorUtils +{ + /// + /// Custom drawer for the BlockReference, allows for more easily selecting a target block in external c# + /// scripts. + /// + [CustomPropertyDrawer(typeof(Fungus.BlockReference))] + public class BlockReferenceDrawer : PropertyDrawer + { + public Fungus.Flowchart lastFlowchart; + + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + var l = EditorGUI.BeginProperty(position, label, property); + position = EditorGUI.PrefixLabel(position, l); + position.height = EditorGUIUtility.singleLineHeight; + var block = property.FindPropertyRelative("block"); + + Fungus.Block b = block.objectReferenceValue as Fungus.Block; + + if (block.objectReferenceValue != null && lastFlowchart == null) + { + if (b != null) + { + lastFlowchart = b.GetFlowchart(); + } + } + + lastFlowchart = EditorGUI.ObjectField(position, lastFlowchart, typeof(Fungus.Flowchart), true) as Fungus.Flowchart; + position.y += EditorGUIUtility.singleLineHeight; + if (lastFlowchart != null) + b = Fungus.EditorUtils.BlockEditor.BlockField(position, new GUIContent("None"), lastFlowchart, b); + else + EditorGUI.PrefixLabel(position, new GUIContent("Flowchart Required")); + + block.objectReferenceValue = b; + + block.serializedObject.ApplyModifiedProperties(); + property.serializedObject.ApplyModifiedProperties(); + EditorGUI.EndProperty(); + } + + public override float GetPropertyHeight(SerializedProperty property, GUIContent label) + { + return EditorGUIUtility.singleLineHeight * 2; + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs.meta b/Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs.meta new file mode 100644 index 000000000..f6f316140 --- /dev/null +++ b/Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 9755f9b06e534264e94b45e16e12338e +timeCreated: 1523271928 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/Utils/BlockReference.cs b/Assets/Fungus/Scripts/Utils/BlockReference.cs new file mode 100644 index 000000000..4d9693c93 --- /dev/null +++ b/Assets/Fungus/Scripts/Utils/BlockReference.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Fungus +{ + /// + /// A simple struct wrapping a reference to a Fungus Block. Allows for BlockReferenceDrawer. + /// This is the recommended way to directly reference a fungus block in external c# scripts, + /// as it will give you an inspector field that gives a drop down of all the blocks on a + /// flowchart, in a similar way to what you would expect from selecting a block on a command. + /// + [System.Serializable] + public struct BlockReference + { + public Block block; + + public void Execute() + { + if (block != null) + block.StartExecution(); + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Utils/BlockReference.cs.meta b/Assets/Fungus/Scripts/Utils/BlockReference.cs.meta new file mode 100644 index 000000000..bca4e3586 --- /dev/null +++ b/Assets/Fungus/Scripts/Utils/BlockReference.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: ab8298053f5d7eb49b16e15fb3570c3c +timeCreated: 1523271928 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: