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

UnityのUIでDrag and Drop

IBeginDragHandler, IDragHandler, IDropHandlerを継承するとOnBeginDrag, OnDrag, OnDropが使えようになるそうです。

public class Example : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler
{
  public void OnBeginDrag(PointerEventData eventData)
  {
    Debug.Log("OnBeginDrag");
  }

  public void OnDrag(PointerEventData eventData)
  {
    Debug.Log("OnDrag");
  }

  public void OnDrop(PointerEventData eventData)
  {
    Debug.Log("OnDrop");
  }
}

Drag & Dropに限らず、UnityでUIを使うときはEventSystemも必要になるので気を付けてください。

リンク

uGuiでDrag And Drop処理 – Qiita
https://qiita.com/divideby_zero/items/d8eebc44e151a60b2b81

unity4.6 beta / uGUI ドラッグ編 – petlust
http://petlust.hateblo.jp/entry/2014/08/24/190838

Time.timeScale=0でもiTweenを使う方法

UnityではTime.timeScale01未満の値に設定することで、ゲームの一時停止やスローモーション状態を簡単に作ることができます。ただ、ゲームは停止中でもiTweenを使ったUIアニメーションだけは動かしたいという場面もあると思います。そういう場合はignoretimescaleのプロパティーを使うと良いそうです。

使い方は次のような感じです。

Hashtable ht = new Hashtable ();
ht.Add ("from", 0.0f);
ht.Add ("to", 1.0f);
ht.Add ("ignoretimescale", true);
ht.Add ("onupdate", "myOnUpdateFunction");
iTween.MoveTo(gameObject, ht);

リンク

iTween Ignore Scale Time | Unity Community
https://forum.unity.com/threads/itween-ignore-scale-time.175926/