Skip to content

Instantly share code, notes, and snippets.

@Quickz
Quickz / SerializedObjectUtility.cs
Created May 8, 2021 22:48
[Unity] Gets the target value of a property. In other words, if you have a serialized class and you want to get it's instance from the property, use this method.
using UnityEditor;
public static class SerializedObjectUtility
{
public static object GetTargetValue(this SerializedProperty property)
{
var targetObject = property.serializedObject.targetObject;
var field = targetObject.GetType().GetField(property.propertyPath);
return field?.GetValue(targetObject);
@Quickz
Quickz / SingletonInstance
Created January 7, 2021 17:58
Unity singleton example
public class SingletonInstance : MonoBehaviour
{
public static SingletonInstance Instance { get; private set; }
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
return;
@Quickz
Quickz / CameraEffect.cs
Created January 7, 2021 09:24
Low health warning effect script example for Unity.
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class CameraEffect : MonoBehaviour
{
public Material material;
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
@Quickz
Quickz / ExampleScriptableObject.cs
Created December 20, 2020 18:08
Scriptable Object instance icon
using UnityEngine;
[CreateAssetMenu(fileName = "New Example", menuName = "Example")]
public class ExampleScriptableObject : ScriptableObject
{
public Sprite Icon => icon;
[SerializeField]
private Sprite icon = null;
}
@Quickz
Quickz / screenshoter.py
Created October 17, 2020 14:55
Script that makes a screen shot every 5 seconds using a Windows feature
# "pip install pynput" to install pynput
from pynput.keyboard import Key, Controller
from time import time, sleep
def take_screenshot():
keyboard = Controller()
# Windows key + Print screen
with keyboard.pressed(Key.cmd_r):
keyboard.press(Key.print_screen)
@Quickz
Quickz / EventSystem.ts
Last active July 18, 2020 22:38
C# like event pattern for TypeScript
export type EventHandler<T> = (event: T) => void;
export interface IEvent<T>
{
add(handler: EventHandler<T>): void;
remove(handler: EventHandler<T>): void;
}
export class InvokeableEvent<T> implements IEvent<T>
{
@Quickz
Quickz / async loop with delay (sleep)
Created June 30, 2020 14:44
Asynchronous loop with a delay between iterations
doStuff();
async function doStuff()
{
while (true)
{
console.log("Hello, world!");
await sleep(500);
}
}
@Quickz
Quickz / RotationUtility.cs
Created April 13, 2020 20:26
A simple utility class for limiting rotation in Unity.
public static class RotationUtility
{
/// <param name="rotation">
/// Value of 0f to 360f.
/// Rotation that will be limitted to a certain angle.
/// </param>
public static float LimitRotation(
float rotation,
float minRotation,
float maxRotation)
@Quickz
Quickz / CameraUtility.cs
Created April 12, 2020 11:53
A simple pattern for catching Camera.main in Unity.
using UnityEngine;
public static class CameraUtility
{
public static Camera MainCamera
{
get
{
if (_mainCamera == null)
{
@Quickz
Quickz / VariableChangedEventPattern.cs
Created March 3, 2020 18:50
A generic variable wrapper that allows detecting value changes.
using System;
namespace VariableChangedEventPattern
{
class Program
{
private static void Main()
{
Game.Score.Changed += OnScoreChanged;
Game.Score.Value += 200;