Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #669 from stevehalliwell/BlockReferenceDrawer
Added BlockReference, a simple data type with a property drawer that …
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.