Last active
September 2, 2020 07:48
-
-
Save Marsgames/f5808a289f2ee135ac08930937787c68 to your computer and use it in GitHub Desktop.
Exemple of static Instance for a GameManager, SoundManager, etc... in Unity + GameManager version ready for ads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Author | |
///////////////////////////////////////// | |
// RAPHAËL DAUMAS --> GameManager | |
// https://raphdaumas.wixsite.com/portfolio | |
// https://github.com/Marsgames | |
///////////////////////////////////////// | |
#endregion | |
using UnityEngine; | |
[HelpURL("https://gist.github.com/Marsgames/f5808a289f2ee135ac08930937787c68")] | |
public class GameManager : MonoBehaviour | |
{ | |
#region Variables | |
[HideInInspector] public static GameManager Instance { get; private set; } | |
#endregion Variables | |
/////////////////////////////////////////////////////////// | |
#region Unity's functions | |
private void Awake() | |
{ | |
if (!Instance) | |
{ | |
Instance = this; | |
} | |
else if (this != Instance) | |
{ | |
Destroy(gameObject); | |
} | |
DontDestroyOnLoad(this); | |
} | |
private void Start() | |
{ | |
CheckIfSetUp(); | |
} | |
#endregion Unity's functions | |
/////////////////////////////////////////////////////////// | |
#region Functions | |
#endregion Functions | |
/////////////////////////////////////////////////////////// | |
#region Accessors | |
#endregion Accessors | |
/////////////////////////////////////////////////////////// | |
#region Utils | |
/// <summary> | |
/// Checks if all variables are set up correctly, otherwise close Editor | |
/// </summary> | |
private void CheckIfSetUp () | |
{ | |
#if UNITY_EDITOR | |
bool isSetUp = true; | |
if (!randomVariable) | |
{ | |
Debug.LogError($"<b>Random Variable</b> cannot be null in <color=#00f> {name} </color>", gameObject); | |
isSetUp = false; | |
} | |
UnityEditor.EditorApplication.isPlaying = isSetUp; | |
#endif | |
} | |
#endregion Utils | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Author | |
///////////////////////////////////////// | |
// RAPHAËL DAUMAS --> GameManager | |
// https://raphdaumas.wixsite.com/portfolio | |
// https://github.com/Marsgames | |
///////////////////////////////////////// | |
#endregion | |
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.Advertisements; | |
[HelpURL("https://gist.github.com/Marsgames/c7330e598d7e1ef00bdbad07dc450bdb")] | |
public class GameManager : MonoBehaviour, IUnityAdsListener | |
{ | |
#region Variables | |
[HideInInspector] public static GameManager Instance { get; private set; } | |
private string adsID; | |
private bool adsTestMode = false; | |
#endregion Variables | |
/////////////////////////////////////////////////////////// | |
#region Unity's functions | |
private void Awake() | |
{ | |
if (!Instance) | |
{ | |
Instance = this; | |
} | |
else if (this != Instance) | |
{ | |
Destroy(gameObject); | |
} | |
DontDestroyOnLoad(this); | |
} | |
void Start() | |
{ | |
CheckIfSetUp(); | |
#if UNITY_IOS | |
adsID = ; // IOS Ads ID (found on Unity Dashboard) | |
#elif UNITY_ANDROID | |
adsID = ; // Android Ads ID (found on Unity Dashboard) | |
#endif | |
#if UNITY_EDITOR | |
adsTestMode = true; | |
#endif | |
Advertisement.Initialize(adsID, adsTestMode); | |
ShowAds("Banner"); | |
} | |
#endregion Unity's functions | |
/////////////////////////////////////////////////////////// | |
#region Functions | |
/// <summary> | |
/// Show banner when the ad is ready | |
/// </summary> | |
private IEnumerator ShowBannerWhenReady() | |
{ | |
while (!Advertisement.IsReady("Banner")) | |
{ | |
yield return new WaitForSeconds(0.5f); | |
} | |
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER); | |
Advertisement.Banner.Show("Banner"); | |
} | |
#endregion Functions | |
/////////////////////////////////////////////////////////// | |
#region Ads | |
public void ShowAds(string placementID) | |
{ | |
OnUnityAdsReady(placementID); | |
} | |
public void OnUnityAdsReady(string placementId) | |
{ | |
switch (placementId) | |
{ | |
case "rewardedVideo": | |
Advertisement.Show(placementId); | |
break; | |
case "Banner": | |
// PackageManager --> Advertisement --> v3+ requiered | |
StartCoroutine(ShowBannerWhenReady()); | |
break; | |
case "video": | |
Advertisement.Show(placementId); | |
break; | |
default: | |
Debug.LogWarning($"{placementId} is an unknown ads ID", this); | |
break; | |
} | |
} | |
public void OnUnityAdsDidError(string message) | |
{ | |
Debug.LogError(message, this); | |
} | |
public void OnUnityAdsDidStart(string placementId) | |
{ | |
// Maybe put the game in pause | |
Time.timeScale = 0; | |
} | |
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) | |
{ | |
switch (placementId) | |
{ | |
case "rewardedVideo": | |
// Define conditional logic for each ad completion status: | |
if (showResult == ShowResult.Finished) | |
{ | |
// Action to do when the ads end | |
// Go to next level, give reward, double score... | |
} | |
else if (showResult == ShowResult.Skipped) | |
{ | |
// Action to do if the ads is skipped | |
// Go to main menu, go to end menu... | |
} | |
else if (showResult == ShowResult.Failed) | |
{ | |
// Action to do if playing ads failed (no internet) | |
// Debug.LogWarning(), go to main menu, go to end menu... | |
Debug.LogWarning("The ad did not finish due to an error"); | |
} | |
break; | |
case "banner": | |
return; | |
case "video": | |
// Go to next level, go to main menu... | |
break; | |
default: | |
break; | |
} | |
// Restore timescale if changed; | |
Time.timeScale = 1; | |
} | |
#endregion Ads | |
/////////////////////////////////////////////////////////// | |
#region Accessors | |
#endregion Accessors | |
/////////////////////////////////////////////////////////// | |
#region Utils | |
/// <summary> | |
/// Checks if all variables are set up correctly, otherwise close Editor | |
/// </summary> | |
private void CheckIfSetUp () | |
{ | |
#if UNITY_EDITOR | |
bool isSetUp = true; | |
if (!randomVariable) | |
{ | |
Debug.LogError($"<b>Random Variable</b> cannot be null in <color=#00f> {name} </color>", gameObject); | |
isSetUp = false; | |
} | |
UnityEditor.EditorApplication.isPlaying = isSetUp; | |
#endif | |
} | |
#endregion Utils | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wonder if I should use a constructor or not