Advertisement
nbannister

Unity SerializableDecimalDrawer

Oct 8th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomPropertyDrawer (typeof (SerializableDecimal))]
  5. public class SerializableDecimalDrawer : PropertyDrawer {
  6.     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
  7.         var obj = property.serializedObject.targetObject;
  8.         var inst = (SerializableDecimal) this.fieldInfo.GetValue (obj);
  9.         var fieldRect = EditorGUI.PrefixLabel (position, label);
  10.         string text = GUI.TextField (fieldRect, inst.value.ToString ());
  11.         if (GUI.changed) {
  12.             decimal val;
  13.             if (decimal.TryParse (text, out val)) {
  14.                 inst.value = val;
  15.                 property.serializedObject.ApplyModifiedProperties ();
  16.             }
  17.         }
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement