Skip to content

Instantly share code, notes, and snippets.

View luke161's full-sized avatar

Luke luke161

View GitHub Profile
@luke161
luke161 / AddressablesSceneLoader.cs
Last active March 29, 2024 12:02
Handles loading and unloading of Scenes using Unity's Addressable Assets system. Adds support for using LoadSceneParameters when loading a scene, which is currently missing in the official APIs.
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
@luke161
luke161 / UISafeArea.cs
Last active October 11, 2021 05:41
Unity UI component that modifies a RectTransform so it fits within Screen.SafeArea. Useful for adapting Unity UI for the iPhone X. Works by setting the edge offset of the RectTransform, the RectTransform should be setup to stretch to all corners of it's parent.
/**
* UISafeArea.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
namespace UnityEngine.UI
{
[RequireComponent(typeof(RectTransform))]
public class UISafeArea : MonoBehaviour
@luke161
luke161 / AutoIncrementBuildNumber.cs
Created November 7, 2017 15:38
Unity editor extension to increment iOS build number after each build.
using UnityEditor;
using UnityEditor.Callbacks;
public class AutoIncrementBuildNumber
{
[PostProcessBuild]
public static void OnBuildComplete(BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget==BuildTarget.iOS) {
PlayerSettings.iOS.buildNumber = (int.Parse (PlayerSettings.iOS.buildNumber) + 1).ToString();
@luke161
luke161 / UIDAttribute.cs
Created October 13, 2017 09:34
Unity Editor extension to generate unique identifiers for strings. Shows a 'Generate' button next to a string input field in the inspector, for any string property where a [UID] attribute is assigned. For example: [UID] public string id;
using UnityEngine;
// Example usage:
// [UID] public string id;
public class UIDAttribute : PropertyAttribute
{
}
@luke161
luke161 / TreeNode.cs
Created May 11, 2017 10:10
Simple tree data structure for use with C# and Unity. Start with a root TreeNode and add children as required.
/**
* TreeNode.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
using System;
using System.Collections.Generic;
public class TreeNode<T>
{
@luke161
luke161 / DownloadHandlerFile.cs
Last active June 27, 2024 02:53
Custom DownloadHandler for UnityWebRequest to stream contents into a file. Useful for preloading AssetBundles without them automatically being uncompressed and loaded into memory.
/**
* DownloadHandlerFile.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
public class DownloadHandlerFile : DownloadHandlerScript
@luke161
luke161 / TriangleWave.cs
Created December 26, 2016 20:46
Function for generating a triangle wave between -1 and 1 in Unity C#
public static float TriangleWave(float x)
{
return UnityEngine.Mathf.Abs((x%4)-2)-1;
}
@luke161
luke161 / Singleton.cs
Last active June 20, 2024 15:20
Unity MonoBehaviour Singleton. Base class for creating a singleton in Unity, handles searching for an existing instance, useful if you've created the instance inside a scene. Also handles cleaning up of multiple singleton instances in Awake.
/**
* Singleton.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
@luke161
luke161 / VolumeUtils.cs
Last active October 14, 2016 15:52
Util class for converting between a linear volume (0..1) and decibel volume in Unity.
/**
* VolumeUtils.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
using UnityEngine;
public static class VolumeUtils
{