Skip to content

Instantly share code, notes, and snippets.

@thebne
thebne / ExpandConvexHull.cs
Last active June 30, 2020 15:03
Expand fill of convex polygon - Unity (C#)
// this is an adaptation to Unity of the algorithm described
//. in https://stackoverflow.com/questions/3749678/expand-fill-of-convex-polygon/3897471
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
static class ConvexHull
{
public static Vector2[] ExpandConvexHull(Vector2[] poly, float distance)
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class GuardianJsonRecorder : MonoBehaviour
{
void Update()
{
@thebne
thebne / BezierCurve5Points.cs
Created January 14, 2021 09:51
Unity Bezier 5 points
void DrawBezierCurve(Vector3 point0, Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4)
{
lineRenderer.positionCount = 50;
float t = 0f;
Vector3 B;
for (int i = 0; i < lineRenderer.positionCount; i++)
{
B = 1 * (1 - t) * (1 - t) * (1 - t) * (1 - t) * point0
@thebne
thebne / PointOnBounds.cs
Last active January 21, 2021 09:33
Get closest point on bounds (not within)
Vector3 GetClosestPointOnBounds(Bounds bounds, Vector3 point)
{
var boundPoint1 = bounds.min;
var boundPoint2 = bounds.max;
var points = new Vector3[] {
new Vector3(boundPoint1.x, boundPoint1.y),
new Vector3(boundPoint1.x, boundPoint2.y),
new Vector3(boundPoint2.x, boundPoint2.y),
new Vector3(boundPoint2.x, boundPoint1.y),
@thebne
thebne / UnityDebug.sh
Created March 14, 2021 13:49
Unity debug commands
# use USB to forward profiling on Android / Quest
adb forward tcp:54999 localabstract:Unity-{insert bundle identifier here}
@thebne
thebne / find_unused_references.py
Created July 7, 2021 17:33
Find unused Script references in Unity - using meta GUIDs + naïve Regular Expressions
import os, sys
import unityparser
import re
def get_files(folder_path):
return [x for f in os.walk(folder_path) for x in map(lambda x: f"{f[0]}{os.path.sep}{x}", f[2])]
scripts = {d.data[0]['guid']: d for d in [unityparser.UnityDocument.load_yaml(path) for path in get_files(os.path.join('Assets', 'Scripts')) if path.endswith(".meta")]}
assets_paths = [path for path in get_files('Assets') if path.endswith('.unity') or path.endswith('.prefab') or path.endswith('.asset')]
@thebne
thebne / UnityEditorGetObjectFromSerializedProperty.cs
Last active October 26, 2021 16:07
Get C# object (or Type) from SerializedProperty - useful for ObjectField type constraint
/// <see cref="https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBaseEditor/EditorHelper.cs"/>
public static Type GetTargetTypeOfProperty(SerializedProperty prop)
{
if (prop == null) return null;
var path = prop.propertyPath.Replace(".Array.data[", "[");
path = path.Substring(0, path.LastIndexOf('.'));
object obj = prop.serializedObject.targetObject;
var elements = path.Split('.');
foreach (var element in elements)
@thebne
thebne / unity_rename_managed_reference.py
Created November 11, 2021 13:31
Rename Unity's managed reference (ones created by [SerializeReference] attribute)
import os
import sys
DEFAULT_ASSEMBLY = "Assembly-CSharp"
DEFAULT_NS = ""
def main():
if len(sys.argv) < 3:
print(f"Usage: {sys.argv[0]} <old assembly:?><old ns:?><old name> <new assembly:?><new ns:?><new name>")
print()
@thebne
thebne / color_hex_to_unity_color.py
Last active November 14, 2021 09:25
Convert HTML color (#ffff00 or rgb(...) or rgba(...)) to Unity new Color() format
def h2f(h):
h = h.lstrip('#')
return tuple(int(h[i:i+2], 16) / 255 for i in (0, 2, 4))
def r2f(r):
r = r.lstrip("rgb").lstrip("a").lstrip("(").rstrip(")").strip()
return [float(x.strip()) / (255 if i < 3 else 1) for i, x in enumerate(r.split(", "))]
def c2u(s):
f = r2f(s) if "rgb" in s else h2f(s)
@thebne
thebne / RegisterPropertyChangeCallback_BeforeUnity2020.cs
Created January 4, 2022 13:11
PropertyField.RegisterValueChangeCallback but before 2020
// see https://forum.unity.com/threads/uielements-developer-guide.648043/#post-6073137
void RegisterPropertyChangeCallback(PropertyField field, SerializedProperty property, FieldInfo fieldInfo, Action callback)
{
var propertyType = property.propertyType;
switch (propertyType)
{
case SerializedPropertyType.Integer:
case SerializedPropertyType.LayerMask:
case SerializedPropertyType.ArraySize:
field.RegisterCallback<ChangeEvent<int>>( (evt) => callback());