Created
November 4, 2024 19:15
-
-
Save yasirkula/e2d0d569012e8b9de19bb6a0f9c88e69 to your computer and use it in GitHub Desktop.
Adding comments to Inspector via a component in Unity
This file contains 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
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
using UnityEngine; | |
[AddComponentMenu("Comment")] | |
public class CommentComponent : MonoBehaviour | |
{ | |
#if UNITY_EDITOR | |
[SerializeField] | |
private string comment; | |
[CustomEditor(typeof(CommentComponent))] | |
[CanEditMultipleObjects] | |
public class CommentComponentEditor : Editor | |
{ | |
private SerializedProperty commentProp; | |
private GUIStyle commentStyle; | |
private float inspectorWidth; | |
protected void OnEnable() | |
{ | |
commentProp = serializedObject.FindProperty("comment"); | |
} | |
public override void OnInspectorGUI() | |
{ | |
if (commentStyle == null) | |
{ | |
commentStyle = new GUIStyle(EditorStyles.wordWrappedLabel); | |
commentStyle.normal.textColor = commentStyle.focused.textColor = commentStyle.hover.textColor = Color.green; | |
} | |
serializedObject.Update(); | |
Rect rect = EditorGUILayout.GetControlRect(false, commentStyle.CalcHeight(new GUIContent(commentProp.stringValue), inspectorWidth)); | |
if (Event.current.type == EventType.Repaint) | |
inspectorWidth = rect.width; | |
EditorGUI.DrawRect(rect, Color.black); // Draw dark background | |
EditorGUI.BeginProperty(rect, GUIContent.none, commentProp); | |
EditorGUI.BeginChangeCheck(); | |
string newComment = EditorGUI.TextArea(rect, commentProp.stringValue, commentStyle); | |
if (EditorGUI.EndChangeCheck()) | |
commentProp.stringValue = newComment; | |
EditorGUI.EndProperty(); | |
serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How To
Simply add this component to your objects. Note that it's unfortunately harder to edit the comment on Light editor skin due to the caret blending in with the background.