Last active
July 28, 2020 13:23
-
-
Save ryanmillerca/2dc5f1449623d2a3704ec309170d4db8 to your computer and use it in GitHub Desktop.
A simple script that lets you call UnityEvents based on the current platform (Windows, Mac, Debug, Steam, etc)
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
using UnityEngine; | |
using UnityEngine.Events; | |
public class PlatformBrancher : MonoBehaviour | |
{ | |
public PlatformEvent[] onEnable; | |
public PlatformEvent[] onStart; | |
public PlatformEvent[] onDisable; | |
[Tooltip("to be called manually via PlatformBrancher.OnInvoke()")] | |
public PlatformEvent[] onInvoke; | |
private void Start() | |
{ | |
for (int i = 0; i < onStart.Length; i++) | |
{ | |
FireEvent(onStart[i]); | |
} | |
} | |
private void OnEnable() | |
{ | |
for (int i = 0; i < onEnable.Length; i++) | |
{ | |
FireEvent(onEnable[i]); | |
} | |
} | |
private void OnDisable() | |
{ | |
for (int i = 0; i < onDisable.Length; i++) | |
{ | |
FireEvent(onDisable[i]); | |
} | |
} | |
public void OnInvoke() | |
{ | |
for (int i = 0; i < onInvoke.Length; i++) | |
{ | |
FireEvent(onInvoke[i]); | |
} | |
} | |
void FireEvent(PlatformEvent platformEvent) | |
{ | |
if (platformEvent.platform == Platform.All) | |
{ | |
platformEvent.unityEvent.Invoke(); | |
} | |
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN | |
if (platformEvent.platform == Platform.Windows) | |
{ | |
platformEvent.unityEvent.Invoke(); | |
} | |
#endif | |
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX | |
if (platformEvent.platform == Platform.Mac) | |
{ | |
platformEvent.unityEvent.Invoke(); | |
} | |
#endif | |
#if STEAM | |
if (platformEvent.platform == Platform.Steam) | |
{ | |
platformEvent.unityEvent.Invoke(); | |
} | |
#endif | |
#if DEMO | |
if (platformEvent.platform == Platform.Demo) | |
{ | |
platformEvent.unityEvent.Invoke(); | |
} | |
#endif | |
if (Debug.isDebugBuild) | |
{ | |
if (platformEvent.platform == Platform.Debug) | |
{ | |
platformEvent.unityEvent.Invoke(); | |
} | |
} | |
} | |
} | |
[System.Serializable] | |
public struct PlatformEvent | |
{ | |
public Platform platform; | |
public UnityEvent unityEvent; | |
} | |
public enum Platform | |
{ | |
All, | |
Windows, | |
Mac, | |
Steam, | |
Demo, | |
Debug | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment