Skip to content

Instantly share code, notes, and snippets.

@D4KU
Created January 8, 2022 01:22
Show Gist options
  • Select an option

  • Save D4KU/f7bbcafd58962c008c86e6e4036ec5f9 to your computer and use it in GitHub Desktop.

Select an option

Save D4KU/f7bbcafd58962c008c86e6e4036ec5f9 to your computer and use it in GitHub Desktop.
Unity Comment Component
using UnityEngine;
/// <summary>
/// This component serves no functional purpose.
/// Use it to document objects in the scene.
/// </summary>
public class Comment : MonoBehaviour
{
[TextArea]
[Tooltip("Actual content of the comment.")]
public string content;
[Tooltip("Gameobject references to keep in one place for convenience.")]
public GameObject[] references;
[Tooltip("Who wrote the comment?")]
public string author;
[HideInInspector]
[Tooltip("Objects this comment refers to.")]
public int regarding;
[Tooltip("Inspector background color for highlighting.")]
public Color color = new Color(0f, 0f, 0f, 0.2f);
[Tooltip("If true, this component self-destructs on Play.")]
public bool destroyOnStart = true;
void Start()
{
if (destroyOnStart)
Destroy(this);
}
}
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Comment))]
[CanEditMultipleObjects]
public class CommentEditor : Editor
{
public static readonly string[] regardingOptions = {
"Children",
"Component above",
"Components above (up to previous comment)",
"Component below",
"Components below (down to next comment)",
"Gameobject",
"Prefab",
"Scene",
"Siblings",
};
public override void OnInspectorGUI()
{
Comment comment = (Comment)target;
Rect content = EditorGUILayout.BeginVertical();
base.OnInspectorGUI();
comment.regarding = EditorGUILayout.MaskField(
new GUIContent(
"Regarding",
"Object this comment refers to."
),
comment.regarding,
regardingOptions
);
EditorGUILayout.EndVertical();
//Adjust background color margins for Unity versions
//2019.3 and newer
#if UNITY_2019_3_OR_NEWER
float xmargin = 18.0f;
float ymarginTop = 25.0f;
float ymarginBottom = 32.0f;
#else
float xmargin = 14.0f;
float ymarginTop = 18.0f;
float ymarginBottom = 25.0f;
#endif
content = new Rect(
content.x - xmargin,
content.y - ymarginTop,
EditorGUIUtility.currentViewWidth,
content.height + ymarginBottom
);
EditorGUI.DrawRect(content, comment.color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment