Skip to content

Instantly share code, notes, and snippets.

@chrisyarbrough
chrisyarbrough / open-unity.py
Created April 24, 2023 13:42
A python script that opens a Unity project from within multiple supported locations (project, frontend, assets).
#!/usr/bin/env python3
import os
import sys
import re
import subprocess
from pathlib import Path
def find_version(project_settings_path):
@chrisyarbrough
chrisyarbrough / CustomCollectionInitializer.cs
Created February 13, 2021 16:45
Demonstrates how to implement a custom collection initializer by implement the IEnumerable<T> interface and the Add method.
private void OnPresetSelected(object userdata)
{
// We want to quickly define some string collections as "presets".
foreach (var preset in presetsOld)
{
foreach (var prop in preset)
Debug.Log(prop);
}
// But it's much nicer if each collection has a name.
@chrisyarbrough
chrisyarbrough / WizardFactory.cs
Created February 13, 2021 16:14
Demonstrates how not to use C# a collection initializer to assign fields within the object initializer.
/// <summary>
/// Creates beings imbued with magical powers.
/// </summary>
public class WizardFactory
{
public Wizard Create()
{
// Just imagine this as part of some editor interface
// or default values that are hardcoded for any reason.
return new Wizard
@chrisyarbrough
chrisyarbrough / ExportBuiltinUISprites
Last active April 22, 2024 14:10
Demonstrates how to copy and import Unity's standard UI sprites into a project. This can be useful if the builtin sprites need to be modified or placed onto an atlas.
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public static class ExportBuiltinUISprites
{
private const string outputDirectory = "Assets/Graphics/";
@chrisyarbrough
chrisyarbrough / CircleMath.cs
Created February 11, 2021 21:59
Given are two angles start and end within the range 0..360, now rotate a point around a circle from start to end taking either the clockwise or counter-clockwise direction.
using System.Collections;
using UnityEditor;
using UnityEngine;
public class CircleMath : MonoBehaviour
{
public Vector3 position = Vector3.right;
public float angleStart = 20;
public float angleEnd = 270;
public float duration = 1f;
@chrisyarbrough
chrisyarbrough / CustomPreviewExample.cs
Last active March 24, 2025 10:27
This shows how UnityEditor.PreviewRenderUtility can be used to draw a custom scene and render it interactively in an editor window. The target object can be modified while running, but it is important to note, that in this case we need to managed target instantiation and destruction ourselves.
using UnityEditor;
using UnityEngine;
/// <summary>
/// Demonstrates how to use <see cref="UnityEditor.PreviewRenderUtility"/>
/// to render a small interactive scene in a custom editor window.
/// </summary>
public class CustomPreviewExample : EditorWindow
{
[MenuItem("My Tools/Custom Preview")]
@chrisyarbrough
chrisyarbrough / ImageGenerator.cs
Created February 26, 2020 12:54
ScriptableWizard to create random noise images in Unity
#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEngine;
public class ImageGenerator : ScriptableWizard
{
[MenuItem("Nementic/Utility/Image Generator...")]
public static void CreateWizard()
{