Skip to content

Instantly share code, notes, and snippets.

@forcepusher
Last active May 14, 2023 13:41
Show Gist options
  • Save forcepusher/7663ebfde8805c4a478a4907abbc7cd5 to your computer and use it in GitHub Desktop.
Save forcepusher/7663ebfde8805c4a478a4907abbc7cd5 to your computer and use it in GitHub Desktop.
Unity "void Main" OOP template
using System.Diagnostics;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BananaParty.FurryGame.Client
{
/// <summary>
/// Entry point for wiring up the engine and executing main loop.
/// </summary>
public static class Program
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
public static async void Main()
{
// Prevent Unity Test Framework from shitting itself.
#if UNITY_EDITOR
if (SceneManager.GetActiveScene().name.StartsWith("InitTestScene"))
return;
#elif UNITY_INCLUDE_TESTS
return;
#endif
// Start editor playmode from scene 0.
#if UNITY_EDITOR
AsyncOperation sceneLoadingOperation = SceneManager.LoadSceneAsync(0);
while (!sceneLoadingOperation.isDone)
await Task.Yield();
#endif
// Output Trace events, but don't duplicate them in a web export.
#if UNITY_EDITOR || !UNITY_WEBGL
Trace.Listeners.Add(new UnityConsoleTraceListener
{
Filter = new EventTypeFilter(SourceLevels.Information)
});
#endif
bool quitting = false;
Application.quitting += () => { quitting = true; };
Stopwatch stopwatch = new();
stopwatch.Start();
using (var clientApplication = new ClientApplication(
new UnityRenderLibrary(),
new UnitySceneFactory(),
new UnityUserInterfaceLibrary()))
{
while (!quitting)
{
clientApplication.ExecuteFrame(stopwatch.ElapsedMilliseconds);
await Task.Yield();
}
}
}
}
}
@forcepusher
Copy link
Author

forcepusher commented Feb 15, 2022

Scenes are no longer the root of your project, nor the game code depends on Unity Engine.
Ripped directly out of personal project without modifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment