Skip to content

Instantly share code, notes, and snippets.

@ryanmillerca
Last active July 28, 2020 13:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmillerca/2dc5f1449623d2a3704ec309170d4db8 to your computer and use it in GitHub Desktop.
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)
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