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

1−0

void RewardTTP(string Id)
{
	**.Instance.GiveRewards (Id, delegate(int errorCode) {
	ClaimItem (errorCode);
	});
}
void ClaimItem (int errorCode) 
{
	if (errorCode == 0) 
	{
		EventManager.OnUpdateNotif (true, "");
		**.Achievement.ShowAchievementResultView ();
	}
	else if (errorCode == 204) {		//Reward Redeemed
		EventManager.OnUpdateNotif (false, "You Already Claimed This Item");
	}
	else if (errorCode == -1) {			//Time Out
		EventManager.OnUpdateNotif (false, "Check Your Connection");
	}
}

public void GiveRewards(string Id, Action<int> callback){
	callback(0);
}

int型のアクションを呼ぶ。callback(0);の形で返す。


1−1

void RewardTTP(string Id)
{
	**.Instance.GiveRewards (Id, delegate(int errorCode) {
	if (errorCode == 0) 
	{
		EventManager.OnUpdateNotif (true, "");
		**.Achievement.ShowAchievementResultView ();
	}
	else if (errorCode == 204) {		//Reward Redeemed
		EventManager.OnUpdateNotif (false, "You Already Claimed This Item");
	}
	else if (errorCode == -1) {			//Time Out
		EventManager.OnUpdateNotif (false, "Check Your Connection");
	}
	});
}

【Unity ライブラリ】"Easy Save 2" : List型のデータを保存

配列型ではセーブできなかったのArray型でES2に保存。
(保存したいデータに、項目が複数ある場合は新しくクラスを作って、IDをつけて保存するのも一つの手段。)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test_List : MonoBehaviour {

	public List<bool> _isFinishStep;

	void Awake()
	{
		LoadData();
	}
	private void LoadData()
	{
		if(ES2.Exists("isFinishTutorial"))
		{
			_isFinishStep= ES2.LoadList<bool>("isFinishTutorial");
			Debug.Log("es Created");
		}
		else
		{
			_isFinishStep =  new List<bool> ();
			for(int i = 0; i < 11 ; i ++ )
				_isFinishStep.Add(false);
			SaveData();
		}

	}
	private void SaveData()
	{
		ES2.Save (_isFinishStep, "isFinishTutorial");

	}
	private void Update()
	{
		if(Input.GetKeyDown(KeyCode.Space)){
			_isFinishStep = ES2.LoadList<bool>("isFinishTutorial");
			for(int i =0; i < _isFinishStep.Count ;i++){
				Debug.Log("i " + i + " = "+  _isFinishStep[i]);

			}
		}
	}
	void FinishStep(int id)
	{
		_isFinishStep[id] = true;
		SaveData();
	}
}

kido0617.github.io

docs.moodkie.com

同じクラス内に同じ名前のメソッド

同じクラス内で、引数または、アクセス修飾子(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) 使用例 第二弾

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#】 Dictionary 使用例

qiita.com

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