Created
January 19, 2014 15:55
-
-
Save thallippoli/8506758 to your computer and use it in GitHub Desktop.
ReadOnlyProperty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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