Skip to content

Instantly share code, notes, and snippets.

View snlehton's full-sized avatar

Sampsa Lehtonen snlehton

View GitHub Profile
@snlehton
snlehton / SOExample.cs
Last active August 29, 2015 14:18
ScriptableObject example
using UnityEngine;
using System.Collections;
public class SOExample : ScriptableObject
{
public string text;
}
@snlehton
snlehton / SOExampleBehaviour.cs
Last active August 29, 2015 14:18
SOExample behaviour
using UnityEngine;
using System.Collections;
public class SOExampleBehaviour : MonoBehaviour
{
public SOExample test;
public void OnEnable()
{
// instantiate if needed
if (test == null)
test = ScriptableObject.CreateInstance<SOExample>();
@snlehton
snlehton / SOExample scene
Created April 2, 2015 14:18
SOExample scene
....
--- !u!114 &819407913 <<--- this is our ScriptableObject id in the scene
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4a48e9db79a952f419ca79d71a03fe31, type: 3}
@snlehton
snlehton / SOExampleBehaviour2.cs
Created April 2, 2015 14:41
SOExampleBehaviour v2
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class SOExampleBehaviour : MonoBehaviour
{
public SOExample test;
public void OnEnable()
{
// find existing instance if needed
if (test == null)
public static class RemoveEmptyDirectories
{
[MenuItem("Tools/EditorHacks/Remove empty directories...")]
public static void MenuItem_RemoveEmptyDirectories()
{
RemoveEmptyDirectoriesRecursive("Assets/");
}
private static void RemoveEmptyDirectoriesRecursive(string path)
{
@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)
{
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)
{
using UnityEngine;
public class SpawnTest : MonoBehaviour
{
// a prefab containing SpawnTestPrefab
public GameObject prefab;
void Start()
{
Instantiate(prefab, new Vector3(100, 0, 0), Quaternion.identity);
@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;
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
{