Programming」カテゴリーアーカイブ

プログラミング全般に関するカテゴリーです。

Gentoo LinuxでC#を使ってみた

Gentoo LinuxのMonoでC#を使ってみたので、使い方をメモしてみます。

インストール

# emerge -av mono

プログラミング

$ vi hello.cs
$ cat hello.cs
using System;
public class Hello
{
  static public void Main ()
  {
    Console.WriteLine("Hello world!");
  }
}

コンパイル&実行

$ mcs hello.cs
$ ls
hello.cs  hello.exe*
$ mono hello.exe
Hello world!

リンク

Home | Mono
http://www.mono-project.com/

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