LoginSignup
0
0

More than 5 years have passed since last update.

拡張メソッド作り方 メモ

Last updated at Posted at 2018-04-20

参考URLを見ながら作ってみた

参考URL:http://www.atmarkit.co.jp/ait/articles/1711/22/news030.html

共通データに途中で動的に何かを足すときに役立つかも。

戻り値でデータクラスを返すの要らないかもですね。

■呼び出し元クラス(ボタンクリックされると呼ばれる)

using UnityEngine;

public class ExtensionTest : MonoBehaviour {

    public void OnExtensionTest()
    {
        var test = new Test()
        {
            a = 1,
            b = "abc"
        };

        test.AddValue();

        Debug.Log("a = " + test.a.ToString());
        Debug.Log("b = " + test.b);
        Debug.Log("c = " + test.c);
        Debug.Log("d = " + test.d);
    }
}

■データクラス


public class Test {

    public int a;

    public string b;

    public bool c = false;

    public float d = 0.0f;
}

■拡張クラス

public static class TestExtension  {

    public static void AddValue(this Test test)
    {
        test.c = true;
        test.d = 123.4f;
    }
}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0