Skip to content

Instantly share code, notes, and snippets.

View tomkail's full-sized avatar

Tom Kail tomkail

View GitHub Profile
@tomkail
tomkail / AbsoluteRectTransformController.cs
Last active April 15, 2024 14:57
Sets the rect of the attached RectTransform to a viewport space rect, regardless of the pivot, anchors, or (optionally) scale of the RectTransform.
using UnityEngine;
// Sets the rect of the attached RectTransform to a viewport space rect, regardless of the pivot, anchors, or (optionally) scale of the RectTransform.
[ExecuteAlways]
[RequireComponent(typeof(RectTransform))]
public class AbsoluteRectTransformController : MonoBehaviour {
public Rect viewportRect = new Rect(0,0,1,1);
public bool ignoreScale;
static Vector3[] corners = new Vector3[4];
DrivenRectTransformTracker drivenRectTransformTracker;
@tomkail
tomkail / BetterPropertyField.cs
Last active April 15, 2024 14:07
Unity editor helper that draws a serialized property (including children) fully, even if it's an instance of a custom serializable class.
using UnityEngine;
using UnityEditor;
using System.Collections;
public static class BetterPropertyField {
/// <summary>
/// Draws a serialized property (including children) fully, even if it's an instance of a custom serializable class.
/// Supersedes EditorGUILayout.PropertyField(serializedProperty, true);
/// </summary>
/// <param name="_serializedProperty">Serialized property.</param>
@tomkail
tomkail / ExtendedScriptableObjectDrawer.cs
Last active April 2, 2024 18:56
Displays the fields of a ScriptableObject in the inspector
// Developed by Tom Kail at Inkle
// Released under the MIT Licence as held at https://opensource.org/licenses/MIT
// Must be placed within a folder named "Editor"
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
@tomkail
tomkail / RiveUIRenderer.cs
Last active February 12, 2024 11:04
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 UnityEngine;
using UnityEngine.Rendering;
using Rive;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[ExecuteAlways]
@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 / InputFieldActivationRestorer.cs
Created January 5, 2024 13:07
Workaround for Unity's inability to keep an input field selected when you click a button
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.
// This class also tracks the state of the last selected input field in Update, allowing easier restoration of the input field when it becomes deselected.
// Common use cases are to:
@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 / CreateEditorScriptMenuItem.cs
Last active July 13, 2023 12:39
Adds "Create > Editor Script" menu to scripts a right click that automatically creates editor scripts.
using System;
using System.Globalization;
using System.Text;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
public static class CreateEditorScriptMenuItem {
@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 / 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: