LoginSignup
2
2

More than 5 years have passed since last update.

UnityのAnimationのコールバックについてメモ((φ(・д・。)

Posted at

アニメーションのコールバックを設定(参考にしたページ)しようと思ったら少しはまったのでメモ。

親クラスを定義してみたらはまった

アニメーションのコールバックは、MonoBehaviourの子クラスでありアタッチされていることが条件で、
親クラスがMonoBehaviourの抽象クラスを親とする場合、そのクラスの関数を参照することができない。

Parent.cs
public abstract class Parent : MonoBehaviour{
    public abstract void In( int id );
}
Child.cs
public class Child : Parent{
    // Animatorから参照されない
    public override void In( int id ) {
    }
}

上のコードは参照できない例。

抽象クラスで色々定義してから子のコールバッククラスを作成しようと思っていたので、ものすごく残念。

解決例

interface を定義して、実装をする方法で妥協。

IAnim.cs
public interface Parent{
    void In( int id );
}
Child.cs
public class Child : MonoBehaviour, Parent {
    // 参照された
    public override void In( int id ) {
    }
}

これで参照されました。

2
2
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
2
2