Skip to content

Commit f087e16

Browse files
committed
Adding 'ReadOnly' attributions for Unity Editor UI
1 parent d1de01d commit f087e16

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.meta

ReadOnly/Editor/ReadOnlyDrawer.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
[CustomPropertyDrawer( typeof( ReadOnlyAttribute ) )]
5+
public class ReadOnlyDrawer : PropertyDrawer {
6+
7+
public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) {
8+
return EditorGUI.GetPropertyHeight( property, label, true );
9+
}
10+
11+
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) {
12+
using (var scope = new EditorGUI.DisabledGroupScope(true)) {
13+
EditorGUI.PropertyField( position, property, label, true );
14+
}
15+
}
16+
17+
}
18+
19+
[CustomPropertyDrawer( typeof( BeginReadOnlyGroupAttribute ) )]
20+
public class BeginReadOnlyGroupDrawer : DecoratorDrawer {
21+
22+
public override float GetHeight() { return 0; }
23+
24+
public override void OnGUI( Rect position ) {
25+
EditorGUI.BeginDisabledGroup( true );
26+
}
27+
28+
}
29+
30+
[CustomPropertyDrawer( typeof( EndReadOnlyGroupAttribute ) )]
31+
public class EndReadOnlyGroupDrawer : DecoratorDrawer {
32+
33+
public override float GetHeight() { return 0; }
34+
35+
public override void OnGUI( Rect position ) {
36+
EditorGUI.EndDisabledGroup();
37+
}
38+
39+
}

ReadOnly/ReadOnlyAttribute.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEngine;
2+
3+
/// <summary>
4+
/// Display a field as read-only in the inspector.
5+
/// CustomPropertyDrawers will not work when this attribute is used.
6+
/// </summary>
7+
/// <seealso cref="BeginReadOnlyGroupAttribute"/>
8+
/// <seealso cref="EndReadOnlyGroupAttribute"/>
9+
public class ReadOnlyAttribute : PropertyAttribute { }
10+
11+
/// <summary>
12+
/// Display one or more fields as read-only in the inspector.
13+
/// Use <see cref="EndReadOnlyGroupAttribute"/> to close the group.
14+
/// Works with CustomPropertyDrawers.
15+
/// </summary>
16+
/// <seealso cref="EndReadOnlyGroupAttribute"/>
17+
/// <seealso cref="ReadOnlyAttribute"/>
18+
public class BeginReadOnlyGroupAttribute : PropertyAttribute { }
19+
20+
/// <summary>
21+
/// Use with <see cref="BeginReadOnlyGroupAttribute"/>.
22+
/// Close the read-only group and resume editable fields.
23+
/// </summary>
24+
/// <seealso cref="BeginReadOnlyGroupAttribute"/>
25+
/// <seealso cref="ReadOnlyAttribute"/>
26+
public class EndReadOnlyGroupAttribute : PropertyAttribute { }

0 commit comments

Comments
 (0)