Skip to content

Instantly share code, notes, and snippets.

View markeahogan's full-sized avatar

Mark E A Hogan markeahogan

View GitHub Profile
@markeahogan
markeahogan / AudioSourceExtensions.cs
Created February 27, 2024 16:44
Adds extension methods for Unity's AudioSource class for volume and rolloff calculation based on distance. The CalculateVolumeAtDistance method computes the volume of the audio source at a given distance. CalculateRolloffMultiplier calculates the rolloff factor based on the distance and the AudioSource's rolloff mode.
public static class AudioSourceExtensions
{
public static float CalculateVolumeAtDistance(this AudioSource source, float distance, float volumeRolloffScale = 1)
{
var rolloff = CalculateRolloffMultipler(source, distance, volumeRolloffScale);
return source.volume * Mathf.Lerp(1, rolloff, source.spatialBlend);
}
public static float CalculateRolloffMultipler(this AudioSource source, float distance, float volumeRolloffScale = 1)
{
@markeahogan
markeahogan / Rectangle.cs
Created August 29, 2023 16:25
A class for drawing rounded rectangles in UGUI, aiming for CSS parity
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace PopupAsylum.UIEffects
{
/// <summary>
/// Aiming for feature parity with CSS box styling
/// </summary>
@markeahogan
markeahogan / InspectorCapture.cs
Created August 3, 2023 07:02
Adds method to save the whole inspector to a screenshot
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
/// <summary>
/// Provides functionality for screenshotting full editor windows, and the inspector in particular
@markeahogan
markeahogan / IMGUIRectExtensions.cs
Last active May 30, 2022 13:43
Utility functions for IMGUI Rects, useful for drawing PropertyFields in a single line without lots of Rect boilerplate
using UnityEngine;
namespace PopupAsylum
{
/// <summary>
/// Utility functions for IMGUI Rects, useful for drawing PropertyFields in a single line
/// Works by returning the Rect to draw the contol and filling the out argument with a Rect for the remaining space
/// By passing the same Rect to the out member it can keep eating chunks of the property's Rect
///
/// void ExampleDrawer(SerializedProperty property, Rect rect)
using UnityEngine;
public static class BoundsExtensions
{
/// <summary>
/// Modifies the bounds center position so that it's min and max are within the container
/// It doesnt modify the bounds size, if bounds are larger then the container it will be centered
/// </summary>
public static void MoveInside(ref this Bounds bounds, Bounds container)
{
@markeahogan
markeahogan / Exposure.cs
Created January 30, 2017 21:56
Set a global exposure shader variable based on light probes and raycasts to lights
using UnityEngine;
using System.Collections;
using UnityEngine.Rendering;
[ExecuteInEditMode]
public class Exposure : MonoBehaviour {
const float DEFAULT_EXPOSURE = 3f;
const string EXPOSURE_PROPERTY = "_PA_Exposure";