2014年10月31日金曜日

[Unity]TouchScriptの使い方

ExamplesフォルダにあるSenseにサンプルがある

TapさせたいときはオブジェクトにAdd Component > Tap Gestrure(Script)を追加する
Colliderも追加する
using System;
using UnityEngine;
using TouchScript.Gestures;

public class TapManager : MonoBehaviour
{
    void OnEnable ()
    {
        GetComponent<tapgesture> ().Tapped += tappedHandler; 
    }
    
    void OnDisable ()
    {
        GetComponent<tapgesture> ().Tapped -= tappedHandler;
    }
    
    private void tappedHandler (object sender, EventArgs e)
    {
        Destroy (gameObject);
    }
}

2014年10月29日水曜日

[Unity]Resourcesにあるプレハブを読み込みインスタンスを生成する

Unity 4.5.5f1

プレハブの読み込みは公式にサンプルコードがある

Resources.Load
http://docs.unity3d.com/ScriptReference/Resources.Load.html

以下のコードはInstantiateの戻り値がObjectなのでGameObjectに変換できない、としてコンパイルエラーが発生する

//コンパイルエラー
GameObject instance = Instantiate (Resources.Load ("enemy"));
不格好だがこんなキャストでいける
//Load可能
Transform instance = (Transform)Instantiate ((Transform)Resources.Load ("enemy"));
これで動作はするが、どこかおかしい気がする

2014年10月28日火曜日

[Unity]PlayerPrefsで保存する

UnityではPlayerPrefsを使ってデータを保存できるが、longbool、配列には対応していない。

そこで各種の型に対応したPlayerPrefsXがある
ArrayPrefs2
直接cs,jsファイルはDLできないようなので、コピペで作る。

ただこれだとPlayerPrefsPlayerPrefsXを使い分けるようになるので、PlayerPrefsと同じメソッドを持つよう以下のように編集すると使いやすい
public static bool SetString (string key, string value)
{
    PlayerPrefs.SetString (key, "" + value);
    return true;
}

public static String GetString (string key)
{
    return (PlayerPrefs.GetString (key));
}

public static bool SetInt (string key, int value)
{
    PlayerPrefs.SetInt (key, value);
    return true;
}

public static int GetInt (string key)
{
    return (PlayerPrefs.GetInt (key));
}

public static bool SetFloat (string key, float value)
{
    PlayerPrefs.SetFloat (key, value);
    return true;
}

public static float GetFloat (string key)
{
    return (PlayerPrefs.GetFloat (key));
}

2014年10月27日月曜日

[Unity]連想配列を使う、配列にあるオブジェクトを削除する

//Unity 4.5.5
//リストから要素を取り出す
foreach (Transform child in transformList) {
    Deubg.log(child.localPosition);
}

//Destroyで削除するときは連想配列は使えない
//C#逆順で連想配列を取り出すことはできない、List.Revers()では配列が逆転するだけ
for (int i = transformList.Count - 1; 0 <= i; i--) {
    //Transformの時はDestory時に.gameObjectを取り出す
    Object.Destroy (transformList [i].gameObject);
}

2014年10月17日金曜日

[Unity]Input.Touchesの使い方

https://www.assetstore.unity3d.com/jp/#!/content/3283

・Input.Touchesはスマホのタッチイベントを最適化したスクリプト
・スワイプ、ピンチ、ドラッグ、回転、ダブルタップ、ロングタップ、ショートタップ、チャージなどを検出することができる
・タッチが検出できないPCでの動作ではマウスと同様の動きをする
・価格は25$

使い方
Unity 4.5.5
Input.Touhes v1.1.5

InputTouches > InputPrefb > GestrueのプレハブをHierachyに追加
Hierachyに適当なオブジェクトを追加して新規Scriptを追加
//C# サンプルコード

//オブジェクトが有効になった時に呼び出される
void OnEnable() {
    //onShortTapE、onDoubleTapEというイベントがあるが、
    //onMultiTapEに統合されていおり、
    //onShortTapE、onDoubleTapEはいずれ廃止される 

    //リスナーに登録

    //通常のタップとマルチタップを検出する
    IT_Gesture.onMultiTapE += OnMultiTap;
    //ロングタップ
    IT_Gesture.onLongTapE += OnLongTap;
    //チャージング(タップしたままホールド時間を随時検出)
    IT_Gesture.onChargingE += OnCharging;
    //チャージング終了(チャージ後に指が離れた)
    IT_Gesture.onChargeEndE += OnChargeEnd;
   
    //ドラッグ開始
    IT_Gesture.onDraggingStartE += OnDraggingStart;
    //ドラッグ中
    IT_Gesture.onDraggingE += OnDragging;
    //ドラッグ終了
    IT_Gesture.onDraggingEndE += OnDraggingEnd;
}

//オブジェクトが無効になった時に呼び出される
void OnDisable() {
    //リスナーの登録を解除
    IT_Gesture.onMultiTapE -= OnMultiTap;
    IT_Gesture.onLongTapE -= OnLongTap;
   
    IT_Gesture.onChargingE -= OnCharging;
    IT_Gesture.onChargeEndE -= OnChargeEnd;
   
    IT_Gesture.onDraggingStartE -= OnDraggingStart;
    IT_Gesture.onDraggingE -= OnDragging;
    IT_Gesture.onDraggingEndE -= OnDraggingEnd;
}

//以下のメソッドでタッチを検出する

void OnMultiTap(Tap tap){
    //do a raycast base on the position of the tap
    //対象のオブジェクト(shortTapObj)がタップされているか調べる、
    //1つ1つ調べるのは結構面倒くさい、タップだけならAssetStoreのTouchScriptが良いかも
    Ray ray = Camera.main.ScreenPointToRay(tap.pos);
    RaycastHit hit;
    if(Physics.Raycast(ray, out hit, Mathf.Infinity)){
        //if the tap lands on the shortTapObj, then shows the effect.
        if(hit.collider.transform==shortTapObj){
            //place the indicator at the object position and assign a random color to it
            Indicator.transform.position=shortTapObj.position;
            Indicator.startColor=GetRandomColor();
            //emit a set number of particle
            Indicator.Emit(30);
        }
        //if the tap lands on the doubleTapObj
        else if(hit.collider.transform==doubleTapObj){
            //check to make sure if the tap count matches
            if(tap.count==2){
                //place the indicator at the object position and assign a random color to it
                Indicator.transform.position=doubleTapObj.position;
                Indicator.startColor=GetRandomColor();
                //emit a set number of particle
                Indicator.Emit(30);
            }
        }
    }
}