拡張メソッド
| 書き方 | 説明 | 注意 |
|---|---|---|
ExtStaticHelpers.Twice(2) | 型名からの呼び出し | |
public static int Twice(this int n) | 組み込みの型への追加 | |
public static int Double(this ExtPoint p) | 自作の値型への追加 | |
public static float Flat(this Vector3 v) | 実行環境の型への追加 | 型の定義を持っていなくても書けます。 |
public static readonly int Max = Base * 2; | 静的な読み取り専用フィールド | 初期化子には、定数式と new Vector3(…) のような値の生成を書けます(const も読めます)。 |
public static int Half => Size / 2; | 取得だけの静的プロパティ | 本体を書く形と、初期化子を付けた { get; } の形があります。 |
Twice(int) / Twice(float) / Twice(int, int) | 引数の違う同じ名前のメソッド |
非対応(5)
Section titled “非対応(5)”| 書き方 | 説明 | エラー | 理由 | 代替 |
|---|---|---|---|---|
public static int Count = 0;(静的クラスの中) | 変更できる静的フィールドを読み書きする | TUKI0001 | 設計 | static readonly にする(変わる値は Behaviour のフィールドに置く) |
public static int Volume { get; set; } | set を持つ静的プロパティを読む | TUKI0001 | 設計 | get だけにするか、Behaviour のフィールドに置く |
public static readonly int Max; | 初期化子の無い静的な読み取り専用フィールドを読む | TUKI0001 | 設計 | = … で値を書く |
public static readonly int B = A * 2; | 初期化子から、ほかの静的フィールドを読む | TUKI0001 | 設計 | 定数式で書く(const は読める) |
Id(int v) と Id<T>(T v) を並べる | 同じ名前にジェネリックなメソッドを混ぜて呼ぶ | TUKI0001 | 設計 | ジェネリックなほうに別の名前を付ける |
宣言と呼び出し
Section titled “宣言と呼び出し”型名からの呼び出し
Section titled “型名からの呼び出し”using UnityEngine;using Tsukimi;
public static class ExtStaticHelpers{ public static int Twice(int n) { return n * 2; }}
public class ExtStatic : TsukimiBehaviour{ void Start() { Debug.Log(ExtStaticHelpers.Twice(2)); // => 4 }}組み込みの型への追加
Section titled “組み込みの型への追加”using UnityEngine;using Tsukimi;
public static class ExtIntHelpers{ public static int Twice(this int n) { return n * 2; }}
public class ExtPrimitive : TsukimiBehaviour{ void Start() { Debug.Log(2.Twice()); // => 4 }}自作の値型への追加
Section titled “自作の値型への追加”自分で宣言した struct にも追加できます。
using UnityEngine;using Tsukimi;
public struct ExtPoint{ public int X;}
public static class ExtPointHelpers{ public static int Double(this ExtPoint p) { return p.X * 2; }}
public class ExtStruct : TsukimiBehaviour{ void Start() { ExtPoint p = new ExtPoint(); p.X = 2; Debug.Log(p.Double()); // => 4 }}実行環境の型への追加
Section titled “実行環境の型への追加”実行環境が公開している型にも追加できます。
using UnityEngine;using Tsukimi;
public static class ExtVectorHelpers{ public static float Flat(this Vector3 v) { return v.x + v.z; }}
public class ExtEnvironment : TsukimiBehaviour{ void Start() { Debug.Log(Vector3.one.Flat()); // => 2 }}静的クラスのメンバ
Section titled “静的クラスのメンバ”静的な読み取り専用フィールド
Section titled “静的な読み取り専用フィールド”静的クラスに static readonly のフィールドを置き、クラス名を付けて読めます。
using UnityEngine;using Tsukimi;
public static class ExtLimits{ public const int Base = 5; public static readonly int Max = Base * 2; public static readonly Vector3 Lift = new Vector3(0f, 2f, 0f); public static readonly string Label = "deck";}
public class ExtReadonly : TsukimiBehaviour{ void Start() { Debug.Log(ExtLimits.Max); // => 10 Debug.Log(ExtLimits.Lift.y); // => 2 Debug.Log(ExtLimits.Label); // => "deck" }}取得だけの静的プロパティ
Section titled “取得だけの静的プロパティ”get だけの 静的プロパティを置けます。
using UnityEngine;using Tsukimi;
public static class ExtGrid{ public static readonly int Size = 8; public static int Half => Size / 2; public static int Last { get { return Size - 1; } } public static int Cells { get; } = 64;}
public class ExtGetter : TsukimiBehaviour{ void Start() { Debug.Log(ExtGrid.Half); // => 4 Debug.Log(ExtGrid.Last); // => 7 Debug.Log(ExtGrid.Cells); // => 64 }}引数の違う同じ名前のメソッド
Section titled “引数の違う同じ名前のメソッド”引数の型か数が違えば、同じ名前のメソッドを並べられます。
using UnityEngine;using Tsukimi;
public static class ExtOverloads{ public static int Twice(int n) { return n * 2; } public static float Twice(float n) { return n * 2f; } public static int Twice(int n, int times) { return n * 2 * times; }}
public class ExtOverload : TsukimiBehaviour{ void Start() { Debug.Log(ExtOverloads.Twice(2)); // => 4 Debug.Log(ExtOverloads.Twice(1.5f)); // => 3 Debug.Log(ExtOverloads.Twice(2, 3)); // => 12 }}使った所での検査
Section titled “使った所での検査”静的クラスのメンバは、使った所でだけ検査されます(呼ばないメソッドと、読まないフィールドとプロパティは、書けない書き方を含んでいてもエラーになりません)。
using UnityEngine;using Tsukimi;
public static class ExtTextHelpers{ public static int Count = 0;
public static int Twice(int n) { return n * 2; }
public static int Parse(string s) { try { return int.Parse(s); } catch { return 0; } }}
public class ExtCheckedOnUse : TsukimiBehaviour{ void Start() { Debug.Log(ExtTextHelpers.Twice(2)); // => 4 }}