Skip to content

Instantly share code, notes, and snippets.

View RiskyWilhelm's full-sized avatar
😃

Yunus Yıldız RiskyWilhelm

😃
View GitHub Profile
@RiskyWilhelm
RiskyWilhelm / VectorRangeAttribute.cs
Last active July 9, 2024 17:01
Unity Vector Range/Limiter Attribute (C#>=9)
using UnityEngine;
/// <summary> Customizes the field shown in inspector and clamps the values as <see cref="RangeAttribute"/> does </summary>
public sealed partial class VectorRangeAttribute : PropertyAttribute
{
public readonly (float minX, float maxX, float minY, float maxY, float minZ, float maxZ, float minW, float maxW) clamped;
public VectorRangeAttribute
(
@RiskyWilhelm
RiskyWilhelm / IEnumeratorExtensions.cs
Created June 30, 2024 13:20
Avoid IEnumerable.GetEnumerator() garbage and iterate through an enumerator directly (C# >= 9)
using System.Collections;
public static class IEnumeratorExtensions
{
/// <summary> Allows you to iterate through an IEnumerator </summary>
public static T GetEnumerator<T>(this T enumerator)t
where T : IEnumerator
{
return enumerator;
}
@RiskyWilhelm
RiskyWilhelm / RandomExtensions.cs
Last active September 16, 2024 21:41
Unity Simple Timer/TimerRandomized (C# >= 9)
using System;
public static class RandomExtensions
{
public static float NextFloat(this Random random, float minInclusiveValue, float maxExclusiveValue)
=> (float)random.NextDouble(minInclusiveValue, maxExclusiveValue);
public static double NextDouble(this Random random, double minInclusiveValue, double maxExclusiveValue)
{
return random.NextDouble() * (maxExclusiveValue - minInclusiveValue) + minInclusiveValue;
@RiskyWilhelm
RiskyWilhelm / EnumExtensions.cs
Last active June 18, 2024 15:12
Enum HasAny() Extension (C# >= 9) Highly optimized
using System;
public static class EnumExtensions
{
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary>
public static bool HasAny<EnumType>(this EnumType a, EnumType b)
where EnumType : Enum
{
// Check for the supported enum types
return a.GetTypeCode() switch