- Home /
Question by
benbenmushi · Jul 17, 2014 at 05:38 AM ·
drag-and-droppropertydraweranimationcurve
PropertyDrawer DragAndDrop AnimationCurve
Hi,
so i'm here, trying to save my own AnimationCurve as .asset into my unity folder. So far i can save them into a simple ScriptableObject with one field (AnimationCurve) but i can't load my saved AnimationCurve into a script's InspectorField
Here the code full code:
[CustomPropertyDrawer(typeof(AnimationCurve))]
public class AnimationCurveDrawer : PropertyDrawer
{
private T CreateAsset<T>() where T : ScriptableObject
{
T asset = ScriptableObject.CreateInstance<T>();
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (path == "")
{
path = "Assets";
}
else if (Path.GetExtension(path) != "")
{
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
}
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T).ToString() + ".asset");
AssetDatabase.CreateAsset(asset, assetPathAndName);
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
return asset;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 17f;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (Event.current.type == EventType.DragExited && DragAndDrop.objectReferences.Length > 0)
{
for (int i = 0; i < DragAndDrop.objectReferences.Length; i++)
{
if (DragAndDrop.objectReferences[i].GetType() == typeof(AnimationCurveCustomAsset))
{
Debug.Log("Hey ? " + (DragAndDrop.objectReferences[i] as AnimationCurveCustomAsset).curve);
property.animationCurveValue = (DragAndDrop.objectReferences[i] as AnimationCurveCustomAsset).curve;
return;
}
}
}
if (Event.current.type == EventType.MouseDown && Event.current.button == 1)
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Save"), false, (q) =>
{
this.CreateAsset<AnimationCurveCustomAsset>().curve = property.animationCurveValue;
}, "Save");
menu.ShowAsContext();
}
property.animationCurveValue = EditorGUI.CurveField(position, label, property.animationCurveValue);
}
}
Am I doing something wrong here ?
(btw: I gived up on how the MouseCursor look...)
Comment
Answer by benbenmushi · Jul 17, 2014 at 11:17 AM
There's the working code:
[CustomPropertyDrawer(typeof(AnimationCurve))]
public class AnimationCurveDrawer : PropertyDrawer
{
private T CreateAsset<T>() where T : ScriptableObject
{
T asset = ScriptableObject.CreateInstance<T>();
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (path == "")
{
path = "Assets";
}
else if (Path.GetExtension(path) != "")
{
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
}
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T).ToString() + ".asset");
AssetDatabase.CreateAsset(asset, assetPathAndName);
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
return asset;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 17f;
}
AnimationCurve last;
private AnimationCurve getDragedCurve()
{
if (DragAndDrop.objectReferences.Length > 0 && DragAndDrop.objectReferences[0] != null && DragAndDrop.objectReferences[0].GetType() == typeof(AnimationCurveCustomAsset))
return (DragAndDrop.objectReferences[0] as AnimationCurveCustomAsset).curve;
return null;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (Event.current.type == EventType.DragUpdated && position.Contains(Event.current.mousePosition) && this.getDragedCurve() != null)
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (Event.current.type == EventType.DragPerform && DragAndDrop.objectReferences.Length > 0 && position.Contains(Event.current.mousePosition))
this.last = this.getDragedCurve();
if (Event.current.type == EventType.MouseDown && Event.current.button == 1 && position.Contains(Event.current.mousePosition))
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Save"), false, (q) =>
{
AnimationCurveCustomAsset assetSaved = this.CreateAsset<AnimationCurveCustomAsset>();
assetSaved.curve = property.animationCurveValue;
EditorUtility.SetDirty(assetSaved);
}, "Save");
menu.ShowAsContext();
}
property.animationCurveValue = EditorGUI.CurveField(position, label, (this.last != null) ? this.last : property.animationCurveValue);
if (this.last != null && Event.current.type == EventType.Repaint)
this.last = null;
}
}