4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】Animationによるプロパティの変更を検知する

Last updated at Posted at 2019-10-04

void OnDidApplyAnimationProperties() が使えます。

// アニメーションのプレビューでも値をチェックしたいのでExecuteAlwaysする
[ExecuteAlways] 
public class Foo : MonoBehaviour
{
    public float foo; // TimelineとかAnimationでこの値を変更する

    void OnDidApplyAnimationProperties()
    {
        // ……すると、このメソッドが呼ばれる
        Debug.Log("foo changed!")
    }
}

どの値が変更されたのかはわからないので、古い値をとっておいて自分でチェックする必要があるっぽい。マジか……?

[SerializeField] private _foo;
float _lastFoo;

[SerializeField] private _bar;
float _lastBar;

void OnDidApplyAnimationProperties()
{
    if (_foo != _lastFoo)
    { 
        Debug.Log("foo changed!")
    }
    if (_bar != _lastBar)
    { 
        Debug.Log("bar changed!")
    }

    _lastFoo = _foo;
    _lastBar = _bar;
}

もっといい方法あれば教えてください。

参考

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?