Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Editor script to dump curves of animation clip to text file 1 Answer

preventing animation from tweening 1 Answer

Undocumented property: Keyframe.tangentMode 3 Answers

Can you guys animate the renderer's param in 4.3? 1 Answer

What is the length of the Keyframe tangent? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges