青空の月

PHP, Unity, C#, アプリ開発関連について。

GameObjectにコメントを残す拡張

 GameObjectにコメントを残すサンプル。みんな何かしらコメント残せるようにしてるとは思うけど。
 

 

 ObjectComment.cs

using UnityEngine;

public class ObjectComment : MonoBehaviour {

#if UNITY_EDITOR
    public string comment = "";
#else
#endif

}

 

 ObjectCommentEditor.cs

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(ObjectComment))]
public class ObjectCommentEditor : Editor {

    private string option = "";
	private bool isInputOption = false;
	
	//インスペクタ上の表示設定
	public override void OnInspectorGUI()
	{
		if (isInputOption)
		{
			GUILayout.BeginHorizontal();
			if (GUILayout.Button("年月日"))
			{
				 option = System.DateTime.Now.ToString("yyyy-MM-dd");
			}
			if (GUILayout.Button("年月日時"))
			{
				 option = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
			}
			GUILayout.EndVertical();

			GUILayout.BeginHorizontal();
			if (GUILayout.Button("######"))
				 option = "############################";

			if (GUILayout.Button("------"))
				 option = "----------------------------";
			if (GUILayout.Button("======"))
				 option = "=============================";
			if (GUILayout.Button("******"))
				 option = "*****************************";

			GUILayout.EndVertical();

			option = EditorGUILayout.TextArea(option);
			EditorGUILayout.Space();
		}
		else
		{
			 if (GUILayout.Button("入力補助", GUILayout.Height(30)))
				 isInputOption = true;

		}
		
		ObjectComment myMemo = (ObjectComment)target;

		GUILayout.Label("コメント:");
		myMemo.comment = EditorGUILayout.TextArea(myMemo.comment);
	}



	[MenuItem("Component/etc/Add ObjectComment Component")]
	[MenuItem("GameObject/Create Other/Add ObjectComment Component", false, 200)]
	private static void AddObjectCommentComponent()
	{
		if (Selection.activeTransform != null)
		{
			Selection.activeGameObject.AddComponent();
		}
	}
}

 

 ObjectCommentEditor.csはEditorフォルダー配下に入れる必要がある。

使い方はゲームオブジェクトを選択して、メニューからかHirerarchryのcreateから選択する。

f:id:masa795:20130318003009j:plain

f:id:masa795:20130318003023j:plain

 

こんな感じでコメントを記入できる。日本語も改行も使える。

自分はオプションで日時とかをテキストボックスからコピペして使えるようにしている。定型文とかあればボタンで入力できるようにしとくと便利。

f:id:masa795:20130318003241j:plain

 

 

 

インスペクター上のコンポーネントの並びを簡単に変えられればもっといいんだけどなぁ。

ObjectComment.csを別のクラス名で複数作成しといて用途別にゲームオブジェクトにアタッチすると検索しやすくなるので管理が楽になりそう。