Skip to content

Instantly share code, notes, and snippets.

View njelly's full-sized avatar

Nathaniel Ellingson njelly

View GitHub Profile
@njelly
njelly / PrefabController.md
Last active December 1, 2024 05:50
PrefabController: how to grok scenes and prefabs in Unity

All Unity projects must solve the problem of initializing instantiated prefabs. PrefabController is my attempt to demonstrate a standard solution to this problem in Unity. When a prefab is instantiated, OnEnterScene should be called immediately to inject necessary scene data, such as cameras, lighting info, and other dependencies. All prefab controllers ought to be unit tested, and OnEnterScene provides a clear interface to pass initialization data within a test scenario.

Scenes and prefabs are similar concepts in Unity. Both are modular assets intended to be reused. Both contain a tree structure of GameObjects. But despite having completely separate creation workflows in the Unity editor, the key difference between the two is subtle and rarely alluded to: scenes are self contained singletons, while prefabs are not. Prefabs cannot exist in their own, they are always instantiated into a scene, and PrefabController only attempts to formalize this relationship.

@njelly
njelly / ShapeMath2D.cs
Last active July 29, 2024 10:35
ShapeMath2D - A collection of static functions for performing common (and some uncommon!) geometry tests in 2D.
// MIT License
//
// Copyright (c) 2022 Nathaniel Ellingson
// https://njellingson.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@njelly
njelly / Google Sheet to JSON Web App
Last active January 4, 2021 15:38
Google Sheet to JSON Web App
///////////////////////////////////////////////////////////////////////////////
//
// Google Sheets to JSON Web App by Nathaniel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
@njelly
njelly / Vector2IntQuadTree.cs
Last active August 4, 2020 04:07
A quadtree implementation using Unity's Vector2Int.
///////////////////////////////////////////////////////////////////////////////
//
// Vector2IntQuadTree by Tofunaut
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: