やまめも

【 ゲーム開発・日々のことについて 】

【C#】Delegate使用 Event 使用例 第二弾

EventManager.cs

using UnityEngine;
using System.Collections;

public class EventManager {
	
	public delegate void AchievementTapHandler(int number);
	public static event AchievementTapHandler onAchievementTapE;
	public static void onAchievementTap(int number) { 
		if (onAchievementTapE != null) 
			onAchievementTapE(number); 
	}
}

※オブジェクトとしてhierarckyに置かず、Assetに置いたままにできる。( : MonoBehaviourを外す)

AchievementNotif.cs

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

public class AchievementNotif : MonoBehaviour {
	public AchievementContentNotif _achievementContentNotif;
	
	#region Event
	void OnEnable(){
		EventManager.onAchievementTapE += ShowBanner;
	}
	void OnDisable(){
		EventManager.onAchievementTapE -= ShowBanner;		
	}
	#endregion
	
	void Awake (){
	}
	void ShowBanner (int i) {
		List<AchievementContents> _myCollection =  AchievementSource._instance._myCollection;
		_achievementContentNotif.SetContent(_myCollection[i]._name, _myCollection[i]._type, _myCollection[i]._goal, _myCollection[i]._reward);
		
	}
}

AchievementManager.cs

	public void CheckAchievement(ACHIEVEMEN_TYPE type , double value){

		List<AchievementContents> items = new List<AchievementContents>();

		switch(type){
		case ACHIEVEMEN_TYPE.TAP:
			items = AchievementSource._instance.RetrieveAchievement(type);
			for(int i = 0 ; i < items.Count ; i++){

				if(!items[i]._unlocked){
					double t = 0;
					double.TryParse(items[i]._goal,out t );
					if( tapCounter >= t ){
						items[i]._unlocked = true;

						EventManager.onAchievementTap(i);

					}
				}
			}
			break;
		case ACHIEVEMEN_TYPE.UPGRADE_HERO:
			break;
		case ACHIEVEMEN_TYPE.GOLD_COLLECT:
			break;
		}
	}