|
|
| (同じ利用者による、間の16版が非表示) |
| 1行目: |
1行目: |
| ==クラスやプロパティ==
| | [[Unity/Csharp/クラス/基本]] |
| 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になる。
| | [[Unity/Csharp/クラス/アクセス修飾子]] |
|
| |
|
| ==単純クラス==
| | [[Unity/Csharp/クラス/クラス分割]] |
| public class Animal {
| |
| private int footCnt = 4;
| |
| public void Init() {
| |
| }
| |
| public void SetFootCnt(int cnt) {
| |
| footCnt = cnt;
| |
| }
| |
| public int GetFootCnt(int cnt) {
| |
| return cnt;
| |
| }
| |
| }
| |
|
| |
|
| ==継承と基底クラスのメソッドへのアクセス==
| | [[Unity/Csharp/クラス/継承元から継承先実行]] |
| base.を使う
| |
|
| |
|
| abstract class Animal
| | [[Unity/Csharp/クラス/インターフェイス]] |
| {
| |
| public virtual void Call()
| |
| {
| |
| }
| |
| }
| |
| class Cat : Animal
| |
| {
| |
| public override void Call()
| |
| {
| |
| base.Call();
| |
| }
| |
| }
| |
|
| |
|
| ==単純クラスインスタンス生成方法==
| | [[Unity/Csharp/クラス/引数可変]] |
| Animal animal = new Animal();
| |
|
| |
|
| ==オブジェクトクラスインスタンス生成方法==
| | [[Unity/Csharp/クラス/Tuple]] |
| GameObject gameObj = new GameObject();
| |
| ComScript sc = gameObj.AddComponent<ComScript>();
| |
| sc.Exec();
| |
|
| |
|
| ComScriptはMonoBehaviourを継承している必要がある。
| | [[Unity/Csharp/クラス/クラスメンバfor取得]] |
|
| |
|
| 継承てない場合は、以下エラーが出る
| | [[Unity/Csharp/クラス/ジェネリックメソッド]] |
| 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/クラス/ジェネリックインターフェース]] |
| [[Unity/Csharp/画像ロード]] [ショートカット] | |
| | |
| ==オブジェクトについてるcsを呼び出し実行する方法==
| |
| GameObject obj = transform.Find ("/mc/mc1").gameObject;
| |
| ComScript sc = gameObj.GetComponent<ComScript>();
| |
| sc.Exec();
| |
| | |
| ==プロパティ名を文字列に==
| |
| Debug.Log(nameof(Example.Hoge)); // Hoge
| |
| | |
| ==interface==
| |
| インターフェイス例
| |
| <pre>
| |
| interface ICharacter
| |
| {
| |
| string characterName{get;set;}
| |
| string characterName2{get;set;}
| |
| void AnimeInit();
| |
| void SetKind(string kind);
| |
| void AnimePlay();
| |
| void AnimeStop();
| |
| }
| |
| </pre>
| |
| インターフェイス実装
| |
| <pre>
| |
| public class PandaCharacter : ICharacter
| |
| {
| |
| public string _characterName = "taro";
| |
| public string _characterName2 = "jiro";
| |
| string characterName { get => _characterName; set => _characterName = value; }
| |
| string characterName2 { get => _characterName2; set => _characterName2 = value; }
| |
| public void AnimeInit()
| |
| {
| |
| }
| |
| public void SetKind(string kind)
| |
| {
| |
| }
| |
| public void AnimePlay()
| |
| {
| |
| }
| |
| public void AnimeStop()
| |
| {
| |
| }
| |
| }
| |
| </pre>
| |
| | |
| ==interfaceと継承を同時に==
| |
| 参考:https://qiita.com/chocolamint/items/3fe51eaa7142d2b4f6de
| |
| | |
| ==オブジェクト子クラス名取得==
| |
| Debug.Log("className=" + this.GetType().Name);
| |
| | |
| ==オブジェクトのclone==
| |
| <pre>
| |
| public User Clone()
| |
| {
| |
| return (User)MemberwiseClone();
| |
| }
| |
| </pre>
| |
| 参考:https://smdn.jp/programming/netfx/cloning/
| |
| | |
| ==セッターゲッター==
| |
| 旧
| |
| <pre>
| |
| public class Player
| |
| {
| |
| [SerializeField]
| |
| string name = "taro";
| |
| public string Name {
| |
| get { return name; }
| |
| set { name = value; }
| |
| }
| |
| }
| |
| </pre>
| |
| 新
| |
| <pre>
| |
| public class Name
| |
| {
| |
| public string Name { get; set; } = "taro";
| |
| }
| |
| </pre>
| |
| 新
| |
| <pre>
| |
| private string name = "taro";
| |
| public string Name
| |
| {
| |
| get => name;
| |
| set => name = value;
| |
| }
| |
| </pre>
| |
| 新
| |
| <pre>
| |
| private string name = "taro";
| |
| public string Name { get => name; set => name = value; }
| |
| </pre>
| |
| | |
| 参考:https://qiita.com/RyotaMurohoshi/items/d94ed539935dfb2fcdcd
| |
| | |
| ==interfaceを使ったゲッターセッター==
| |
| ===getのみの場合===
| |
| ICar.cs
| |
| <pre>
| |
| public interface ICar
| |
| {
| |
| string Name { get; }
| |
| }
| |
| | |
| </pre>
| |
| Car.cs
| |
| <pre>
| |
| public class Car : ICar
| |
| {
| |
| private string name = default;
| |
| public string Name => name;
| |
| }
| |
| </pre>
| |
| | |
| ===setとgetの場合===
| |
| ICar.cs
| |
| <pre>
| |
| public interface ICar
| |
| {
| |
| string Name { get; set; }
| |
| }
| |
| </pre>
| |
| Car.cs
| |
| <pre>
| |
| public class Car : ICar
| |
| {
| |
| private string name = default;
| |
| public string Name { get => name; set => name = value; }
| |
| }
| |
| </pre>
| |
| | |
| ==SerializeFieldを使ったプロパティ==
| |
| <pre>
| |
| class Hoge {
| |
| [SerializeField]
| |
| string url;
| |
| }
| |
| </pre>
| |
| *[SerializeField]をつけると、privateであってもInspectorのところから値を設定できる。
| |
| *publicにしても、Inspectorのところから値を設定できるが、他のクラスから変更できるので、Inspectorのところから値を設定するというのを目的にしてるのであれば、SerializeFieldをつけるほうが良い。
| |
| *publicやprivateを、省略すると、privateになる
| |
| | |
| 参考:https://qiita.com/makopo/items/8ef280b00f1cc18aec91
| |
| | |
| ==プロパティ存在有無判定==
| |
| if (room.GetType().GetProperty("key") == null)
| |