Browse Source

Merge pull request #669 from stevehalliwell/BlockReferenceDrawer

Added BlockReference, a simple data type with a property drawer that …
master
Chris Gregan 7 years ago committed by GitHub
parent
commit
bbc4c4f763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 53
      Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs
  2. 13
      Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs.meta
  3. 24
      Assets/Fungus/Scripts/Utils/BlockReference.cs
  4. 13
      Assets/Fungus/Scripts/Utils/BlockReference.cs.meta

53
Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs

@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace Fungus.EditorUtils
{
/// <summary>
/// Custom drawer for the BlockReference, allows for more easily selecting a target block in external c#
/// scripts.
/// </summary>
[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;
}
}
}

13
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:

24
Assets/Fungus/Scripts/Utils/BlockReference.cs

@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Fungus
{
/// <summary>
/// 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.
/// </summary>
[System.Serializable]
public struct BlockReference
{
public Block block;
public void Execute()
{
if (block != null)
block.StartExecution();
}
}
}

13
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:
Loading…
Cancel
Save