Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created July 22, 2023 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtdekker/98227547b84506edbbfd9d7d2ae33543 to your computer and use it in GitHub Desktop.
Save kurtdekker/98227547b84506edbbfd9d7d2ae33543 to your computer and use it in GitHub Desktop.
Make a Unity project / build with zero scenes, just pure code.
using UnityEngine;
// @kurtdekker - for the purists who don't want ANY scenes in project
//
// All other Unity asset inclusion rules are still in effect, so no
// assets will make it into your project unless they are:
//
// - correctly in a Resources/ folder
// - referenced by something in the above
// - correctly in the Streaming Assets folder / system.
// - correctly bundled in Asset Bundles or Addressables
public class RunmeJohnny : MonoBehaviour
{
// This is where it all begins. You will be called here the
// moment the Unity Engine is up and running and ready for you.
[RuntimeInitializeOnLoadMethod]
static void StartWithNoScenes()
{
Camera cam = new GameObject("Camera").AddComponent<Camera>();
cam.transform.position = new Vector3(0, 3, -10);
cam.orthographic = false;
cam.fieldOfView = 60.0f;
// NOTE: the material on this cube will NOT be included
// in the build because... you didn't reference it anywhere.
//
// You will need to get a material on this cube or
// else it will be hot pink. See comment at top.
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
// inject an instance of this script to get things started
var runme = cube.AddComponent<RunmeJohnny>();
runme.cube = cube;
}
// injected by the above (superfluous here, since it is ourselves,
// but provided as a way to inject other scripts with with stuff)
GameObject cube;
private void Update()
{
float angle = Time.time * 100.0f;
cube.transform.rotation = Quaternion.Euler(0, angle, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment