// This code is part of the Fungus library (https://github.com/snozbot/fungus)
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Fungus.EditorUtils
{
///
/// Fungus Variables are drawn via EditorGUI.Property by default, however, some types may require a custom replacement.
/// The most common example is a Quaternion, Unity does not show you a quaternion as 4 floats, it shows you
/// the euler angles, we also want to do that here
///
/// This class is delegated to by editors to draw the actual variable property line.
///
public static class CustomVariableDrawerLookup
{
//If you create new types that require custom singleline drawers, add them here
public static Dictionary> typeToDrawer =
new Dictionary>()
{
{
typeof(QuaternionVariable),
(rect, valueProp) => {valueProp.quaternionValue = UnityEngine.Quaternion.Euler(UnityEditor.EditorGUI.Vector3Field(rect, new UnityEngine.GUIContent(""), valueProp.quaternionValue.eulerAngles)); }
},
{
typeof(Vector4Variable),
(rect, valueProp) => {valueProp.vector4Value = UnityEditor.EditorGUI.Vector4Field(rect, new UnityEngine.GUIContent(""), valueProp.vector4Value); }
},
};
public static bool GetDrawer(System.Type type, out System.Action drawer)
{
return typeToDrawer.TryGetValue(type, out drawer);
}
///
/// Called by editors that want a single line variable property drawn
///
///
///
///
public static void DrawCustomOrPropertyField(System.Type type, Rect rect, SerializedProperty prop)
{
System.Action drawer = null;
//delegate actual drawing to the variableInfo
var foundDrawer = typeToDrawer.TryGetValue(type, out drawer);
if (foundDrawer)
{
drawer(rect, prop);
}
else
{
EditorGUI.PropertyField(rect, prop, new GUIContent(""));
}
}
}
}