Skip to content

Instantly share code, notes, and snippets.

@Rovsau
Rovsau / Voltorb.cs
Last active April 7, 2023 23:02
Make scripts delete themselves.
public class Voltorb
{
// Uncomment at your own risk.
//[UnityEditor.InitializeOnLoadMethod]
private static void SelfDestruct([System.Runtime.CompilerServices.CallerFilePath] string path = "")
{
path = System.IO.Path.GetRelativePath(System.IO.Directory.GetCurrentDirectory(), path);
//UnityEditor.AssetDatabase.DeleteAsset(path);
}
}
@Rovsau
Rovsau / ProjectInitializer.cs
Created April 7, 2023 22:54
Automatically set Project Root Namespace
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Rovsau.Unity.Editor.RunOnce
{
// Sets a root namespace for the project,
// then self destructs.
// Output:
@Rovsau
Rovsau / GoogleSheetAPIKeyReader.cs
Last active April 10, 2023 11:31
Read data from a public Google Sheet with an API Key (instead of OAuth2)
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using System.Collections.Generic;
using UnityEngine;
namespace Rovsau.Unity.Google
{
public static class GoogleSheetAPIKeyReader
{
@Rovsau
Rovsau / GoogleSheetsOAuth2.cs
Last active April 10, 2023 11:32
Read and Write data to a private Google Sheet with an OAuth2 (not async)
using System.IO;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace Rovsau.Unity.Google
@Rovsau
Rovsau / GoogleSheetsOAuth2Async.cs
Last active April 10, 2023 11:32
Read and Write data to a private Google Sheet with an OAuth2 (Async)
using System.IO;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Threading.Tasks;
@Rovsau
Rovsau / DebugColors.cs
Last active April 14, 2023 01:55
Debug logs with any range of color.
public static class DebugColors
{
public static void Log(Color color, string message, UnityEngine.Object context = default)
{
UnityEngine.Debug.Log($"<color={ColorToHex(color)}>{message}</color>", context);
}
public static string ColorToHex(Color32 color)
{
return $"#{color.r:X2}{color.g:X2}{color.b:X2}";
@Rovsau
Rovsau / GoogleSheetsNamedFunctionsExtractor.cs
Last active April 17, 2023 00:22
Extract Named Functions (and Ranges) from all Google Spreadsheets accessible via the credentials.
using System;
using System.IO;
using System.Xml.Linq;
using System.Threading;
using System.IO.Compression;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using System.Collections.Generic;
@Rovsau
Rovsau / ColliderRadar.md
Created April 16, 2023 19:45
One-script-solution Radar for Rigidbody and 3D Colliders, with LayerMask

The Collider Radar detects all 3D Colliders included in the LayerMask.

  • Script is ideally placed on a child object of the player.
  • The player is required to have a Rigidbody for the physics events OnTriggerEnter/Exit.
  • A Radar UI object, background and icon prefab must be set up for this to work. (not provided)

This example does not include pooling, and simply Creates/Destroys icons as colliders enter and exit the trigger.

using System.Collections.Generic;
using UnityEngine;
@Rovsau
Rovsau / ComponentRadar.md
Created April 16, 2023 19:53
Radar which tracks any world object with a custom Component

ComponentRadar tracks all objects with a RadarComponent.

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Rovsau.Unity.ComponentRadar
{
    // ComponentRadar tracks all objects with a RadarComponent.
@Rovsau
Rovsau / RaycastDetection.md
Created April 16, 2023 23:02
Raycast with multiple detection ranges

Uses a PostFixedUpdate solution with Coroutine. (generates garbage)
Which solves the problem of physics events executing before FixedUpdate,
which would result in a 1-frame delay of detection,
as well as a 1-frame lingering when the target has gone out of range.

The coroutine is probably best replaced by injecting the method into the Player loop. (not provided)

The events are mostly there as an example. They probably need to be redesigned or replaced.