Skip to content

Instantly share code, notes, and snippets.

@viruseg
viruseg / .Manually Convert World to Screen Position in Unity with Burst.cs
Last active December 19, 2023 02:20
Manually Convert World to Screen Position in Unity with Burst. The code is intended for the Burst compiler, which does the same thing as Camera.WorldToScreenPoint.
//camera-like projection matrix
var cameraData = new CameraDataForBurst(camera);
//or
//projection matrix for the rendering target of a given size
var cameraData = new CameraDataForBurst(camera, 320, 240);
var worldPoint = new float3(10, 10, 0);
var screenPointWithZ = cameraData.WorldToScreenPointWithZ(worldPoint);
var screenPoint = cameraData.WorldToScreenPoint(worldPoint);
@viruseg
viruseg / .Encrypting & Decrypting (file, string, byte array) in C#, NET 8.0 and Unity.cs
Last active April 3, 2024 19:22
Encrypting & Decrypting (file, string, byte array) in C#, NET 8.0 and Unity
//The code is based on the code from here: https://stackoverflow.com/a/10177020/1221983
//And adapted for NET 8 and Unity.
//Reduced memory consumption.
//Added possibility to encrypt byte arrays, not only strings.
//You can also attach custom data that will not be encrypted.
var password = "nso%dhfkl$siohf";
var str = "This is test string!№;%^#@^&";
@viruseg
viruseg / .Interaction in unity with Gradient via jobs + burst.cs
Last active December 19, 2023 02:18
Interaction in unity with Gradient via jobs + burst
Interaction in unity with Gradient via jobs + burst
var gradient = new Gradient();
//Access Gradient.colorKeys and Gradient.alphaKey via NativeArray without a Garbage Collector:
//Single threaded mode:
var gradientPtr = gradient.DirectAccess();
using var colorKeys = gradientPtr->GetColorKeys(Allocator.Temp);
using var alphaKeys = gradientPtr->GetAlphaKeys(Allocator.Temp);