Skip to content

Instantly share code, notes, and snippets.

View tomkail's full-sized avatar

Tom Kail tomkail

View GitHub Profile
@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 / LoadableSprite.cs
Last active August 9, 2022 07:48
Loads a Sprite, instantly or asynchronously, using Resources.
// Example usage -
// LoadableSprite loadableSprite = new LoadableSprite ("texture");
// loadableSprite.LoadAsync (loadedSprite => {
// Do stuff on load here!
// })
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// You'll need to put this in your scene somewhere so the LoadableSprite class can use coroutines.
@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 / DefaultAssetEditor.cs
Last active September 16, 2022 15:02
Draws inspectors for any file type Unity doesn't draw by default
/// Used to draw custom inspectors for unrecognised file types, which Unity imports as "DefaultAsset"
/// To do this, create a new editor class extending DefaultAssetInspector
/// Return true in the IsValid function if the file extension of the file matches the type you'd like to draw.
/// The DefaultAssetEditor class will then hold a reference to the new instance of your editor class and call the appropriate methods for drawing.
/// An example can be found at the bottom of the file.
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
@tomkail
tomkail / OnOpenUnityEditorExample.cs
Created May 4, 2018 10:03
Example of how to get a callback that only runs when Unity opens (not on script recompile)
[InitializeOnLoad]
public static class OnOpenUnityEditorExample {
const string lastCompileTimeKey = "InkIntegrationLastCompileTime";
static InkEditorUtils () {
float lastCompileTime = LoadAndSaveLastCompileTime();
if(EditorApplication.timeSinceStartup < lastCompileTime)
OnOpenUnityEditor();
}
static void OnOpenUnityEditor () {
@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 / MultitouchDraggable.cs
Last active July 26, 2022 21:49
Multitouch UI Draggable
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
// Handles moving, scaling and rotating UI objects using multitouch.
// Should be just about 1-1, as you'd expect on a touch screen, although because it applies deltas there's a bit of "slippage" if you manipulate the same object for a while/rapidly.
// To test multitouch with a mouse editor you can comment out the marked line in OnEndDrag.
public class MultitouchDraggable : Selectable, IBeginDragHandler, IEndDragHandler, IDragHandler {
@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 / 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) {
@tomkail
tomkail / Better TextMeshPro GetRenderedValues
Created September 7, 2022 09:02
Better GetRenderedValues for TextMeshPro that doesn't return NaN/other invalid values and allows you to test with custom text
// 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 textMeshPro, string text, float maxWidth = Mathf.Infinity, float maxHeight = Mathf.Infinity, bool onlyVisibleCharacters = true) {
if(string.IsNullOrEmpty(text)) return Vector2.zero;
var originalRenderMode = textMeshPro.renderMode;
var originalText = textMeshPro.text;
var originalDeltaSize = textMeshPro.rectTransform.sizeDelta;