「Unity/Csharp/クラス/基本」の版間の差分
ナビゲーションに移動
検索に移動
| (同じ利用者による、間の19版が非表示) | |||
| 28行目: | 28行目: | ||
} | } | ||
} | } | ||
==継承先から継承元のconstructにアクセス== | |||
base()を記述 | |||
<pre> | |||
public class Animal { | |||
public Animal() | |||
{ | |||
} | |||
} | |||
public class Cat : Animal { | |||
public Cat() : base() | |||
{ | |||
} | |||
} | |||
</pre> | |||
参考:https://kan-kikuchi.hatenablog.com/entry/Constructor_Inheritance | |||
==継承と基底クラスのメソッドへのアクセス== | ==継承と基底クラスのメソッドへのアクセス== | ||
| 50行目: | 66行目: | ||
===メンバがpublicであるときは、以下のようにも生成可能=== | ===メンバがpublicであるときは、以下のようにも生成可能=== | ||
car.cs | |||
<pre> | <pre> | ||
public class Car | public class Car | ||
| 56行目: | 73行目: | ||
public bool IsNew = false; | public bool IsNew = false; | ||
} | } | ||
</pre> | |||
main.cs | |||
<pre> | |||
var car = new Car() | var car = new Car() | ||
{ | { | ||
Name = " | Name = "nsx", | ||
IsNew = true | IsNew = true | ||
}; | |||
</pre> | |||
===クラスが入ってる配列の場合=== | |||
<pre> | |||
List<Car> cars = new List<Car> | |||
{ | |||
new Car() { Name = "nsx", IsNew = true}, | |||
new Car() { Name = "gto", IsNew = true}, | |||
}; | |||
</pre> | |||
or | |||
new()のパターン | |||
<pre> | |||
List<Car> cars = new List<Car> | |||
{ | |||
new() { Name = "nsx", IsNew = true}, | |||
new() { Name = "gto", IsNew = true}, | |||
}; | }; | ||
</pre> | </pre> | ||
| 89行目: | 128行目: | ||
interface ICharacter | interface ICharacter | ||
{ | { | ||
string characterName{get;set;} | string characterName { get; set; } | ||
string characterName2{get;set;} | string characterName2 { get; set; } | ||
void AnimeInit(); | void AnimeInit(); | ||
void SetKind(string kind); | void SetKind(string kind); | ||
| 135行目: | 174行目: | ||
Debug.Log("className=" + this.GetType().Name); | Debug.Log("className=" + this.GetType().Name); | ||
== | ==オブジェクトの複製== | ||
<pre> | <pre> | ||
public User Clone() | public User Clone() | ||
| 179行目: | 218行目: | ||
参考:https://qiita.com/RyotaMurohoshi/items/d94ed539935dfb2fcdcd | 参考:https://qiita.com/RyotaMurohoshi/items/d94ed539935dfb2fcdcd | ||
==セッターゲッター関数== | |||
<pre> | |||
private bool test = false; | |||
public bool Test { | |||
get { | |||
if (test == null) return false; | |||
return test; | |||
} | |||
set { | |||
if (test == null) return false; | |||
test = value; | |||
} | |||
} | |||
</pre> | |||
参考:https://it-rpa.hatenablog.com/entry/C_%E3%83%97%E3%83%AD%E3%83%91%E3%83%86%E3%82%A3_Set_Get | |||
==interfaceを使ったゲッターセッター== | ==interfaceを使ったゲッターセッター== | ||
| 250行目: | 305行目: | ||
==プロパティ存在有無判定== | ==プロパティ存在有無判定== | ||
if (room.GetType().GetProperty("key") == null) | if (room.GetType().GetProperty("key") == null) | ||
==継承禁止== | |||
sealedを使う | |||
<pre> | |||
sealed class A {} | |||
class B : A {} //エラー | |||
</pre> | |||
==2つの値を返す場合== | |||
<pre> | |||
void Start() | |||
{ | |||
var (userName, age) = DoubleFunction(); | |||
Debug.Log("userName=" + userName); | |||
Debug.Log("userAge=" + age); | |||
} | |||
(string userName, int age) DoubleFunction() | |||
{ | |||
return ("taro", 19); | |||
} | |||
</pre> | |||
参考:https://www.create-forever.games/return-tuple/ | |||
==クラス名を文字列に== | |||
Debug.Log(typeof(HogeModel).Name); // HogeModel | |||
2025年3月20日 (木) 14:47時点における最新版
クラスやプロパティ
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;
}
}
継承先から継承元のconstructにアクセス
base()を記述
public class Animal {
public Animal()
{
}
}
public class Cat : Animal {
public Cat() : base()
{
}
}
参考:https://kan-kikuchi.hatenablog.com/entry/Constructor_Inheritance
継承と基底クラスのメソッドへのアクセス
base.を使う
abstract class Animal
{
public virtual void Call()
{
}
}
class Cat : Animal
{
public override void Call()
{
base.Call();
}
}
単純クラスインスタンス生成方法
Animal animal = new Animal();
メンバがpublicであるときは、以下のようにも生成可能
car.cs
public class Car
{
public string Name = default;
public bool IsNew = false;
}
main.cs
var car = new Car()
{
Name = "nsx",
IsNew = true
};
クラスが入ってる配列の場合
List<Car> cars = new List<Car>
{
new Car() { Name = "nsx", IsNew = true},
new Car() { Name = "gto", IsNew = true},
};
or
new()のパターン
List<Car> cars = new List<Car>
{
new() { Name = "nsx", IsNew = true},
new() { Name = "gto", IsNew = true},
};
オブジェクトクラスインスタンス生成方法
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
複数継承する場合
,で区切る
public class PandaCharacter : MonoBehaviour, ICharacter
{
}
オブジェクト子クラス名取得
Debug.Log("className=" + this.GetType().Name);
オブジェクトの複製
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
セッターゲッター関数
private bool test = false;
public bool Test {
get {
if (test == null) return false;
return test;
}
set {
if (test == null) return false;
test = value;
}
}
参考:https://it-rpa.hatenablog.com/entry/C_%E3%83%97%E3%83%AD%E3%83%91%E3%83%86%E3%82%A3_Set_Get
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; }
}
setとgetとinterfaceとSerializeFieldの場合
Player.cs
public class Player : IPlayer
{
[SerializeField] string name = "taro";
public string Name {
get { return name; }
set { name = value; }
}
}
IPlayer.cs
public interface IPlayer
{
string Name { get; set; }
}
SerializeFieldを使ったプロパティ
class Hoge {
[SerializeField]
string url;
}
- [SerializeField]をつけると、privateであってもInspectorのところから値を設定できる。
- publicにしても、Inspectorのところから値を設定できるが、他のクラスから変更できるので、Inspectorのところから値を設定するというのを目的にしてるのであれば、SerializeFieldをつけるほうが良い。
- publicやprivateを、省略すると、privateになる
参考:https://qiita.com/makopo/items/8ef280b00f1cc18aec91
プロパティ存在有無判定
if (room.GetType().GetProperty("key") == null)
継承禁止
sealedを使う
sealed class A {}
class B : A {} //エラー
2つの値を返す場合
void Start()
{
var (userName, age) = DoubleFunction();
Debug.Log("userName=" + userName);
Debug.Log("userAge=" + age);
}
(string userName, int age) DoubleFunction()
{
return ("taro", 19);
}
参考:https://www.create-forever.games/return-tuple/
クラス名を文字列に
Debug.Log(typeof(HogeModel).Name); // HogeModel