Skip to content

Instantly share code, notes, and snippets.

View beardordie's full-sized avatar

Beard or Die beardordie

View GitHub Profile
@beardordie
beardordie / ButtonAttribute.cs
Created February 16, 2019 07:22
Unity - minimalist Button inspector drawer for zero-parameter methods
using System;
using UnityEngine;
#if UNITY_EDITOR
using System.Linq;
using System.Reflection;
using UnityEditor;
#endif
public class ButtonAttribute : Attribute
{
@beardordie
beardordie / ExampleScriptableObject.cs
Created January 28, 2019 01:43
Click-to-assign a name field from a Scriptable Object's filename (requires Odin Inspector)
[InlineButton("AutoName", "Auto")]
public new string name;
public void AutoName()
{
#if UNITY_EDITOR
string assetPath = UnityEditor.AssetDatabase.GetAssetPath(GetInstanceID());
name = System.IO.Path.GetFileNameWithoutExtension(assetPath);
#endif
}
@beardordie
beardordie / ConvertColorToRGB.cs
Last active January 16, 2019 23:06
Unity - Code Snippet to convert a Color value to an RGB hex value
// this is not a full class, just a code snippet showing the relevant method to convert a Color object
// to an RGB hex value so it can be used in a text field
public Color playerColor;
public Text playerTextField;
playerTextField.text = "<color=#" + ColorUtility.ToHtmlStringRGB(playerColor) + ">PLAYER</color>";
// put this line in EVERY .cs file that gets that annoying error about a variable always having default value
#pragma warning disable 0649
@beardordie
beardordie / SimulateGravity.cs
Created August 18, 2018 19:34
Unity Editor-only helper script to simulate gravity for placing objects on ground with physics
#if UNITY_EDITOR
using System.Collections;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class SimulateGravity : MonoBehaviour
{
public bool simulate = false;
public bool remove = false;
@beardordie
beardordie / CollisionAudio.cs
Last active August 30, 2018 03:45
Play audio clip based on a rigidbody's collision force, with pitch variance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
[RequireComponent(typeof(Collider))]
public class CollisionAudio : MonoBehaviour
{
[SerializeField] private AudioClip collideSoft;
[SerializeField] private AudioClip collideHard;
@beardordie
beardordie / MinMaxRangeAttribute.cs
Created August 18, 2018 19:20
Custom attribute to enforce Min and Max float values - used by RangedFloatDrawer
using System;
public class MinMaxRangeAttribute : Attribute
{
public MinMaxRangeAttribute(float min, float max)
{
Min = min;
Max = max;
}
@beardordie
beardordie / RangedFloat.cs
Created August 18, 2018 19:16
RangedFloat type with Custom Editor
using System;
[Serializable]
public struct RangedFloat
{
public float minValue;
public float maxValue;
public RangedFloat(float minValue, float maxValue) //constructor
{
@beardordie
beardordie / SimpleAudioEvent.cs
Last active June 11, 2020 18:53
Simple Audio Event - supports multiple clips per event, pitch and volume variance - with Custom Editor
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Audio;
/// <summary>
/// This is a template to create a new Simple Audio Event.
/// It supports an array of audio clips, pitch and volume variance,
/// and a custom editor that shows a button to preview a random sound
/// from the array with the selected pitch and volume variance.
@beardordie
beardordie / HowTo-UnityUICamera-AlwaysOnTop.txt
Created August 18, 2018 03:54
Unity UI Camera - Always On Top - Setup Instructions
If you would like to have some UI elements in world space, but always appear on "top" (never obscured by other world objects), here is the setup:
- Create a new camera as a child of your main camera, this is your UI camera
- Zero out the new camera's transform position, and match its FOV with the parent camera
- Set the Layer of this camera to UI
- Set Clear Flags to Depth only
- Set Culling mask to none then to only the UI layer
- On your main camera, set Culling mask to omit the UI layer
After doing this, any canvas or UI elements on that canvas set to the UI layer will only use the UI camera to render, and will not be hidden by intersecting world objects. i.e. they will be Always On Top