• 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
3
Question by DavidDebnar · Feb 01, 2014 at 10:41 PM · animationreflectionanimationcurvepropertygetsetter

Use AnimationCurve to animate a property?

Hey guys. I have a system which generates some AnimationCurves, using .SetCurve assigns them to an AnimationClip, which is then played on loop. It works fine, although I haven't been able to animate properties with custom get/setters.

I have a variable called Distance, which, when changed, updates the size of a BoxCollider2D component. This works when either set directly 'Distance = 10' or via Reflection (which I suppose is what AnimationCurves use). Problem is, that I'm guessing, that AnimationCurves use FieldInfo to to cache the Reflection and not PropertyInfo, which makes get/setters with side effects not work.

To sum up: Am I right thinking that Curves only support fields? If no, then what's causing the issue - the solution?

I'm open to any other ideas too.

Also, if you know of a way to detect variable change, which doesn't involve a get/setter, then I'd be pleased to now. I thought about caching the old value and than have an Update loop running, but I'm trying to optimize as much as possible and having a loop running just to check if a variable has changed seems kinda ridiculous to me.

 public float Distance 
 {
     get
     {
         return m_Distance;
     }
     set
     {
         if(m_Distance != value)
         {
             m_Distance = value;
             BoxCollider2D b = GetComponent<BoxCollider2D>();
             b.size = new Vector2(m_Distance, b.size.y);
             b.center = new Vector2(m_Distance / -2, b.center.y);
         }
     }
 }
 public float m_Distance;

Thanks,

--David

Comment
Add comment · Show 1
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
avatar image potti · Oct 16, 2018 at 08:38 PM 0
Share

I am dealing with a similar issue right now. I looked into some unity internals and it seems that predefined components, like the sprite renderer actually have properties (not fields) that can be animated, but i do not see a way to allow custom properties to be animated.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I find the name and value for each variable of a script component, using Unity script? 2 Answers

Can I make animations snap to a frame? 2 Answers

Create a linear animation curve with javascript 1 Answer

Extended Jump Animation Mecanim 0 Answers

How to Move the rotated Game Object Forward 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges