// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using UltimateXR.Attributes;
using UnityEditor;
using UnityEngine;
namespace UltimateXR.Editor.Attributes
{
///
/// Custom property drawer for inspector fields that use the ReadOnly attribute.
///
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyPropertyDrawer : PropertyDrawer
{
#region Public Overrides PropertyDrawer
///
/// Called when the GUI wants to know the height needed to draw the property.
///
/// SerializedProperty that needs to be drawn
/// Label used
/// Height in pixels
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var roAttr = (ReadOnlyAttribute)attribute;
return Application.isPlaying ? roAttr.HideInPlayMode ? 0 : EditorGUIUtility.singleLineHeight :
roAttr.HideInEditMode ? 0 : EditorGUIUtility.singleLineHeight;
}
#endregion
#region Unity
///
/// Called when the GUI needs to draw the property.
///
/// GUI position
/// Property to draw
/// Property label
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var roAttr = (ReadOnlyAttribute)attribute;
if (roAttr.HideInEditMode && !Application.isPlaying)
{
return;
}
GUI.enabled = !Application.isPlaying && roAttr.OnlyWhilePlaying;
EditorGUI.PropertyField(position, property, label);
GUI.enabled = true;
}
#endregion
}
}