Skip to content

Instantly share code, notes, and snippets.

@kraj0t
kraj0t / EditorComponentUtils.cs
Created April 28, 2022 08:11
[Unity] Retrieve a component's fileId and its gameObject's asset guid (if any)
/// <summary>Universal method for retrieving the fileId of a component inside a saved gameObject, and the guid to the file that contains the gameObject.
/// The gameObject could live in a prefab or in a scene, and the component does not need to be at the root gameObject of the prefab.</summary>
/// <param name="comp"></param>
/// <returns>A tuple containing the guid and the fileId of the component. Returns null if the component's gameObject has not been saved to a scene or a prefab.</returns>
public static Tuple<string, long> GetComponentIdentifiers(Component comp)
{
// Check if the component is the direct instance from the prefab.
// In the Editor, this means that the prefab asset is selected and the component is showing in the Inspector.
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(comp, out string guid, out long fileId))
{
@kraj0t
kraj0t / .Raycast inside RenderTexture.gif
Last active March 3, 2024 10:50
[Unity] PhysicsRaycaster for indirectly casting rays inside a quad that has a RenderTexture on it
.Raycast inside RenderTexture.gif
@kraj0t
kraj0t / ffmpeg_batchAudiosToVideos.bat
Created May 1, 2022 18:46
ffmpeg - Windows batch file - convert m4a audios to videos
@echo off
if not exist *.m4a (
echo This directory contains no m4a files.
) else (
for %%f in (*.m4a) do (
:: Option 1. LOOPING VIDEO
ffmpeg -i "%%f" -stream_loop -1 -i "loop-360p.mp4" -shortest "%%~nf.mp4"
:: Option 2. STATIC BLACK IMAGE
@kraj0t
kraj0t / MeshDataCache.cs
Created May 9, 2022 21:26
[Unity] MeshDataCache - collect mesh data and modify it before applying the changes to the Mesh. Compatible with VertexHelper
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
namespace kraj0t.Rendering
{
/// <summary>
/// This class is a convenient way of collecting mesh data and avoiding having to constantly query and dirty a Unity mesh, which is BAD for performance.
///
@kraj0t
kraj0t / CloseEditorWindowShortcut.cs
Last active March 3, 2024 17:21
[Unity] Editor shortcut for closing the window/tab that is currently focused (I just cannot live without it heheh)
using UnityEditor;
using UnityEditor.ShortcutManagement;
using UnityEngine;
public class CloseEditorWindowShortcut
{
[Shortcut("Window/Close", KeyCode.W, ShortcutModifiers.Control)]
private static void CloseTab(ShortcutArguments args)
{
if (EditorWindow.focusedWindow != null)
@kraj0t
kraj0t / UsingCsCodeInHLSL.cs
Last active January 5, 2025 14:34
How to include C# code in an HLSL file - useful for sharing code between CPU and shader without duplicating any files
// Share code between HLSL (shaders) and C#
//
// Limitations:
// - Cannot use #include
// - Must use defines to hide certain keywords, such as public, private, protected, or any other object-oriented programming keywords
// - Use defines to convert half or fixed to your desired C# equivalent
// - Must always write f after float literals
// - Use #if !MY_DEFINE instead of #ifndef MY_DEFINE
// - #define cannot be used with a value in C#, not even inside an '#if SHADER_TARGET' block. Therefore, you have two options for declaring valued constants:
// a. Declare each constant separately in HLSL (using 'static const float MyConstant = 1.0f') and in C# (using 'const float MyConstant = 1.0f'). C# does not support 'static const'.
@kraj0t
kraj0t / ClampedCurveAttribute.cs
Last active March 13, 2024 10:21
Clamped AnimationCurve GUI - constrains the curve to a range, typically 0-1
using System;
using UnityEngine;
/// <summary>Constrains an AnimationCurve to a specific range. Use the default constructor for the typical 0-1 range. Use Mathf.Infinity to unconstrain specific values.</summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class ClampedCurveAttribute : PropertyAttribute
{
public Rect ranges;
public ClampedCurveAttribute()
@kraj0t
kraj0t / InternalClassReflectionExtendedEditor.cs
Last active April 24, 2024 10:50
Extend or modify built-in private Editor classes that are not exposed in the API, such as MeshRendererInspector or TransformInspector
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
/// <summary><para>Use this class to override a built-in Editor that is not publicly exposed in the API.</para>
///
@kraj0t
kraj0t / DebugShaderGlobalsProfile.cs
Last active March 21, 2025 21:07
Asset that I like to use for quick debugging of shader values
using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
/// <summary>
/// Convenience asset for testing different global shader values in the editor.
/// </summary>
[CreateAssetMenu(fileName = "DebugShaderGlobalsProfile", menuName = "__AURELIO/DebugShaderGlobalsProfile")]
public class DebugShaderGlobalsProfile : ScriptableObject
@kraj0t
kraj0t / Shader notes tips and tricks.md
Last active March 30, 2025 22:57
Shader notes, tips and tricks

Reconstruct faceted normal from position

normalWS = normalize(cross(ddy(IN.positionWS), ddx(IN.positionWS)));

_ScreenParams vs _ScreenSize

Unity documentation is wrong when it explains _ScreenParams, and _ScreenSize is not documented. _ScreenParams contains the actual screen size, while _ScreenSize contains the size of the camera’s target texture. This is important because the camera's texture (the buffer) can vary with Render Scale. Also, note that the .z and .w components are calculated differently.