Skip to content

Instantly share code, notes, and snippets.

@soraphis
Created April 1, 2016 16:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soraphis/df2ca801c94fa95e0c940b40b3655284 to your computer and use it in GitHub Desktop.
Save soraphis/df2ca801c94fa95e0c940b40b3655284 to your computer and use it in GitHub Desktop.
Unity3D Handles by Propertydrawer
#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