「Unity/Csharp/クラス」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
|||
| 2行目: | 2行目: | ||
public class CanvasScript : MonoBehaviour { | public class CanvasScript : MonoBehaviour { | ||
private int cnt = 1; | private int cnt = 1; | ||
void Start () { | void Start() { | ||
Debug.Log("helloworld" + cnt); | Debug.Log("helloworld" + cnt); | ||
Add(10, 15) | Add(10, 15) | ||
} | } | ||
void Update () { | void Update() { | ||
} | } | ||
public int Add(int x, int y) { | public int Add(int x, int y) { | ||
| 18行目: | 18行目: | ||
public class Animal { | public class Animal { | ||
private int footCnt = 4; | private int footCnt = 4; | ||
public void Init () { | public void Init() { | ||
} | } | ||
public void SetFootCnt(int cnt) { | public void SetFootCnt(int cnt) { | ||
| 51行目: | 51行目: | ||
GameObject gameObj = new GameObject(); | GameObject gameObj = new GameObject(); | ||
ComScript sc = gameObj.AddComponent<ComScript>(); | ComScript sc = gameObj.AddComponent<ComScript>(); | ||
sc.Exec (); | sc.Exec(); | ||
ComScriptはMonoBehaviourを継承している必要がある。 | ComScriptはMonoBehaviourを継承している必要がある。 | ||
| 64行目: | 64行目: | ||
GameObject obj = transform.Find ("/mc/mc1").gameObject; | GameObject obj = transform.Find ("/mc/mc1").gameObject; | ||
ComScript sc = gameObj.GetComponent<ComScript>(); | ComScript sc = gameObj.GetComponent<ComScript>(); | ||
sc.Exec (); | sc.Exec(); | ||
==プロパティ名を文字列に== | ==プロパティ名を文字列に== | ||
2021年10月14日 (木) 16:03時点における版
クラスやプロパティ
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
interface
インターフェイス例
interface ICharacter
{
string characterName{get;set;}
string characterName2{get;set;}
void AnimeInit();
void SetKind(string kind);
void AnimePlay();
void AnimeStop();
}
インターフェイス実装
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()
{
}
}
interfaceと継承を同時に
参考:https://qiita.com/chocolamint/items/3fe51eaa7142d2b4f6de
オブジェクト子クラス名取得
Debug.Log("className=" + this.GetType().Name);
オブジェクトのclone
public User Clone()
{
return (User)MemberwiseClone();
}
参考:https://smdn.jp/programming/netfx/cloning/
セッターゲッター
旧
public class Player
{
private string name = "taro";
public string Name {
get { return name; }
set { name = value; }
}
}
新
public class Name
{
public string Name { get; set; } = "taro";
}
新
private string name = "taro";
public string Name
{
get => name;
set => name = value;
}
新
private string name = "taro";
public string Name { get => name; set => name = value; }
参考:https://qiita.com/RyotaMurohoshi/items/d94ed539935dfb2fcdcd
interfaceを使ったゲッターセッター
getのみの場合
ICar.cs
public interface ICar
{
string Name { get; }
}
Car.cs
public class Car : ICar
{
private string name = default;
public string Name => name;
}
setとgetの場合
ICar.cs
public interface ICar
{
string Name { get; set; }
}
Car.cs
public class Car : ICar
{
private string name = default;
public string Name { get => name; set => name = value; }
}
SerializeFieldを使ったプロパティ
class Hoge {
[SerializeField]
string url;
}
[SerializeField]をつけるとprivateであってもAddComponentのところからプロパティを設定できる?
プロパティ存在有無判定
if (room.GetType().GetProperty("key") == null)