1
2

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.

Animationのタイムラインからメソッドを呼ぶ一連の流れ

Last updated at Posted at 2015-03-26

●仮にTestControllerというAnimator Controllerを作成し、IdleというStateを作成してデフォルト状態にする。
スクリーンショット 2015-03-26 13.14.23.png

●仮にTestAnimeというAnimationを作成し、下記のように始点と終点に移動するアニメーションキーを打つ
スクリーンショット 2015-03-26 13.15.30.png
スクリーンショット 2015-03-26 13.15.34.png

TestControllerを選択し、TestAnimeを**[Animator View]にドラッグアンドドロップする。
[Animator View]上でIdleからTestAnime**に対して条件なしのTransitionのを設定
スクリーンショット 2015-03-26 13.28.23.png

TestControllerを選択し、[Inspector]上ににドラッグアンドドロップする。
●TestAnimationMethod.cs
というクラスを作成する。
 ※ソースコードは最下段にあり
TestAnimationMethod.csを選択し、**[Inspector]**上ににドラッグアンドドロップする。
スクリーンショット 2015-03-26 13.35.48.png

TestAnimeを**[Animation View]**で開き、時間の少し下でマウスの右クリックを押すと
Add Animation Eventが表示されるのでクリックする。

Animation1.png

●呼び出し先(デリゲート先)のメソッドをTestAnimationMethod.csのメソッドから選択する。
 ※publicになっていないメソッドは選択できないので注意
Animation2.png

TestAnimationMethod.cs
using UnityEngine;
using System.Collections;

public class TestAnimationMethod : MonoBehaviour {

    private GameObject ParticleObject;

    private Animator animator;
    private int _plusVal = 0;
    private float _blendTreeVal = 0.0f;

    public void EndAnimationMethod()
    {
        // アニメーション状態を取得
        AnimatorStateInfo state = GetComponent<Animator>().GetCurrentAnimatorStateInfo(0);
        // パーティクルを取得
        ParticleSystem particle = ParticleObject.GetComponent<ParticleSystem>();

        Debug.Log("アニメーションから呼ばれました");

        // Random クラスの新しいインスタンスを生成する
        

        // 0.0 以上 1.0 以下の乱数を取得する
        float redcolor = Random.Range(0.0f, 1.0f);
        float greencolor = Random.Range(0.0f, 1.0f);
        float bluecolor = Random.Range(0.0f, 1.0f);

        particle.startColor = new Color(redcolor, greencolor, bluecolor);

        // パーティクルをスタート
        particle.Play();

        // ステータス状態はIdleか?
        if(state.IsName("Idle") == true)
        {
            Debug.Log("Idle");
        }
        // ステータス状態はTestAnimeか?
        else if (state.IsName("TestAnime") == true)
        {
            Debug.Log("TestAnime");
        }
        // ステータス状態はSetValueか?
        else if (state.IsName("SetValue") == true)
        {
            Debug.Log("SetValue");
        }
        else
        {
            Debug.Log("Not Exist");
        }


        // tag1か?
        if (state.IsTag("tag1") == true)
        {
            Debug.Log("tag1");
        }
        // tag2か?
        else if (state.IsTag("tag2") == true)
        {
            Debug.Log("tag2");
        }
        // tag3か?
        else if (state.IsTag("tag3") == true)
        {
            Debug.Log("tag3");
        }
        else
        {
            Debug.Log("tag Not Exist");
        }
    }

    // Use this for initialization
    void Start()
    {
        animator = GetComponent<Animator>();
        // アニメーションをTrueにする。
        //animator.enabled = true;
    }

    // Update is called once per frame
    void Update()
    {
        // 上矢印が押されている間だけテストアニメが実行される。
        if (Input.GetKey(KeyCode.UpArrow))
        {
            animator.SetBool("GoTestAnime", true);
            _plusVal++;
            animator.SetInteger("SetText", _plusVal);
        }
        // 下矢印が押されている間だけBlendTreeが実行される。
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            _blendTreeVal = _blendTreeVal + 0.05f;
            animator.SetFloat("BlendVal1", _blendTreeVal);
        }
        // 上矢印が押されていない間は動作なし
        else
        {
            if (_plusVal > 1000)
            {
                animator.SetBool("GoTestAnime", false);
                _plusVal = 0;
            }
        }
    }
}
1
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?