Skip to content

Instantly share code, notes, and snippets.

View snlehton's full-sized avatar

Sampsa Lehtonen snlehton

View GitHub Profile
@snlehton
snlehton / UIFollow.cs
Created December 19, 2017 12:08
A simple Unity example script that makes a UI component track position of world space object rendered with specific world space camera. Canvas needs to be in Screen Space Camera mode.
// A simple Unity example script that makes a UI component track position of world space object rendered with
// specific world space camera. Canvas needs to be in Screen Space Camera mode.
[ExecuteInEditMode]
public class UIFollow : MonoBehaviour
{
[Tooltip("World space object to follow")]
public GameObject target;
[Tooltip("World space camera that renders the target")]
public Camera worldCamera;
[Tooltip("Canvas set in Screen Space Camera mode")]
@snlehton
snlehton / ResoScaler.cs
Created October 16, 2016 18:57
A simple Unity component to render the screen in lower resolution
using UnityEngine;
// Render the screen in lower resolution
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ResoScaler : MonoBehaviour
{
[Range(1, 8)]
public float scale = 1;
@snlehton
snlehton / UnityEditorCheatSheet.cs
Created January 22, 2020 10:19
Various random Unity editor snippets
// Create Dropdown button
if (EditorGUI.DropdownButton(position, GUIContent.none, FocusType.Passive))
{
var genericMenu = new GenericMenu();
// List<string> options
foreach (var option in options)
{
bool selected = option == property.stringValue;
genericMenu.AddItem(new GUIContent(options), selected, () =>
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
/*
Simple EventManager class to implement one-to-many event passing. Features:
* Simple enum and payload events.
* Delayed event sending (send later in the frame in one place).
* Detects adding duplicates/removing non-existent listeners in Unity editor mode.
using UnityEngine;
using UnityEngine.Assertions;
// Barebones Singleton class to be used with Unity
// Supports also Singleton instances coming from prefabs (PrefabSingleton).
// Prefabs need to reside in Resources folder and named [ComponentName].asset.
// If you have GameManager class, then you need Resources/GameManager.asset
public class Singleton<T> : MonoBehaviour where T:MonoBehaviour
{
@snlehton
snlehton / RandomUtils.cs
Last active February 8, 2018 09:52
My RandomUtils for Unity
using UnityEngine;
/* Random Utils By Sampsa Lehtonen, sampsa.lehtonen(AT)iki.fi, @snlehton
Some of my Random utils created for procedural generation and deterministic randomness.
Uses Linear Feedback Shift Register (LFSR) for quick and dirty random.
Lacks Perlin noise... need to reimplement the one in Unity in case they go and change the implementation.
CAUTION: I haven't verified the distribution of any of these functions, so use at your own risk. The aim has been to have
deterministic Unity-implementation-free random functions with features that Unity hasn't offered.
using UnityEngine;
public class SpawnTest : MonoBehaviour
{
// a prefab containing SpawnTestPrefab
public GameObject prefab;
void Start()
{
Instantiate(prefab, new Vector3(100, 0, 0), Quaternion.identity);
using System.Collections.Generic;
public static class ListExtensions
{
public static T GetRandomWeighted<T>(this List<T> list, Func<T, float> selector)
{
T selected = default(T);
float totalWeight = 0;
foreach (var item in list)
{
@snlehton
snlehton / BetterButton.cs
Created December 7, 2016 17:17
"Better" version of the Unity UI.Button which prevents the button from becoming selected upon mouse click
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class BetterButton : Button
{
public override void OnSelect(BaseEventData eventData)
{
// prevent the button on becoming selected via mouse click
if (eventData is PointerEventData)
{
public static class RemoveEmptyDirectories
{
[MenuItem("Tools/EditorHacks/Remove empty directories...")]
public static void MenuItem_RemoveEmptyDirectories()
{
RemoveEmptyDirectoriesRecursive("Assets/");
}
private static void RemoveEmptyDirectoriesRecursive(string path)
{