Skip to content

Instantly share code, notes, and snippets.

@yasirkula
yasirkula / BetterWaitForSeconds.cs
Created October 13, 2019 15:54
WaitForSeconds manager that reuses instances to avoid GC in Unity
using System.Collections.Generic;
using UnityEngine;
public static class BetterWaitForSeconds
{
private class WaitForSeconds : CustomYieldInstruction
{
private float waitUntil;
public override bool keepWaiting
{
@yasirkula
yasirkula / HierarchyFolderObject.cs
Last active November 12, 2023 10:19
Create folder objects in Hierarchy that automatically detach all their children while building the game (for Unity3D)
#define ENABLE_LOGGING // Logs the folder objects that were flattened to the console
//#define SIMULATE_BUILD_BEHAVIOUR_ON_PLAY_MODE // Simulates Execution.AtBuildTime when entering Play Mode in the Editor, as well
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.Reflection;
#endif
@yasirkula
yasirkula / SingleColorTextureDetector.cs
Last active November 15, 2023 20:12
Find Textures with only a single color in a Unity project
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class SingleColorTextureDetector : EditorWindow, IHasCustomMenu
{
@yasirkula
yasirkula / PaddingIgnoringImage.cs
Last active January 2, 2024 18:46
Modified version of Image component that ignores the sprite's padding in Unity
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.UI;
#if UNITY_2017_4 || UNITY_2018_2_OR_NEWER
using UnityEngine.U2D;
#endif
#if UNITY_EDITOR
using UnityEditor;
@yasirkula
yasirkula / CustomMaxSizeSetter.cs
Created March 13, 2022 08:27
Set non-power-of-2 Max Size values for textures/sprites in Unity
using UnityEditor;
using UnityEngine;
public class CustomMaxSizeSetter : EditorWindow
{
private const int MINIMUM_MAX_SIZE = 32;
private const int MAXIMUM_MAX_SIZE = 2048;
private int initialMaxSize = -1;
private int currentMaxSize = -1;
@yasirkula
yasirkula / TMP_IntegerText.cs
Last active January 15, 2024 08:12
Show numbers (int, float etc.) on TextMesh Pro texts without any GC allocations
//#define TMP_ROUND_DECIMALS // When defined, float and double values are rounded using Math.round
using TMPro;
public static class TMP_IntegerText
{
private static readonly char[] arr = new char[64]; // prefix + number + postfix can't exceed this capacity!
private static int charIndex = 0;
public static void SetText( this TMP_Text text, sbyte number )
@yasirkula
yasirkula / CustomRectHandles.cs
Created August 7, 2021 15:15
Drawing Rect handles in Unity (similar to built-in Rect tool)
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
public class CustomRectHandles : ScriptableObject
{
public class Rect3D
{
@yasirkula
yasirkula / EmptyParentCreator.cs
Last active February 13, 2024 05:55
Group the selected objects under a new parent object in Hierarchy (Unity 3D)
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class EmptyParentCreator
{
[MenuItem( "GameObject/Create Empty Parent", priority = 0 )]
private static void CreateEmptyParent( MenuCommand command )
{
// This happens when this button is clicked via hierarchy's right click context menu
@yasirkula
yasirkula / FileDownloader.cs
Last active February 26, 2024 05:52
C# Download Public File From Google Drive™ (works for large files as well)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;
/* EXAMPLE USAGE
FileDownloader fileDownloader = new FileDownloader();
@yasirkula
yasirkula / BenchmarkEnterPlayMode.cs
Last active February 27, 2024 11:27
Calculate the time it takes to enter/exit play mode in Unity editor
using UnityEditor;
using UnityEngine;
public class BenchmarkEnterPlayMode
{
[InitializeOnLoadMethod]
private static void Initialize()
{
void OnPlayModeStateChanged( PlayModeStateChange state )
{