Skip to content

Instantly share code, notes, and snippets.

@ahmed-shariff
Last active October 12, 2024 02:48
Show Gist options
  • Select an option

  • Save ahmed-shariff/e9e87b45cd04cf4a2e2bb732e4701aa1 to your computer and use it in GitHub Desktop.

Select an option

Save ahmed-shariff/e9e87b45cd04cf4a2e2bb732e4701aa1 to your computer and use it in GitHub Desktop.
When I need a dict like field in a script in any of packages ,I use a struct with two fields. This is a simple drawer for that.
using UnityEditor;
using UnityEngine;
namespace ubco.ovilab.ViconUnityStream.Editor
{
[CanEditMultipleObjects]
[CustomPropertyDrawer(typeof(S), true)]
public class SimpleTypeValueDrawer: PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
SerializedProperty keyProp = property.FindPropertyRelative("key");
SerializedProperty valueProp = property.FindPropertyRelative("value");
float width = EditorGUIUtility.currentViewWidth;
float valueHeight = EditorGUIUtility.singleLineHeight;
// Calculate rects
Rect keyRect = new Rect(position.x, position.y, position.width * 0.5f, valueHeight);
Rect valueRect = new Rect(position.x + position.width * 0.51f, position.y, position.width * 0.5f, valueHeight);
// Draw fields - pass GUIContent.none to each so they are drawn without labels
EditorGUI.PropertyField(keyRect, keyProp, GUIContent.none);
EditorGUI.PropertyField(valueRect, valueProp, GUIContent.none);
EditorGUI.EndProperty();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment