Skip to content

Instantly share code, notes, and snippets.

@BrianMacIntosh
Created December 9, 2019 01:33
Show Gist options
  • Save BrianMacIntosh/94afe7e08b3c0edd75d71a6d59d41d1b to your computer and use it in GitHub Desktop.
Save BrianMacIntosh/94afe7e08b3c0edd75d71a6d59d41d1b to your computer and use it in GitHub Desktop.
Base class for a Unity property drawer for structures like "quantity" of "thing"
using UnityEditor;
using UnityEngine;
// Author: Brian MacIntosh (The Periodic Group)
// MIT License
/// <summary>
/// Base class that can be extended to quickly create a property drawer for a structure containing
/// data like "quantity" of "thing".
/// </summary>
public abstract class ItemQuantityPropertyDrawer : PropertyDrawer
{
/// <summary>
/// Name of the quantity/stack size property.
/// </summary>
public abstract string QuantityPropName { get; }
/// <summary>
/// Name of the item/object property.
/// </summary>
public abstract string ItemPropName { get; }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
Rect quantityPosition = position;
quantityPosition.width = 40f;
Rect resourcePosition = position;
resourcePosition.xMin += quantityPosition.width + 4f;
EditorGUI.PropertyField(quantityPosition, property.FindPropertyRelative(QuantityPropName), GUIContent.none);
EditorGUI.PropertyField(resourcePosition, property.FindPropertyRelative(ItemPropName), GUIContent.none);
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment