Skip to content

Instantly share code, notes, and snippets.

@xepherys
xepherys / Cast From Object to Generic
Created February 2, 2023 14:59
Cast from an object to the target type of a generic in a generic Class<T>.
using System;
public class Program
{
public static void Main()
{
int myInt = 5;
TestValue<float> testValue = new TestValue<float>();
@xepherys
xepherys / UnityVisualElement.cs
Created December 12, 2022 14:18
Unity VisualElement CustomStyleProperties and Callbacks
class MyElement : VisualElement
{
static readonly CustomStyleProperty<int> k_CellSizeProperty = new CustomStyleProperty<int>("--cell-size");
static readonly CustomStyleProperty<Color> k_OddCellColorProperty = new CustomStyleProperty<Color>("--odd-cell-color");
static readonly CustomStyleProperty<Color> k_EvenCellColorProperty = new CustomStyleProperty<Color>("--even-cell-color");
int m_CellSize;
Color m_OddCellColor;
Color m_EvenCellColor;
@xepherys
xepherys / EditPrefab.cs
Created November 8, 2021 12:31 — forked from ulrikdamm/EditPrefab.cs
Unity editor script for better editing of prefabs. Put in Assets/Editor.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
// Adds a "Edit Prefab" option in the Assets menu (or right clicking an asset in the project browser).
// This opens an empty scene with your prefab where you can edit it.
// Put this script in your project as Assets/Editor/EditPrefab.cs
public class EditPrefab {
static Object getPrefab(Object selection) {
@xepherys
xepherys / EditPrefabInScene.cs
Created October 11, 2021 14:08 — forked from JohannesDeml/EditPrefabInScene.cs
Unity editor script for better editing of prefabs. Put in Assets/Editor.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
// From https://gist.github.com/JohannesDeml/5802473b569718c9c86de906b7039aec
// Original https://gist.github.com/ulrikdamm/338392c3b0900de225ec6dd10864cab4
// Adds a "Edit Prefab" option in the Assets menu (or right clicking an asset in the project browser).
// This opens an empty scene with your prefab where you can edit it.
// Put this script in your project as Assets/Editor/EditPrefab.cs
@xepherys
xepherys / ReflectiveEnumerator.cs
Created September 6, 2021 17:38
ReflectiveEnumerator - Enumerate classes inheriting from a specified abstract class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
public static class ReflectiveEnumerator
{
/// <summary>
/// Original class from StackExchange post
/// https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class/6944605#6944605
@xepherys
xepherys / RollingList.cs
Created September 29, 2020 16:12
RollingList Type that will move the indexer back to 0 when the end of the list is reached.
public class RollingList<T> : List<T>
{
private int indexer = 0;
private int maxIndexer;
public RollingList() : base()
{
maxIndexer = this.Count;
}
@xepherys
xepherys / Xeph.Windows.Forms.ExtendedControls.cs
Last active February 20, 2020 23:13
Extended Windows Forms Controls for Visual Studio
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Windows.Forms;
namespace Xeph.Windows.Forms.ExtendedControls
@xepherys
xepherys / MainCameraEllipticalMWE.cs
Created November 14, 2019 01:28
Unity camera script for tracking around an elliptical or circular path
using System;
using UnityEngine;
/// <summary>
/// The MainCameraEllipticalMWE class provides motion of the camera in an ellipse (or a circle)
/// given sizeX and sizeZ where one is the major axis and the other the minor axis. Which is
/// which isn't important, and they can be equal (x == z) if the camera is to track in a circle.
///
/// The size values assume a 2D surface where x=0, z=0 is at the bottom left and built in both x+
/// and z+ directions.
@xepherys
xepherys / RotateFlagEnum.cs
Last active November 9, 2019 13:33
Rotating values of a flagged System.Enum in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
// Requires C# 7.3 or later (Roslyn 3.0 or later) for where TEnum : System.Enum
public class Program
{
public static void Main()
{
@xepherys
xepherys / BitEnum.cs
Created November 5, 2019 01:33
BitFlag Enum
[System.Flags]
public enum LaserDirection
{
NONE = 0,
NORTH = 1 << 0,
NORTHEAST = 1 << 1,
EAST = 1 << 2,
SOUTHEAST = 1 << 3,
SOUTH = 1 << 4,