Unity

Unity Listからforeachを使ってremoveする

foreach(Class item in Lists){
 if(item.id == id){ 
  Lists.remove(item);
 }
}

こんなかんじでやると、

InvalidOperationException: Collection was modified; enumeration operation may not execute.

とエラーになるので、

List<Class> TmpList = Lists;
foreach(Class item in Lists){
 TmpList.Add(item );
}

foreach(Class item in TmpList){
 if(item.id == id){ 
  Lists.remove(item);
 }
}

こうしてやる

Unity StreamingAssetsからファイルを読み出す

Streaming Assets で iOSとAndroidの違いにハマったのでメモ。

string FilePath;
#if UNITY_IPHONE
  FilePath = Application.dataPath + "/Raw/" + filename;
#else
  string fullPath = "jar:file://" + Application.dataPath + "/!/assets/" + filename;
  WWW www = new WWW (fullPath);
    while (!www.isDone) {
  }
  FilePath = Application.persistentDataPath + filename;
  File.WriteAllBytes (toPath,www.bytes);
#endif

iOSはそのままパスからファイル読み込みできるけど、Androidは一度Application.persistentDataPathにコピーしてから開かないといけない。

Unity の lookAt を おきかえる

UnityのlookAtは便利ですが、回転をさらに細かく制御しようと思うと難しいので置き換えます。

Rot.transform.LookAt (Vector3.zero);

こうするところを、

Vector3 relative = gameObject.transform.InverseTransformPoint(Vector3.zero);<br />
float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;<br />
Rot.transform.localRotation = Quaternion.Euler(0,angle - 90.0f, 0);

こうしてあげると、Vector3.zeroをむきつつ90.0度Y軸回転をできます

Structure Sensor が届きました 、VR/ARもモバイルが本命という話

<p style=" margin:8px 0 0 0; padding:0 4px;">
  [Structure sensor きた!全く新しいVR/ARのプロダクト作る! #vr #ar #structuresensor](https://www.instagram.com/p/BNVcFAXAFsK/" style=" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;)
</p>

<p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;">
  正裕 黒川さん(@masahiro8.backham)が投稿した写真 &#8211; <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2016-11-28T01:13:46+00:00">2016 11月 27 5:13午後 PST</time>
</p>
<p style=" margin:8px 0 0 0; padding:0 4px;">
  [iPhone6のベゼルを3Dプリントしてみた。ラフトが少し沿ってしまって失敗したけどとりあえずくっついた。#3dprint #structuresensor #iphone6 #ar #vr](https://www.instagram.com/p/BNWdDndgvFA/" style=" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;)
</p>

<p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;">
  正裕 黒川さん(@masahiro8.backham)が投稿した写真 &#8211; <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2016-11-28T10:41:34+00:00">2016 11月 28 2:41午前 PST</time>
</p>

Structure Senseor という iPhone / iPad を拡張する深度センサーをゲットしました。

VRのSDKを使わずにUnityでVRの作り方

カードボードなどのVRのSDKを入れなくてもUnityでVRを実装する方法を教えてもらったらめちゃくちゃ簡単でした。

2眼カメラを作る

まずは2眼のカメラの作り方から。仕組みは簡単で要は右目と左目のカメラを用意して表示範囲を左右半分づつにするだけです。