言語はC#で使ってみた。
iTween公式サイト: iTween
iTween:
- iTween本体。無料、商用可。
- 入手方法
UnityのAssetStoreから入手。
※後述 iTween Visual Editor にはiTween本体は同梱されている。
iTween Visual Editor を使う場合は、iTween本体は別途入れる必要なし。
両方入れるとエラーになるので注意。
iTween Visual Path Editor:
- iTween用の移動パス作成を支援するUnityのアドオン。
- 入手方法
Asset Store又はサイトから入手可。
- Asset Storeから入手:
Unity上から CTRL+9でAssets Storeを開きiTween Visual Editor で検索。
- 公式サイトから入手: VISUAL ITWEEN PATH EDITOR
UnityのAssetにドラッグ&ドロップでインストール。
Asset Storeから入れるのが楽。iTween本体も同梱されているので iTween本体パッケージは不要(両方入れるとエラーになる)。
- Asset Storeから入手:
- UnityのIDE画面上ではコンポーネントの一種として利用するような形になっている。
- 公式ページにある動画がわかりやすい(ただし英語) → Visual editor for iTween motion paths
iTween Visual Path Editorがスゴイ。操作がシンプルなのに良く出来てる。
足りないなぁと思う機能は別途コードで実装できるようなしくみがiTweenのクラスで用意されている。
以下、iTweenメモ。
■iTween Visual Path Editorで作成したパスを使う
1 2 | iTween.MoveTo(gameObject, iTween.Hash( "path" , iTweenPath.GetPath( "MyPath" ) , "time" , 5); |
"MyPath"はUnity上のiTween Visual Path Editorで作成したパス。
"time"で次のパラメータ属性が時間であることを指定。5 は秒数。この場合は5秒間で先のパスを実行する。
■iTween Visual Path Editorで作成したパスを逆順に使う
往復動作をさせたい場合PingPong等の指定もあるが、これは往復動作をLOOPで実行するので片道には使いにくい。
なのでiTween Visual Path Editorで作成した片道分のパスを逆順に取得する GetPathReversed()を使う。
1 2 3 | iTween.MoveTo(gameObject, iTween.Hash( "path" , iTweenPath.GetPathReversed( "MyPath" ) , "time" , 5); |
■データ構造について
追記:ソースは iTweenPath.cs。各変数はpublicなのでパスデータへもアクセスできる。
- パスは
Array配列のようなイメージでシンプルにVector3をList型でXYZ座標値データを格納している。
- iTween Visual Path EditorはXYZ座標値データ列をパス名で紐付けている。
- iTween クラスの iTweenPath.GetPathでパス名でXYZ座標値データを引っ張ってくる。
- iTween.MoveTo()等の引数は
ハッシュ名, パラメータ
という規則性があることが伺える。※よくある可変数パラメータの引渡し方
■iTween開始、終了をコールバックさせる
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 | using UnityEngine; using System.Collections; public class cs_iTWTest : MonoBehaviour { const float Time_Default = 0.5f; Hashtable table = new Hashtable(); void Start () { table.Add( "path" , iTweenPath.GetPath( "MyPath" ) ); //ITween path hash data table.Add( "time" , Time_Default ); table.Add( "easetype" , iTween.EaseType.easeInOutSine ); table.Add( "onstart" , "cb_iTweenStart" ); //Handler func when iTween start table.Add( "onstartparams" , "Start" ); //parameter of Handler func when iTween start table.Add( "oncomplete" , "cb_iTweenComplete" ); //Handler func when iTween end table.Add( "oncompleteparams" , "Complete" ); //parameter of Handler func when iTween end iTween.MoveTo(gameObject, table); } void cb_iTweenStart( string param){ Debug.Log( "[iTween] cb_iTweenStart: " +param); } void cb_iTweenComplete( string param){ Debug.Log( "[iTween] cb_iTweenComplete: " +param); } void cb_iTweenUpdate( float value){ camera.orthographicSize = value; } } |
引渡しパラメータ用に Hashtable 型のtable を使用。そこに各種いろいろ詰め込んで最後にiTween.MoveTo()へ渡して処理。
そのパラメータの中に、
- onstart: 開始時コールバック関数指定
- onstartparams : 開始時コールバック関数へのパラメータ指定
- oncomplete: 終了時コールバック関数指定
- oncompleteparams: 終了時コールバック関数へのパラメータ指定
があるので、それを使う。
■回転拡大、カメラなど
回転、拡大を行うiTween関数は一応ある。それらの指定方法は、
- 現在~最終的な回転角度を指定秒数で実行する
移動もこれと同じ概念で、
- 現在~最終的な位置へ指定秒数で実行する
となる。なので2つを同時に実行開始すればおおむねは期待する結果になる。※1つの関数で両方同時にはできない様子。
しかしラグ(?)が絡むとずれそうな気配もある。また、Unityのカメラを、
1 2 | camera.orthographic = true ; camera.orthographicSize = 10f; |
として camera.orthographicSize を動的に変化させてカメラビュー内の拡大率を変える場合、これに相当する引数がパッと見用意されていないように思える(ひょっとしたらあるのかもしれない?)。
そういう際に以下のような感じで実現できる。
呼び出し側
1 2 | iTween.MoveTo(gameObject, table); iTween.ValueTo(gameObject, iTween.Hash( "from" , 12.3f, "to" , 5.0f, "time" , 1, "onupdate" , "cb_iTweenUpdate" )); |
コールバック関数
1 2 3 | void cb_iTweenUpdate( float value){ camera.orthographicSize = value; } |
上記コードはカメラの拡大率を1秒間で12.3~5.0に変動させる。コールバック関数は1フレーム毎に呼び出される。
3Dでも簡単にiTweenできるのはかなり楽ですね~。ちょっと感動。
0 件のコメント:
コメントを投稿