同じクラス内に同じ名前のメソッド
同じクラス内で、引数または、アクセス修飾子(private,public, etc)が違えば、同じ名前のメソッドを定義することができる。
using UnityEngine; using System.Collections; public class Test_SplashScreen : MonoBehaviour { public void Test(int id) { } public void Test(string stg) { } private void Test() { } }
【C#】デリゲート(delegate) 使用例 第二弾
using UnityEngine; using System.Collections; namespace Test { delegate void SomeDelegate(int a); class DelegateTest { public void Main() { SomeDelegate a = new SomeDelegate(A); a(256); } static void A(int n) { Debug.Log("delegate / " + n); } } public class TestClass : MonoBehaviour { void Start() { DelegateTest test1 = new DelegateTest (); test1.Main (); } } }
以下と同じ
using UnityEngine; using System.Collections; namespace Test { delegate void SomeDelegate(int a); class DelegateTest { public void Main() { SomeDelegate a = A; a(256); } static void A(int n) { Debug.Log("delegate / " + n); } } public class TestClass : MonoBehaviour { void Start() { DelegateTest test1 = new DelegateTest (); test1.Main (); } } }
delegate ShowMessageインスタンスに複数の関数を入れることができる。
using UnityEngine; using System.Collections; namespace Test { delegate void ShowMessage(); class DelegateTest { public void Main() { ShowMessage a = A; a += B; a += C; a(); } static void A() { Debug.Log("A\n"); } static void B() { Debug.Log("B\n"); } static void C() { Debug.Log("C\n"); } } public class TestClass : MonoBehaviour { void Start() { DelegateTest test1 = new DelegateTest (); test1.Main (); } } }
【C#】 Dictionary 使用例
example 1
{ public class TestClass : MonoBehaviour { Dictionary<int, string> dic = new Dictionary<int, string>() { { 0, "today's weather is nice!" }, { 1, "today's weather is so so!" }, { 2, "today's weather is really nice!" }, }; void Start() { foreach(int key in dic.Keys) { Debug.Log (dic[key]); } } } }
【C#】スコープを浅くする
スコープが深いと読みづらい
良くない例
if (item[i]._levelId == 0){ if (item[i]._stageId == 1){ // null }else{ _iStagePre.SetSprite(item[i - 1]._stageInGame); } }else{ _iStagePre.SetSprite(item[i - 1]._stageInGame); } _iStageNow .SetSprite(item[i]._stageInGame); _iStageNext.SetSprite(item[i + 1]._stageInGame);
良い例
if (item[i]._levelId != 0 && item[i]._stageId != 1){ _iStagePre.SetSprite(item[i - 1]._stageInGame); } _iStageNow .SetSprite(item[i]._stageInGame); _iStageNext.SetSprite(item[i + 1]._stageInGame);
【C#】デリゲート(delegate) 使用例 第一弾
delegate例
例1
public void FacebookShare (int id) { string msg = BossSource._item[id]._shareHeadline; Sprite ShareSprite = BossSource._item[id]._bossShareSprite; *****.Facebook.PostImageWithDialog(msg ,ShareSprite.texture, OnFacebookShareComplete); } void OnFacebookShareComplete (*****.Social.IActionResult result) { if(result.IsSuccess){ ShowModal("Thanks For Posting"); EventManager.ShareSccessMethod(); }else{ ShowModal(result.Error); } EventManager.onFinishUIShareMethod(); }
例2
public void FacebookShare (int id) { string msg = BossSource._item[id]._shareHeadline; Sprite ShareSprite = BossSource._item[id]._bossShareSprite; *****.Facebook.PostImageWithDialog(msg ,ShareSprite.texture, delegate(*****.Social.IActionResult result) { if(result.IsSuccess){ ShowModal("Thanks For Posting"); EventManager.ShareSccessMethod(); }else{ ShowModal(result.Error); } EventManager.onFinishUIShareMethod(); }); }
【Unity エラー】UnityException : Textureの読み書き
デバイスでバグが確認でき、バグの原因がデバイスだけでわからない時、Android Device Monitor を使って確認する。(モニターでのチェックは必須..?)
エラー
03-30 16:13:01.606: I/Unity(3016): UnityException: Texture 'frame_share_000' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
解決法
- texture[texture type : advance]をスクリプトから読み書きする時、使用する"Read / Write anabled" を"true"にする必要がある。
#if UNITY_EDITOR 使用例
デバイスとエディタで違う出力をしたい時
#if UNITY_EDITOR #else #endif
【C#】ネーミング まとめ
命名の際に注意すること
- 名前に数字を利用するのは良くない
- 略語は出来る限り利用しない。一般的なもののみ利用してもよい。
- 複数形は利用しない。複数形か単数形かを混同したエラーを避ける。
- tblやdbなどの接頭語は冗長であり無駄なので使わない
- ブーリアンのカラム名はisから始める
- http://gokexn.blog.fc2.com/blog-entry-34.html
Codic
関連記事
【C#】ToString("0.0") : 数値型を小数点1桁の文字列型に変換
小数点1桁
_tCompletePercent.text = _percent.ToString("0.0") + "%";
【Unity】RectTransform offsetMin : buttomとleftの値を取得
- offsetMin
UI RectTransformのbuttomとleftの値を取得
- offsetMax
UI RectTransformのtopとrightの値を取得
GetComponent<RectTransform>().offsetMin; GetComponent<RectTransform>().offsetMax;