「毎日Unity」の技術ブログ

開発で役立つ情報を発信する

UnityC#

【UnityC#】文字列のn文字目を置換する方法

文字列のn文字目を置換する方法をメモ。 [ 方法 ] 文字列targetのn文字目をrに置換します。 using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScriptTest : MonoBehaviour { private void Start() { string ta…

【UnityC#】FPS視点スクリプト

「Player」という名前のC#スクリプトを作成して以下のスクリプトをコピペして保存します。そしてインスペクター内にある「Main Camera」にアタッチすれば、FPS視点のような動きができます。 [ スクリプト ] WASDで移動、Spaceで上昇、Ctrlで下降、Rで原点に…

【UnityC#】Array.IndexOf、List.IndexOfの速度比較

Array.IndexOf、List.IndexOfの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2021.2.19.f1 [ 比較結果 ] Array.IndexOf 12702 ms List.IndexOf 12864 ms [ スクリプト ] using System.Collections; using System.Collections.Generic; using Un…

【UnityC#】==、GameObject.CompareTagの速度比較

==、GameObject.CompareTagの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] == 340 ms GameObject.CompareTag 99 ms [ スクリプト ] using System.Collections; using System.Collections.Generic; using UnityEngine…

【UnityC#】string.Contains、string.StartsWith、string.IndexOfの速度比較

string.Contains、string.StartsWith、string.IndexOfの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] string.Contains 94 ms string.StartsWith 925 ms string.IndexOf 232 ms [ スクリプト ] using System.Collecti…

【UnityC#】スクリプトからVolumeを変更する方法

スクリプトからVolumeを変更する方法をメモ。 [ 変更方法 ] VolumeにあるBloomのIntensityを10.0fに変更するスクリプトです。 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; using UnityEn…

【UnityC#】配列のコピー方法まとめ

配列のコピー方法をまとめました。 [ 一覧 ] = 「=」を使って配列をコピーするとシャローコピーになります。 using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScriptTest : MonoBehaviour { private void Sta…

【UnityC#】Math.Pow(i, 3)、Mathf.Pow(i, 3)、i * i * iの速度比較

Math.Pow(i, 3)、Mathf.Pow(i, 3)、i * i * iの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2021.2.19.f1 [ 比較結果 ] Math.Pow(i, 3) 26 ms Mathf.Pow(i, 3) 55 ms i * i * i 0 ms PowInt(i, 3) 3 ms [ スクリプト ] using System.Collectio…

【UnityC#】Math.Cos、Mathf.Cosの速度比較

Math.Cos、Mathf.Cosの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] Math.Cos 20 ms Mathf.Cos 31 ms [ スクリプト ] using System.Collections; using System.Collections.Generic; using UnityEngine; using Syste…

【UnityC#】Math.Sin、Mathf.Sinの速度比較

Math.Sin、Mathf.Sinの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] Math.Sin 20 ms Mathf.Sin 30 ms [ スクリプト ] using System.Collections; using System.Collections.Generic; using UnityEngine; using Syste…

【UnityC#】Math.Clamp、Mathf.Clampの速度比較

Math.Clamp、Mathf.Clampの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] Math.Clamp 8 ms Mathf.Clamp 9 ms [ スクリプト ] using System.Collections; using System.Collections.Generic; using UnityEngine; using…

【UnityC#】Math.Sqrt、Mathf.Sqrtの速度比較

Math.Sqrt、Mathf.Sqrtの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] Math.Sqrt 4 ms Mathf.Sqrt 21 ms [ スクリプト ] using System.Collections; using System.Collections.Generic; using UnityEngine; using Sy…

【UnityC#】Math.Abs、Mathf.Absの速度比較

Math.Abs、Mathf.Absの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] Math.Abs 6 ms Mathf.Abs 18 ms [ スクリプト ] using System.Collections; using System.Collections.Generic; using UnityEngine; using System…

【UnityC#】Array.Contains、List.Contains、LinkedList.Contains、HashSet.Contains、Dictionary.ContainsKey、Dictionary.ContainsValueの速度比較

Array.Contains、List.Contains、LinkedList.Contains、HashSet.Contains、Dictionary.ContainsKey、Dictionary.ContainsValueの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] Array.Contains 13984 ms List.Contains…

【UnityC#】List.RemoveAt、LinkedList.RemoveFirst、HashSet.Remove、Dictionary.Removeの速度比較

List.RemoveAt、LinkedList.RemoveFirst、HashSet.Remove、Dictionary.Removeの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] List.RemoveAt 261 ms LinkedList.RemoveFirst 5 ms HashSet.Remove 2 ms Dictionary.Rem…

【UnityC#】List.Add、LinkedList.Add、HashSet.Add、Dictionary.Addの速度比較

List.Add、LinkedList.Add、HashSet.Add、Dictionary.Addの速度比較をしたので結果を残しておきます。 [ 環境 ] Unity 2018.4.14.f1 [ 比較結果 ] List.Add 0 ms LinkedList.Add 23 ms HashSet.Add 2 ms Dictionary.Add 6 ms [ スクリプト ] using System.Co…

【UnityC#】Text/TextMeshProコンポーネントの行数の取得方法

Text/TextMeshProコンポーネントの行数の取得方法を自分用にメモ。 [ 取得方法 ] Textコンポーネント int LineCount = Text.cachedTextGeneratorForLayout.lineCount; TextMeshProコンポーネント int LineCount = TextMeshPro.textInfo.lineCount;

【UnityC#】LightningのEnviromentにあるIntensity Multiplierの変更方法

LightningのEnviromentにあるIntensity Multiplierの変更方法を自分用にメモ。 [ 変更方法 ] RenderSettings.ambientIntensity = 1.0f; [ 関連記事 ] edunity.hatenablog.com

【UnityC#】LightningのEnviromentにあるAmbient Colorの変更方法

LightningのEnviromentにあるAmbient Colorの変更方法を自分用にメモ。 [ 変更方法 ] RenderSettings.ambientLight = new Color(1.0f, 1.0f, 1.0f, 1.0f); [ 関連記事 ] edunity.hatenablog.com

【UnityC#】親/子オブジェクトの取得方法一覧

自分用に親オブジェクトと子オブジェクトの取得方法一覧を作ることにしました。 [ 取得方法一覧 ] 親オブジェクト 親オブジェクト取得 一番上の親オブジェクト取得 子オブジェクト n番目の子オブジェクト取得 全子オブジェクト取得 子オブジェクト数取得 [ …

【UnityC#】Lightコンポーネントの色の変更方法

Lightコンポーネントの色の変更方法を自分用にメモ。 [ 変更方法 ] GameObject.Find("Directional Light").GetComponent<Light>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);</light>

【UnityC#】ゲームオブジェクトのレイヤーの変更方法

ゲームオブジェクトのレイヤーの変更方法を自分用にメモ。 [ 方法 ] "hogehoge"はレイヤー名です。 gameObject.layer = LayerMask.NameToLayer("HogeHoge");

【UnityC#】n番目の子オブジェクトの取得方法

n番目の子オブジェクトの取得方法を自分用にメモ。 [ 方法 ] nは任意の値 gameObject.transform.GetChild(n).gameObject

【UnityC#】RectTransformのWidthとHeightの変更方法

RectTransformのWidthとHeightの変更方法をメモすることにしました。 [ 変更方法 ] using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScriptTest : MonoBehaviour { private void Start() { float Width = 100…

【UnityC#】eulerAnglesをClampする方法

eulerAnglesをClampする方法を残しておくことにしました。 [ 方法 ] 下記はtransform.eulerAngles.xの範囲を-45から45にClampしています。 Vector3 EulerAngle = transform.eulerAngles; float EulerAngleX = EulerAngle.x;// 0 ~ 360 EulerAngleX = Mathf.R…

【UnityC#】処理時間の計測方法

処理時間の計測方法を自分用にメモすることにしました。 [ 計測方法 ] using UnityEngine; public class ScriptTest : MonoBehaviour { private void Start() { System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch(); StopWatch.St…

【UnityC#】HashSetの使い方

HashSetの使い方を自分用にメモすることにしました。 [ HashSetとは ] [ 使い方 ] 宣言 初期化 要素の追加 要素の消去 全要素の消去 要素数を取得 特定の要素を含むか [ 関連記事 ] [ HashSetとは ] HashSetとは特定の要素を含むかどうかの判定はArrayやList…

【UnityC#】文字列操作まとめ

自分用に文字列操作のまとめのようなもの作ってみました。 [ 文字列操作まとめ ] 文字列の連結 文字列の改行 文字列を比較 文字列の一部を消去 文字列の一部を置換 文字列の一部を取得 文字列の長さ(文字数)を取得 文字列が特定の文字を含むか取得 特定の文…

【UnityC#】int形からbool型に変換する方法

intからboolに変換する方法をメモ。 [ 方法 ] 「Convert.ToBoolean(int)」で変換できます。 [ 使用例 ] 下記はint型のIntTestをbool型に変換しBoolTestに代入しています。 using System.Collections; using System.Collections.Generic; using UnityEngine; …

【UnityC#】高速化する方法

UnityC#を高速化する方法を自分用にメモすることにしました。 [ 対処法 ] GameObject.Find()の使用を避ける GetComponentの使用を避ける transformの読み書きを避ける Instantiate()の使用を避ける Destroy()の使用を避ける GCが重たくなる処理を避ける Hash…