Mastering App Development with Unity 3D: Game Achievements Reusable Module

0 of 13 lessons complete (0%)

App Development

The Initialization class

The first class to implement is the Initialization class, its responsibility is to contain all the use case objects references (either drag and dropped though the Unity Editor or by fetching references internally) for the Controller and Presenter blocks.

Every work is done in the Awake MonoBehaviour call.
Here we have to link the use case functional blocks together and to provide some validation pattern to ensure every reference is setup correctly.

Define the following ObjectivesInitialization class :

using UnityEngine;

using UnityEngine.UI;

public class ObjectivesInitialization : MonoBehaviour {

// MARK: Controller Initialization


private ObjectivesController controller = null;

// MARK: Presenter Initialization


private ObjectivesPresenter presenter = null;


public Text gameObjectivesTitle; 


public Objectives.ObjectivesViewModel[] objectivesView;

 public Sprite starOn;

 public Sprite starOff;

// MARK: MonoBehaviour

void Awake() {


// MARK: Initialization

public void init () {

 if (this.controller == null)

Debug.LogError(“ObjectivesController not set properly !”);

if (this.presenter == null)
 Debug.LogError(“ObjectivesPresenter not set properly !”);

if (this.objectivesView == null)
 Debug.LogError(“objectivesView not set properly !”);



private void initController() {


//map claim buttons actions to corresponding use case
 for (int index = 0; index < objectivesView.Length; index++)

int currentIndex = index;

objectivesView[currentIndex].claimButton.onClick.AddListener(delegate { controller.claimReward(currentIndex); } );




We also choose to add the claim buttons target function manually to reduce the module integration process duration. The user just have to give the objectives view references through the inspector, the Unity UI button onClick event will be mapped to corresponding delegates through the setup code to allow more flexibility.

Note: As we game objectives title and stars can be considered as optional given

your project needs, we don’t add them to the validation list (we could also have added them as warning logs).

Note: if you implement the Awake() MonoBehaviour method both in the Initialization and the Controller classes you should ensure that the Initialization awake call is done prior to the Controller one so all variables are correctly initialized. You can do so by giving it a highest execution order priority in Edit\Project Settings\Script Execution Order.