Debug出力する際に、定例分の中に変数を埋め込むString.formatは便利なのですが、Debug.Logと組み合わせると、コードが長くなってしまいますが、直接、フォーマットできる方法があります。
Debug.LogFormatを使うと、整形フォーマットを指定できます。
string foo = "abc"; Debug.LogFormat("foo: {0}", foo); // foo: abc
引数の指定の仕方は、String.formatと同じ。
Debug出力する際に、定例分の中に変数を埋め込むString.formatは便利なのですが、Debug.Logと組み合わせると、コードが長くなってしまいますが、直接、フォーマットできる方法があります。
Debug.LogFormatを使うと、整形フォーマットを指定できます。
string foo = "abc"; Debug.LogFormat("foo: {0}", foo); // foo: abc
引数の指定の仕方は、String.formatと同じ。
int[,] twoDim = new int[8,16];
要素8個の中に、要素16個を持つ2次元配列をつくる。
int[2,4] = 128;
DoTweenでオーディオのフェードイン/アウトを簡単に実装
冒頭で、
using DG.Tweening;
ボリューム0まで、1秒でフェードアウトする場合
AudioSource audioSource = this.gameObject.GetComponent<AudioSource>(); float vol = 0.0f; float duration = 1.0f; audioSource.DOFade( vol, duration );
AudioSourceコンポーネントを持ったGameObjectなどに、このコードを記述したスクリプトを張りつける。
UnityのVersion 5.6.1 f1で発生するのだが、macOSをHigh Sierraにすると、Assetsの中身が空になってしまう症状がある。
原因は、High Sierraで採用されているファイルシステムAPFSにUnityの古いバージョンが対応していないのが原因。プロジェクトフォルダを、これまでのAFPSの外付けHDDにコピーして読み込めば、
開くことが可能。一安心。
ちなみに、このバグのパッチが当たったバージョンもリリースされている。
イベントデータからボタンの種別を取得できる
例えば、
public void OnPointerClick(PointerEventData pointerEventData) { switch(pointerEventData.pointerId) { case -1: Debug.Log("Left Click"); break; case -2: Debug.Log("Right Click"); break; case -3: Debug.Log("Middle Click"); break; } }
https://docs.unity3d.com/ja/current/ScriptReference/EventSystems.PointerEventData.html
Prefab化したUIパーツをCanvasに、スクリプトでアタッチしようとしたらWarningが出た。
Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues.
UnityEngine.Transform:set_parent(Transform)
GameObject o = Instantiate( Foo ); o.transform.parent = this.transform;
こういう様に親GameObjectを指定するのは良くはなく、以下が正しい。
GameObject o = Instantiate( Foo ); o.transform.SetParent(this.transform);
Unityで機種情報を取得する方法
Debug.Log("deviceModel: " + SystemInfo.deviceModel);
iPad7,1 みたいな文字列を取得できる。
Via! http://hiyotama.hatenablog.com/entry/2016/12/16/153620
主なデバイス判別文字列
https://qiita.com/YumaInaura/items/31838a72678fa09d7e19
ofMapのように、任意の数値を定められた数値範囲変換(スケーリング、数学的には線形写像というらしい)をC#でできるようにする。
独自のユーティリティクラスを作る方法もあるが、既存のfloat型を拡張する形での実装方法。
以下のコードを、Map.csなどとして、スクリプトを置くフォルダ(ex. /Scripts)に保存する
using System.Collections; using System.Collections.Generic; using UnityEngine; public static class ExtensionMethods { public static float Map (this float value, float fromSource, float toSource, float fromTarget, float toTarget) { return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget; } }
使用方法は以下の通り
float foo = 0.5f; float result = foo.Map( 0.0f, 1.0f, 0, 100 ); //50
スクリプトからSkyBoxの色を変える方法
RenderSettings.skybox.SetColor("_BgColor", Color );
RenderSettings.skybox
でMaterialまでアクセスできる。