Skip to content

Instantly share code, notes, and snippets.

@chrisyarbrough
chrisyarbrough / ICommand.cs
Created December 1, 2025 23:49
CommandLine ICommand
using CommandLine;
using CommandLine.Text;
using System.Reflection;
/// <summary>
/// A CLI command of the application, like <i>clone</i> in <i>git clone</i>.
/// Corresponds to a verb in the CommandLineParser.
/// </summary>
/// <remarks>
/// Derived types are created by the CommandLineParser and their Run method is called when parsing was successful.
using System.Collections;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Will allocate.
IList list = new List<int>();
foreach (var item in list)
@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 / VirtualDelegateInvocation
Created February 12, 2021 17:38
Be careful when refactoring lambdas with method groups. The method group may seem cleaner but it also breaks virtual methods by only calling the base implementation.
using System;
using DG.Tweening;
using Nementic.Exit;
using UnityEngine;
public class VirtualDelegateInvocation : MonoBehaviour
{
private Child p = new Child();
[EditorButton()]
@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 July 28, 2025 02:22
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()
{