LoginSignup
0
2

More than 5 years have passed since last update.

Unityでintのプロパティをアニメーションさせたいときの対策

Posted at

UnityのAnimationで数値を変化させたいとき、変数がfloatならいつもの通り数値を変更するだけでアニメーションに記憶させることが出来るんだけど、変数がintだった場合、Animationにキーフレームを生成することが出来ない。
なんでだよ。

しょうがないのでfloatの変数を持ったコンポーネントを自作してintにキャストして目的のプロパティに入れる。

以下コード。
(Anima2DのSprite Mesh Instance の表示順を変更したかったとき。
適宜置き換えて見て下さい。)


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Anima2D;

[RequireComponent(typeof(SpriteMeshInstance))]
[ExecuteInEditMode]
public class SpriteMeshInstanceHelper : MonoBehaviour {

    private SpriteMeshInstance spriteMeshInstance;
    public float orderInLayer;

    // Use this for initialization
    void Start () {

        spriteMeshInstance = GetComponent<SpriteMeshInstance>();
    }

    // Update is called once per frame
    void Update () {

        spriteMeshInstance.sortingOrder = (int)orderInLayer;
    }
}

ちなみに[ExecuteInEditMode]をつけることにより、シーンビューでアニメーションを変更中にも見た目の変更が反映されます!
やったね!

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