Skip to content

Instantly share code, notes, and snippets.

@anchan828
Created December 16, 2012 14:34
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/4307873 to your computer and use it in GitHub Desktop.
Save anchan828/4307873 to your computer and use it in GitHub Desktop.
自分だけのPropertyDrawerを作ろう! ref: http://qiita.com/kyusyukeigo/items/8be4cdef97496a68a39d
using UnityEngine;
using System.Collections;
public class SampleScript : MonoBehaviour {
public float hp;
void Update()
{
Debug.Log( hp );
}
}
using UnityEngine;
public class Range2Attribute : PropertyAttribute
{
public float min;
public float max;
public Range2Attribute (float min, float max)
{
this.min = min;
this.max = max;
}
}
using UnityEngine;
public class Range2AttributeExample : MonoBehaviour
{
[Range2( 0f, 100f )]
public float hp;
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer( typeof ( Range2Attribute ) )]
public class Range2Drawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
Range2Attribute range2Attribute = (Range2Attribute)attribute;
if (property.propertyType == SerializedPropertyType.Float) {
EditorGUI.Slider (position, property, range2Attribute.min, range2Attribute.max, label);
}
}
}
using UnityEngine;
using System.Collections;
public class SampleScript : MonoBehaviour {
[Range( 0f, 100f )]
public float hp;
}
using UnityEngine;
public class SampleAttribute : PropertyAttribute
{
public bool init = false;
public SampleAttribute ()
{
}
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(SampleAttribute))]
public class SampleDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
if (sampleAttribute.init == false) {
sampleAttribute.init = true;
return;
}
...
}
public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
{
if (sampleAttribute.init == false) {
return 0;
}
return base.GetPropertyHeight (property, label);
}
}
using UnityEngine;
public class SampleScript : MonoBehaviour
{
[Range2( 0f, 100f )]
public float hp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment