Skip to content

Instantly share code, notes, and snippets.

View tomkail's full-sized avatar

Tom Kail tomkail

View GitHub Profile
@tomkail
tomkail / BeginDragImmediately.cs
Created October 23, 2024 13:33
Immediately begins a drag as soon as a mouse/finger down event occurs on a Unity UI object.
using UnityEngine;
using UnityEngine.EventSystems;
public class BeginDragImmediately : MonoBehavior, IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler {
public void OnInitializePotentialDrag(PointerEventData eventData) {
eventData.pointerDrag = gameObject;
eventData.dragging = true;
eventData.useDragThreshold = false;
ExecuteEvents.ExecuteHierarchy(transform.gameObject, eventData, ExecuteEvents.beginDragHandler);
}
@tomkail
tomkail / RiveUIRenderer.cs
Last active September 27, 2024 02:28
Renders a Rive asset to Unity UI
// RiveUIRenderer.cs created by Tom Kail. This file is licensed under the MIT License.
// Renders a Rive asset to Unity UI using a RenderTexture in a similar manner to RawImage.
// Supports pointer events and masking.
using Rive;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.UI;
@tomkail
tomkail / InputFieldActivationRestorer.cs
Last active August 27, 2024 15:19
Workaround for Unity's inability to keep an input field selected when you click a button
// Released under MIT License
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
// Workaround for Unity's inability to keep an input field selected when you click a button.
// Call ReactivateInputField to immediately activate and restore caret position/selection.
@tomkail
tomkail / PresetImportPerFolder.cs
Created September 25, 2023 14:45
Automatically applies Preset files to assets in the same directory. Also adds labels present on the Preset file to those assets.
using System.IO;
using UnityEditor;
using UnityEditor.Presets;
using System.Linq;
// This script automatically applies Presets to assets when they are imported into a folder containing a Preset.
// It also adds any labels on the Preset file to the asset.
public class PresetImportPerFolder : AssetPostprocessor {
void OnPreprocessAsset() {
// Make sure we are applying presets the first time an asset is imported.
@tomkail
tomkail / GetLocalToAnchoredPositionOffset.cs
Created December 20, 2022 14:47
Converts between localPosition and anchoredPosition for a RectTransform.
// Add this to convert a local position to an anchored position
public static Vector2 GetLocalToAnchoredPositionOffset(this RectTransform rectTransform) {
var parentRT = (RectTransform) rectTransform.parent;
var pivotAnchor = new Vector2(Mathf.LerpUnclamped(rectTransform.anchorMin.x, rectTransform.anchorMax.x, rectTransform.pivot.x), Mathf.LerpUnclamped(rectTransform.anchorMin.y, rectTransform.anchorMax.y, rectTransform.pivot.y));
return -parentRT.rect.size * (pivotAnchor - parentRT.pivot);
}
// Add this to convert a local position to an anchored position
public static Vector2 GetAnchoredToLocalPositionOffset(this RectTransform rectTransform) {
return -rectTransform.GetLocalToAnchoredPositionOffset();
@tomkail
tomkail / Trim Ends.txt
Created November 3, 2022 13:10
Audacity macro to trim 5 seconds from the start and end of a track
Select:End="5" RelativeTo="ProjectStart" Start="0" Track="0" TrackCount="1"
Delete:
Select:End="5" RelativeTo="ProjectEnd" Start="0" Track="0" TrackCount="1"
Delete:
@tomkail
tomkail / Seamless Loop.txt
Last active November 3, 2022 16:33
Audacity macro to create seamless loops from audio tracks
Select:End="100000" RelativeTo="Project" Start="40" Track="0" TrackCount="1"
Delete:
Select:End="2" RelativeTo="ProjectEnd" Start="0"
Copy:
SelectNone:
Paste:
SelectTracks:Mode="Set" Track="0" TrackCount="2"
CrossfadeTracks:curve="0" direction="Automatic" type="ConstantGain"
MixAndRender:
SelectNone:
@tomkail
tomkail / InkParserUtility.cs
Last active January 21, 2024 14:11
Utility for parsing lines of ink into instructions that can be executed on the game side.
// Usage example:
// using System.Collections.Generic;
// using System.Text.RegularExpressions;
// public enum AudioCategory {
// Undefined,
// Music,
// Narration,
// }
@tomkail
tomkail / Better TextMeshPro GetRenderedValues
Last active June 14, 2024 10:48
Better GetRenderedValues for TextMeshPro that doesn't return NaN/other invalid values and allows you to test with custom text
// Applying float.MaxValue to a rectTransform can cause crashes (not sure why) so we just use a very big number instead.
const float veryLargeNumber = 10000000;
// Gets the tightest bounds for the text by updating the text and using GetRenderedValues
// Note this uses sizeDelta for sizing so won't work when using anchors.
// This is wayyyy more reliable than the actual GetRenderedValues because it won't return stupid values, as GetRenderedValues is prone to doing.
public static Vector2 GetRenderedValues (this TMP_Text textComponent, string text, float maxWidth = veryLargeNumber, float maxHeight = veryLargeNumber, bool onlyVisibleCharacters = true) {
if(string.IsNullOrEmpty(text)) return Vector2.zero;
// Setting RT size to Infinity can lead to weird results, so we use a very large number instead.
if(maxWidth > veryLargeNumber) maxWidth = veryLargeNumber;
@tomkail
tomkail / ColorDescriptor.cs
Last active September 14, 2022 17:29
Describes a color in english. Crude implementation, but handy for scanning colors when shown in text fields.
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public static class ColorDescriptor {
public static string GetNameForColor (Color color) {
var bestDist = float.MaxValue;
string best = null;
foreach(var colorName in colorNames) {