Skip to content

Instantly share code, notes, and snippets.

@yasirkula
yasirkula / ScreenshotCapture.cs
Last active March 27, 2020 07:16
Quickly capture a screenshot in Unity
using System;
using System.IO;
using UnityEngine;
public static class ScreenshotCapture
{
// Saves the screenshot to desktop
public static void Capture()
{
string saveDirectory = Environment.GetFolderPath( Environment.SpecialFolder.DesktopDirectory );
@yasirkula
yasirkula / OpenShaderInNotepad.cs
Created August 3, 2019 13:14
An editor script for Unity 3D to open shaders in Notepad++ or the default text editor
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public class OpenShaderInNotepad
{
private const string NPP1 = @"C:\Program Files\Notepad++\notepad++.exe";
private const string NPP2 = @"C:\Program Files (x86)\Notepad++\notepad++.exe";
@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 / EditorCameraZoomWithScrollWheel.cs
Last active September 21, 2019 16:42
"Right mouse button+Scroll wheel" will continue to zoom in/zoom out the Scene View instead of changing the camera speed on Unity 2019+
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class EditorCameraZoomWithScrollWheel
{
private const float CAMERA_SPEED = -0.25f;
private static bool rmbDown = false;
@yasirkula
yasirkula / CameraFollow.cs
Created September 21, 2019 16:40
A camera follow script with numerous parameters for Unity
using UnityEngine;
[ExecuteInEditMode]
public class CameraFollow : MonoBehaviour
{
[Tooltip( "Object to follow" )]
public Transform Target;
[Tooltip( "Target distance to the followed object" )]
public Vector3 DistanceToTarget = new Vector3( 0f, 3f, -5f );
@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 / 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 / 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 / UIToggler.cs
Last active January 11, 2020 09:14
Quickly toggle the visibility of UI layer (canvases) in Unity's Scene view
using UnityEditor;
using UnityEngine;
public class UIToggler
{
private const int UI_LAYER = 1 << 5;
[InitializeOnLoadMethod]
private static void Init()
{
@yasirkula
yasirkula / DirectoryComparer.cs
Last active January 26, 2021 09:22
Compare the contents of two directories and apply any changes to the target directory to synchronize these two directories
#define COMPARE_FILE_CONTENTS
#if COMPARE_FILE_CONTENTS
#define USE_THREADS
#endif
using System;
using System.Collections.Generic;
using System.IO;
#if USE_THREADS
using System.Threading;