「Unity/Csharp/クラス」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→クラスやプロパティ) |
(→プロパティ名を文字列に) |
||
(同じ利用者による、間の4版が非表示) | |||
行14: | 行14: | ||
メンバはpublicとprivateがあり省略するとprivateになる。 | メンバはpublicとprivateがあり省略するとprivateになる。 | ||
+ | |||
+ | ==単純クラス== | ||
+ | public class Animal { | ||
+ | private int footCnt = 4; | ||
+ | public void Init () { | ||
+ | } | ||
+ | public void SetFootCnt(int cnt) { | ||
+ | footCnt = cnt; | ||
+ | } | ||
+ | public int GetFootCnt(int cnt) { | ||
+ | return cnt; | ||
+ | } | ||
+ | } | ||
==継承と基底クラスのメソッドへのアクセス== | ==継承と基底クラスのメソッドへのアクセス== | ||
行31: | 行44: | ||
} | } | ||
} | } | ||
+ | |||
+ | ==単純クラスインスタンス生成方法== | ||
+ | Animal animal = new Animal(); | ||
+ | |||
+ | ==オブジェクトクラスインスタンス生成方法== | ||
+ | GameObject gameObj = new GameObject(); | ||
+ | ComScript sc = gameObj.AddComponent<ComScript>(); | ||
+ | sc.Exec (); | ||
+ | |||
+ | ComScriptはMonoBehaviourを継承している必要がある。 | ||
+ | |||
+ | 継承てない場合は、以下エラーが出る | ||
+ | error CS0311: The type `ComScript' cannot be used as type parameter `T' in the generic type or method `UnityEngine.GameObject.AddComponent<T>()'. There is no implicit reference conversion from `ComScript' to `UnityEngine.Component' | ||
+ | |||
+ | ==新規画像インスタンス生成方法== | ||
+ | [[Unity/Csharp/画像ロード]] [ショートカット] | ||
+ | |||
+ | ==オブジェクトについてるcsを呼び出し実行する方法== | ||
+ | GameObject obj = transform.Find ("/mc/mc1").gameObject; | ||
+ | ComScript sc = gameObj.GetComponent<ComScript>(); | ||
+ | sc.Exec (); | ||
+ | |||
+ | ==プロパティ名を文字列に== | ||
+ | Debug.Log(nameof(Example.Hoge)); // Hoge |
2019年5月30日 (木) 11:14時点における版
目次
クラスやプロパティ
public class CanvasScript : MonoBehaviour { private int cnt = 1; void Start () { Debug.Log("helloworld" + cnt); Add(10, 15) } void Update () { } public int Add(int x, int y) { return x + y; } }
メンバはpublicとprivateがあり省略するとprivateになる。
単純クラス
public class Animal { private int footCnt = 4; public void Init () { } public void SetFootCnt(int cnt) { footCnt = cnt; } public int GetFootCnt(int cnt) { return cnt; } }
継承と基底クラスのメソッドへのアクセス
base.を使う
abstract class Animal { public virtual void Call() { } } class Cat : Animal { public override void Call() { base.Call(); } }
単純クラスインスタンス生成方法
Animal animal = new Animal();
オブジェクトクラスインスタンス生成方法
GameObject gameObj = new GameObject(); ComScript sc = gameObj.AddComponent<ComScript>(); sc.Exec ();
ComScriptはMonoBehaviourを継承している必要がある。
継承てない場合は、以下エラーが出る
error CS0311: The type `ComScript' cannot be used as type parameter `T' in the generic type or method `UnityEngine.GameObject.AddComponent<T>()'. There is no implicit reference conversion from `ComScript' to `UnityEngine.Component'
新規画像インスタンス生成方法
Unity/Csharp/画像ロード [ショートカット]
オブジェクトについてるcsを呼び出し実行する方法
GameObject obj = transform.Find ("/mc/mc1").gameObject; ComScript sc = gameObj.GetComponent<ComScript>(); sc.Exec ();
プロパティ名を文字列に
Debug.Log(nameof(Example.Hoge)); // Hoge