iTweenでSystem.Actionを使う方法

iTweenはとても便利なAssetですが、onupdateoncompletedelegateが使えないという点が少し不便だったので調べてみたところ簡単に変更できるようです。

//throw an error if a string wasn't passed for callback:
if (tweenArguments[callbackType].GetType() == typeof(System.String)) {
  target.SendMessage((string)tweenArguments[callbackType],(object)tweenArguments[callbackType+"params"],SendMessageOptions.DontRequireReceiver);
}else{
  Debug.LogError("iTween Error: Callback method references must be passed as a String!");
  Destroy (this);
}

という部分を

//throw an error if a string wasn't passed for callback:
if (tweenArguments[callbackType] is Action<object>) {
  ((Action<object>)tweenArguments[callbackType]).Invoke((object)tweenArguments[callbackType + "params"]);
}else{
  Debug.LogError("iTween Error: Callback method references must be passed as a String!");
  Destroy (this);
}

と書き換えます。

すると、次のような感じでonupdateが使えるようになります。

Hashtable ht = new Hashtable ();
ht.Add ("from", 0.0f);
ht.Add ("to", 1.0f);
ht.Add ("onupdate", (System.Action<object>)(v => {
  Debug.Log("v = " + (float)v);
}));
iTween.MoveTo(gameObject, ht);

便利です。

リンク

Specifying a delegate for the value of onupdate in iTween. – Unity Answers
https://answers.unity.com/questions/490719/specifying-a-delegate-for-the-value-of-onupdate-in.html

関連記事

iTween
Unityを使っている人であれば、1度は聞いたことがあるかもしれない有名なアセット「iTween」です。 個人的によく使っているのはValueTo。 Hashtable ht = new Hashtable(); ht.Add("from", 0.0f); ht.Add("to", 1.0f); ht.Add("time", 1.0f); ht.Add("onupdate","myU...

UnityでScreenshotMovieを作成するスクリプト
Unityで作ったゲームのScreenshotMovieを作成できるスクリプト ScreenshotMovie - Unify Community Wikihttp://wiki.unity3d.com/index.php/ScreenShotMovie 作成されるファイルはPNG形式の連番画像ファイルになるので、FFmpegや他の動画編集ソフト等で動画に変換してください。 F...

Unityの街並みジェネレーターアセット
適当な街並みを自動生成してくれるUnityのアセット「TekitouCityGenerator」。 テスト・プロトタイプでの利用を想定して作られたアセットだと思いますが、本番用途で使えそうなクオリティだと思います。 比較的軽めなのでスマホゲーに良いかもしれません。 リンク GitHub - Maruchu/TekitouCityGenerator: 適当な...

MeshをVoxel化してくれるUnityのアセット
MeshをVoxel化してくれるUnityのアセット「unity-voxel」を試してみました。 マイクラ風というかレゴ風というか分かりませんが、良い感じの雰囲気になっている気がします。 リンク GitHub - mattatz/unity-voxel: Mesh voxelization for Unity.https://github.com/mattatz/uni...

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です