- Home /
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
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.
Your answer
Follow this Question
Related Questions
Unity4.5 AnimationCurve - Is the property "_MainTex.rotation" valid ? 0 Answers
Animation property for box collider doesn't work until I press the small record button twice 0 Answers
code animation on multiple properties using one AnimationClip 1 Answer
Adding different animations to the Unity Locomotion System 0 Answers
How to edit animation curves for a humanoid animation? 0 Answers