【C#】デリゲート(delegate) 使用例 第二弾

ufcpp.net

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#】スコープを浅くする

スコープが深いと読みづらい

良くない例

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"にする必要がある。

【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") + "%";

【C#】保守しやすいように書く

1. TODO: を活用する


2. より簡潔に

private void SetAllButtonToDefault () {
	//一行で書ける。
	btnNormal.color = btnHard.color = btnImpossibru.color = Color.gray;
//	btnNormal.color = Color.gray;
//	btnHard.color = Color.gray;
//	btnImpossibru.color  = Color.gray;
}


参考資料