Skip to content

Instantly share code, notes, and snippets.

@anchan828
Created December 10, 2012 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anchan828/4250814 to your computer and use it in GitHub Desktop.
Save anchan828/4250814 to your computer and use it in GitHub Desktop.
using UnityEngine;
/// <summary>
/// <para>通常使うスクリプトファイル。ゲームオブジェクトにアタッチすればインスペクターに表示される</para>
/// <para>今回は既にあるRangeAttributeを自作します。</para>
/// </summary>
public class Example : MonoBehaviour
{
/// <summary>
/// 0から10までしか設定することの出来ないスライダーを作成します。
/// </summary>
[Hoge(0f, 10f)]
public float num;
}
using UnityEngine;
/// <summary>
/// 自分で作成したAttribute。必ずPropertyAttributeを継承する必要があります。
/// </summary>
public class HogeAttribute : PropertyAttribute
{
/// <summary>
/// 最小値。ここの値をHogeDrawerで読み込みます。
/// </summary>
public readonly float min;
/// <summary>
/// 最大値。ここの値をHogeDrawerで読み込みます。
/// </summary>
public readonly float max;
/// <summary>
/// Example.csで記述したものはここを呼び出しています。
/// </summary>
/// <param name='min'>
/// 最小値
/// </param>
/// <param name='max'>
/// 最大値
/// </param>
public HogeAttribute (float min, float max)
{
this.min = min;
this.min = max;
}
}
using UnityEngine;
using UnityEditor;
/// <summary>
/// <para>HogeDrawerでインスペクターに表示するGUIスクリプトを記述します。ここがエディタ拡張の部分になります。</para>
/// <para>必ずPropertyDrawerを継承して下さい</para>
/// </summary>
[CustomPropertyDrawer(typeof(HogeAttribute))]
public class HogeDrawer : PropertyDrawer
{
/// <summary>
/// Raises the GU event.
/// </summary>
/// <param name='pos'>
/// <para>インスペクターで表示されるべき(だった)位置。</para>
/// <para>この値を利用することで正常の位置にカスタムプロパティを描画することができます。</para>
/// </param>
/// <param name='prop'>
/// <para>プロパティの値。</para>
/// <para>今回はExample.csのnumを指します。</para>
/// </param>
/// <param name='content'>
/// <para>プロパティ名</para>
/// <para>今回はExample.csのNum(デフォルトで頭文字は大文字になる)を指します。
/// </param>
public override void OnGUI (Rect pos, SerializedProperty prop, GUIContent content)
{
/// PropertyDrawerを継承するとattributeを取得できます。これはHogeAttributeのことを指すのでキャストします。
HogeAttribute hoge = (HogeAttribute)attribute;
/// EditorGUI.Sliderでスライダーを作成します。
EditorGUI.Slider (pos, prop, hoge.max, hoge.min, content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment