(この記事の使用環境: Unity5.5.0f3 Personal、Windows10


無料アセット「iTween」の動作確認用に、関数を自動で連続して動かすようにしてみました。
前回記事 では 関数は「MoveTo」固定で、各種「EaseType」を連続して確認しましたが、今回は関数自体を変更します)

iTweenはこれ。



iTweenには多くの関数やパラメータが用意されており、それらを使ってオブジェクトに対して面白い動作を簡単に実装できるようになっています。

ドキュメントも整備されています。
iTween for Unity by Bob Berkebile (pixelplacement)


関数もたくさんあるので連続して動かせると確認用に便利かなと。
SnapCrab_NoName_2017-3-20_22-22-1_No-00

動画はこんな感じ。




考え方はこんな感じ。
  • iTweenの関数名で主なものを列挙しておく。それらをすべて取得し、foreachで順番に動かす
  • 動かしている関数名および設定しているEaseType、LoopTypeが分かるようテキストUIに情報を表示する
  • EaseTypeとLoopTypeはインスペクターから指定可能
  • 列挙した関数名もインスペクターから指定可能で、それは個別動作確認時に使用する
  • 実行時に左クリックで個別確認、右クリックで連続確認
 SnapCrab_NoName_2017-3-20_22-34-36_No-00

スクリプトはこんな感じ。
using System;                   // 追記(Enum.GetValues関数を使用するため)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// iTweenの動作確認用その2
///  ・関数、EaseType、LoopTypeの設定が可能
///  ・左クリックで個別確認
///  ・右クリックでeFunctionに列挙した関数を連続して確認
/// </summary>
public class iTweenController_02 : MonoBehaviour
{
    public GameObject targetPrefeb;     // ターゲットオブジェクトのPrefab
    GameObject target;                  // ターゲットオブジェクト

    public Transform startPos;          // スタート位置
    public Transform targetPos;         // ターゲット位置(移動先)
    public Transform lookTargetPos;     // 見る対象の位置

    // 動作確認する iTween の関数名
    public enum eFunction
    {
        PunchPosition,
        PunchRotation,
        PunchScale,
        ShakePosition,
        ShakeRotation,
        ShakeScale,
        FadeTo,
        CameraFadeTo,
        ColorTo,
        MoveTo,
        RotateTo,
        ScaleTo,
        LookTo,
    }
    public eFunction function = eFunction.PunchPosition;

    public iTween.EaseType easetype = iTween.EaseType.linear;   // iTweenのEaseType
    public iTween.LoopType looptype = iTween.LoopType.none;     // iTweenのLoopType

    // 各関数で使う設定値
    float time = 1.2f;
    float offsetTime = 0.3f;
    float moveAmount = 3f;
    float rotateAmount = 180f;
    float scaleAmount = 5f;
    float alphaAmount = 0f;

    // 各種情報を表示するテキストUI
    public Text infoFunctionText;
    public Text infoEaseTypeText;
    public Text infoLoopTypeText;


    // --- 初期化 ---------------------------------------
    private void Start ()
    {
        iTween.CameraFadeAdd ();    // iTween.CameraFadeを使う前に必要
    }


    // --- 更新処理 ---------------------------------------
    private void Update ()
    {
        // 左クリックで個別確認
        if (Input.GetMouseButtonDown (0))
        {
            StartCoroutine (OneExeFunction (function));
        }

        // 右クリックで連続確認
        if (Input.GetMouseButtonDown (1))
        {
            StartCoroutine (AllExeFunction ());
        }
    }


    // --- 個別確認用のコルーチン --------------------------------------------
    IEnumerator OneExeFunction (eFunction function)
    {
        // ターゲットオブジェクトの生成
        if (target == null)
            target = Instantiate (targetPrefeb, startPos.position, startPos.rotation);

        // 各種情報テキストUIの更新
        infoFunctionText.text = function.ToString ();
        infoEaseTypeText.text = "easetype: " + easetype.ToString ();
        infoLoopTypeText.text = "looptype: " + looptype.ToString ();

        // 関数の呼び出し
        SendMessage ("Exe" + function.ToString ());
        yield return new WaitForSeconds (time + offsetTime);    // 実行待ち時間

        // LoopTypeが設定されている場合は、実行待ち時間を増やす
        if (looptype != iTween.LoopType.none) yield return new WaitForSeconds (10f);

        // 動作確認後の片付け処理
        Destroy (target.gameObject);
        infoFunctionText.text = "";
        infoEaseTypeText.text = "";
        infoLoopTypeText.text = "";
        lookTargetPos.gameObject.SetActive (false);
        iTween.CameraFadeTo (iTween.Hash ("amount", 0f, "time", 0.01f));
    }


    // --- 連続確認用のコルーチン ------------------------------------------------
    IEnumerator AllExeFunction ()
    {
        // Enumの値をすべて取得し、iTweenの関数を実行する
        foreach (eFunction function in Enum.GetValues (typeof (eFunction)))
        {
            // 不正動作回避のための待ち時間
            yield return new WaitForSeconds (0.01f);

            // ターゲットオブジェクトの生成
            if (target == null)
                target = Instantiate (targetPrefeb, startPos.position, startPos.rotation);

            // 各種情報テキストUIの更新
            infoFunctionText.text = function.ToString ();
            infoEaseTypeText.text = "easetype: " + easetype.ToString ();
            infoLoopTypeText.text = "looptype: " + looptype.ToString ();

            // 関数の呼び出し
            SendMessage ("Exe" + function.ToString ());
            yield return new WaitForSeconds (time + offsetTime);    // 実行待ち時間

            // LoopTypeが設定されている場合は、実行待ち時間を増やす
            if (looptype != iTween.LoopType.none) yield return new WaitForSeconds (time + offsetTime);

            // 動作確認後の片付け処理
            Destroy (target.gameObject);
            infoFunctionText.text = "";
            infoEaseTypeText.text = "";
            infoLoopTypeText.text = "";
            lookTargetPos.gameObject.SetActive (false);
            iTween.CameraFadeTo (iTween.Hash ("amount", 0f, "time", 0.01f));
        }
    }


    // --- 以下、各関数の呼び出し用("Exe" + iTweenの関数名) -------------------------

    void ExePunchPosition ()
    {
        iTween.PunchPosition (target, iTween.Hash (
            "y", moveAmount,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExePunchRotation ()
    {
        iTween.PunchRotation (target, iTween.Hash (
            "y", rotateAmount,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExePunchScale ()
    {
        iTween.PunchScale (target, iTween.Hash (
            "x", scaleAmount,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeShakePosition ()
    {
        iTween.ShakePosition (target, iTween.Hash (
            "y", moveAmount / 3,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeShakeRotation ()
    {
        iTween.ShakeRotation (target, iTween.Hash (
            "y", rotateAmount / 3,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeShakeScale ()
    {
        iTween.ShakeScale (target, iTween.Hash (
            "x", scaleAmount / 3,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeFadeTo ()
    {
        iTween.FadeTo (target, iTween.Hash (
            "alpha", alphaAmount,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeCameraFadeTo ()
    {
        iTween.CameraFadeTo (iTween.Hash (
            "amount", 0.4f,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeColorTo ()
    {
        iTween.ColorTo (target, iTween.Hash (
            "color", Color.red,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeMoveTo ()
    {
        iTween.MoveTo (target, iTween.Hash (
            "position", targetPos.position,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeRotateTo ()
    {
        iTween.RotateTo (target, iTween.Hash (
            "y", rotateAmount,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeScaleTo ()
    {
        iTween.ScaleTo (target, iTween.Hash (
            "x", scaleAmount,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }

    void ExeLookTo ()
    {
        lookTargetPos.gameObject.SetActive (true);
        iTween.LookTo (target, iTween.Hash (
            "looktarget", lookTargetPos.position,
            "time", time,
            "easetype", easetype,
            "looptype", looptype));
    }
}


今回は以上です。
(管理情報: AssetTest  > iTween_Test > Main