Skip to content

Instantly share code, notes, and snippets.

@nevarman
Last active February 28, 2017 15:17
Show Gist options
  • Save nevarman/58d9b39a2a2ced4f9fed8d4f8b597b1c to your computer and use it in GitHub Desktop.
Save nevarman/58d9b39a2a2ced4f9fed8d4f8b597b1c to your computer and use it in GitHub Desktop.
Comment Attribute for Unity Scripts. Add this attribute to top of first property in a class for a quick comment on Editor
using System;
using UnityEngine;
using System.Collections;
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public class CommentAttribute : PropertyAttribute
{
/// <summary>
/// <para>The header text.</para>
/// </summary>
public readonly string comment;
/// <summary>
/// The height
/// </summary>
public readonly float height = 100f;
/// <summary>
/// Add a comment above some fields in the Inspector.
/// </summary>
/// <param name="comment">The header text.</param>
public CommentAttribute(string comment)
{
this.comment = comment;
}
/// <summary>
/// Initializes a new instance of the <see cref="CommentAttribute"/> class.
/// </summary>
/// <param name="comment">The comment.</param>
/// <param name="height">The height.</param>
public CommentAttribute(string comment, float height)
{
this.comment = comment;
this.height = height;
}
}
#if UNITY_EDITOR
using UnityEditor;
[CustomPropertyDrawer(typeof(CommentAttribute))]
public class CommentDrawer : DecoratorDrawer
{
public override void OnGUI(Rect position)
{
position.height -= 2f;
position = EditorGUI.IndentedRect(position);
GUI.Box(position, string.Empty);
var text = new GUIContent((attribute as CommentAttribute).comment);
var style = EditorStyles.textArea;
style.normal.textColor = Color.green;
style.fontStyle = FontStyle.Bold;
GUI.TextArea(position, text.text, style);
}
public override float GetHeight()
{
return (this.attribute as CommentAttribute).height;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment