FC2ブログ
    11 «1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.» 01

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    【Unity】オブジェクトをふにゃふにゃ動かすのに、iTweenPath使ったんだぜぇ~ ワイルドだr(自粛 

    おはようございます。
    本日は昼まで祝日とは知りませんでした。テヘ
    曜日感覚とかやばくなってきました。坂内です。
    みなさんはハロウィンはっちゃけました?
    俺は家でゲームしてました。
    誰だよハロウィンとか広めたの!ちょっと前まで、コンビニとかでちょっとお菓子がハロウィン用になるくらいだったのに、最近わけーのもいい年のもはっちゃけすぎだろ!落ち着けお前ら!
    とまぁ 苦言を一つ。

    本日はiTweenPathを初めて使ったので、そのことについてちょちょっと書いてみます。

    まずはこちらで「iTween Visual Editor」をインストールします。

    もちろんタダ。タダ、これ最強也。

    この時すでにiTweenを単体で入れていた場合、「iTweenクラスかぶってんぞ おぃ!」って怒られるので気を付けてください。

    ここにiTweenクラスは入ってます。(ちなみに俺はここのiTweenを残しました)

    あとはもう簡単!
    適当なオブジェクトを作って、それにiTweenPathをつけます。

    実際の使い方は以下の通り。

    iTweenPathのNodeCountがPathになる座標です。
    こちら10個まで設定できるようです。
    座標を好きに移動させて、こんな動きのパスを作ってみました。

    あとは、コードで動かす感じですね。

    TestITweenPathっていうクラス作って、iTweenPathをつけたオブジェクトにつけました。
    using UnityEngine;
    using System.Collections;

    public class TestITweenPath : MonoBehaviour {
    public GameObject sphere;
    // Use this for initialization
    void Start () {
    Hashtable hash = new Hashtable();
    hash.Add("path",iTweenPath.GetPath("TestPath"));
    hash.Add("time",5f);
    hash.Add("easetype",iTween.EaseType.linear);
    iTween.MoveTo(sphere,hash);
    }
    }

    で、玉(Sphere)を1個作って、こちらのsphereにアサインしときます。
    あ、Pathの名前は「TestPath」にしました。

    これ、ちゃんとパス通りに動きますよ!
    すごいね!
    やったね!

    しかしだ、1点問題がありまして。
    複数回同じPathを取得するとエラーが出ます。
    普通に複数回上記のコードで流す分にはいいのですが、InstantiateしたオブジェクトにてiTweenPathを行った際の問題(?)です。


    先ほどのiTweenPathをつけたオブジェクトと、玉を1つのオブジェクトに突っ込んで、Prefab化します。
    「TestITweenPath」クラスを以下のように修正しておきます。
    これで、ボールの動きが終わったら丸々Destroyするようにします。
    using UnityEngine;
    using System.Collections;

    public class TestITweenPath : MonoBehaviour {
    public GameObject sphere;

    void Start () {
    Hashtable hash = new Hashtable();
    hash.Add("path",iTweenPath.GetPath("TestPath"));
    hash.Add("time",5f);
    hash.Add("easetype",iTween.EaseType.linear);
    hash.Add("oncompletetarget",gameObject);
    hash.Add("oncomplete","EndPath");
    iTween.MoveTo(sphere,hash);
    }

    public void EndPath(){
    Destroy(gameObject);
    }
    }


    で、ボタンを用意し、ボタンを押すことで上記オブジェクトが出てくるようにします。

    これで実行をすると、ボタンを押すと玉が出てきて、Path通りに動き、最後には消えます。
    そこで、もう一度ボタンを押したところ、以下のようなエラーが発生しました。
    ArgumentException: An element with the same key already exists in the dictionary.
    System.Collections.Generic.Dictionary`2[System.String,iTweenPath].Add (System.String key, .iTweenPath value)
    (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/
    Dictionary.cs:404)
    iTweenPath.OnEnable () (at Assets/iTweenEditor/iTweenPath.cs:17)
    UnityEngine.Object:Instantiate(Object)
    PathTest2:StartPath() (at Assets/Scripts/PathTest2.cs:8)
    UnityEngine.EventSystems.EventSystem:Update()

    なんかDictionaryに同じキーが入ってるよーってエラーぽいですね。
    まったく同じPathを取得するときにエラーがでてるのかな?
    ってことで、次のようにすると動くようになりました。

    using UnityEngine;
    using System.Collections;

    public class TestITweenPath : MonoBehaviour {
    public GameObject sphere;

    void Start () {
    Hashtable hash = new Hashtable();
    hash.Add("path",iTweenPath.GetPath("TestPath"));
    hash.Add("time",5f);
    hash.Add("easetype",iTween.EaseType.linear);
    hash.Add("oncompletetarget",gameObject);
    hash.Add("oncomplete","EndPath");
    iTween.MoveTo(sphere,hash);
    }

    public void EndPath(){
    iTweenPath.paths.Clear();
    Destroy(gameObject);
    }
    }


    これ追加しました ⇒ iTweenPath.paths.Clear();
    iTweenPath.pathsをクリアーしてやりました。

    ってお話でしたと。

    では アデュ~ノシ

    Category: 開発日記(Unity)

    Tag: Unity  iTween 
    tb 0 : cm 0   

    コメント

    コメントの投稿

    Secret

    トラックバック

    トラックバックURL
    →http://halcyonsystemblog.jp/tb.php/150-aeee2edf
    この記事にトラックバックする(FC2ブログユーザー)