Created
April 1, 2016 16:33
-
-
Save soraphis/df2ca801c94fa95e0c940b40b3655284 to your computer and use it in GitHub Desktop.
Unity3D Handles by Propertydrawer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
namespace Editor { | |
using UnityEditor; | |
[CustomEditor(typeof(MonoBehaviour), true, isFallback = true)] | |
public class PositionHandleEditor : UnityEditor.Editor { | |
void OnSceneGUI() { | |
var t = target as MonoBehaviour; | |
if(t == null) return; | |
foreach(var fieldInfo in t.GetType().GetFields()) { | |
try { | |
var attribs = fieldInfo.GetCustomAttributes(typeof(PositionHandleAttribute), false); | |
if(attribs.Length > 0 && fieldInfo.FieldType == typeof(Vector3)) { | |
Vector3 v = (Vector3) fieldInfo.GetValue(t); | |
v = Handles.PositionHandle((Vector3)v, Quaternion.identity); | |
Handles.Label(v, fieldInfo.Name); | |
fieldInfo.SetValue(t, v); | |
} | |
}catch(System.Exception) { | |
// ignored | |
} | |
} | |
} | |
} | |
} | |
#endif | |
public class PositionHandleAttribute : PropertyAttribute {} | |
/// ------------ | |
public class DummyClass : MonoBehaviour{ | |
[PositionHandle] public Vector3 TargetPosition; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment