Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Created November 4, 2024 19:15
Show Gist options
  • Save yasirkula/e2d0d569012e8b9de19bb6a0f9c88e69 to your computer and use it in GitHub Desktop.
Save yasirkula/e2d0d569012e8b9de19bb6a0f9c88e69 to your computer and use it in GitHub Desktop.
Adding comments to Inspector via a component in Unity
#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
}
@yasirkula
Copy link
Author

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.

image

@yusam-hub
Copy link

Cool!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment