JavaScript => C#
Unityの開発をJavaScriptからC#へ移行しようと決断。
だいたいの機能はこちらのMasamitsu Ishikawaさんが公開されているSlideShareの資料を見れば網羅できそうな感じなのかしら。
自分なりのC#メモとしてソース内にソースとコメントで説明。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/**
* C# Test Class.
*/
public class CSharpTest : MonoBehaviour {
/**
* スタティック変数。
* constは値型のみ、readonlyは参照型にも付けられる。
*/
public const int CONST_NUM1 = 400;
public static readonly int CONST_NUM2 = 500;
/**
* valueのアクセサ。
*/
public int value1 {
// アクセサの実装
get { return this._value1; }
set { this._value1 = value; }
}
/**
* 値2。
*/
public int value2 { get; private set; } // 実装省略
/**
* インデクサーの定義。
*/
public int this[int index] {
get { return this.array[index]; }
set { this.array[index] = value; }
}
/**
* Serializableテスト。
* privateをInspector上で触れ、publicを触れないように。
* スライダーで値調整。
*/
[SerializeField]
[Range(1, 10)]
private int serializableNum;
[System.NonSerialized]
public int nonSerializableNum;
/**
* 値。
*/
private int _value1 = 100;
/**
* 配列。
*/
private int[] array;
/**
* enumテスト。
* JavaScriptでも使えますが。
*/
enum EnumValues {
ENUM_A,
ENUM_B,
ENUM_E = 5,
ENUM_F
}
private EnumValues enumVal;
/**
* デリゲート。
*/
public delegate void DelegateMethod(int num);
public event DelegateMethod onMethod; // eventを付けることにより外から呼べなくなる
public void MethodTest(int num) {
Debug.Log("MethodTest");
}
/**
* overrideテスト。
*/
public virtual int virtualMethod() { // virtual は継承クラスのオーバーライドを許可
return 600;
}
/**
* Start.
*/
public void Start() {
// positionの代入はjsと違いVector3を入れる
// this.transform.position.x = 0; // NG
this.transform.position = new Vector3(0, 0, 0);
// アクセサテスト1
Debug.Log(this.value1); // MonoBehaviourを継承していればprintも可能だが、Debug.Logの方が高速
this.value1 += 3;
Debug.Log(this.value1);
// アクセサテスト2
this.value2 = 200;
Debug.Log(this.value2);
// インデクサのテスト
this.array = new int[2];
this.array[0] = 300;
this.array[1] = 301;
Debug.Log(this[0]);
// 定数
Debug.Log(CSharpTest.CONST_NUM1);
Debug.Log(CSharpTest.CONST_NUM2);
// オーバーライドテスト
Debug.Log(this.virtualMethod());
// enumテスト
this.enumVal = EnumValues.ENUM_F;
Debug.Log(this.enumVal);
// ジェネリック
Transform trans = this.gameObject.GetComponent<Transform>(); // JSであれば this.gameObject.GetComponent() as Transform
Debug.Log(trans);
// 配列
string[] strs = new string[3]; // 型が決まっている場合は var strs = new string[3] でも可能
ArrayList arrList = new ArrayList(); // 数が決まっていない配列
List<int> list = new List<int>(); // System.Collections.Generic内で定義されているジェネリック対応版ArrayList
// デリゲート
DelegateMethod someMethod = this.MethodTest;
someMethod(3);
// ラムダ式(メソッド名を付けるまでもないメソッド内の即席のメソッド)
DelegateMethod lambdaMethod = (int num) => {
Debug.Log("ラムダテスト : " + num);
};
lambdaMethod(10);
}
}
/**
* 構造体。
*/
public struct StructTest {
public int a;
public int b;
public StructTest(int a, int b) {
this.a = a;
this.b = b;
}
}
using UnityEngine;
using System.Collections;
/**
* C# Test Extend Class.
*/
namespace Space1 { // namespaceも付けられる
public sealed class CSharpExtendTest : CSharpTest { // sealed はこれ以上継承できないように
/**
* overrideテスト。
*/
public override int virtualMethod() {
return base.virtualMethod() + 1000;
}
/**
* Start.
*/
public void Start() {
// オーバーライドテスト
Debug.Log(this.virtualMethod());
}
}
}
Unityでどの程度使うか分からないけどその他に
refとoutについて
++C++;// 未確認飛行 C | 引数の参照渡し
無名関数なんかの当たりがJavaScriptに比べてC#だとややこしそう。LINQなんかは少し書式が慣れないけど、追々覚えられるかな。
他にもこんな資料があった。
コメントを残す