Skip to content

Instantly share code, notes, and snippets.

@thallippoli
Created January 19, 2014 15:55
Show Gist options
  • Save thallippoli/8506757 to your computer and use it in GitHub Desktop.
Save thallippoli/8506757 to your computer and use it in GitHub Desktop.
ReadOnlyProperty
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public abstract class ReadOnlyPropertyAttribute : PropertyAttribute
{
public string Displayname { get; private set; }
public ReadOnlyPropertyAttribute( string displayName )
{
this.Displayname = displayName;
}
}
public class ReadOnlyObjectAttribute : ReadOnlyPropertyAttribute
{
public ReadOnlyObjectAttribute( string displayName ) : base( displayName )
{
}
}
public class ReadOnlyVector3Attribute : ReadOnlyPropertyAttribute
{
public ReadOnlyVector3Attribute( string displayName ) : base( displayName )
{
}
}
#if UNITY_EDITOR
// [AS] Property drawers go here ============================================================================================
[ CustomPropertyDrawer( typeof( ReadOnlyObjectAttribute ) ) ]
public class ReadOnlyObjectDrawer : PropertyDrawer
{
public override void OnGUI( Rect position, SerializedProperty prop, GUIContent label )
{
EditorGUI.ObjectField( position, ( attribute as ReadOnlyPropertyAttribute ).Displayname + " (Read Only)", prop.objectReferenceValue, typeof( System.Object ), true );
}
}
[ CustomPropertyDrawer( typeof( ReadOnlyVector3Attribute ) ) ]
public class ReadOnlyVector3Drawer : PropertyDrawer
{
public override void OnGUI( Rect position, SerializedProperty prop, GUIContent label )
{
EditorGUI.Vector3Field( position, ( attribute as ReadOnlyPropertyAttribute ).Displayname + " (Read Only)", prop.vector3Value );
}
public override float GetPropertyHeight( SerializedProperty property, GUIContent label )
{
return base.GetPropertyHeight( property, label ) * 2.0f;
}
}
// ==================================================================================================================================
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment