https://github.com/gilzoide/unity-enum-bitset
Fast and memory efficient implementation of C#'s ISet for enums, storing data as bit masks
https://github.com/gilzoide/unity-enum-bitset
bitmask enums enumset package unity unity3d upm-package
Last synced: 3 months ago
JSON representation
Fast and memory efficient implementation of C#'s ISet for enums, storing data as bit masks
- Host: GitHub
- URL: https://github.com/gilzoide/unity-enum-bitset
- Owner: gilzoide
- License: unlicense
- Created: 2022-02-13T01:36:06.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-06-21T12:33:41.000Z (over 2 years ago)
- Last Synced: 2025-01-31T03:55:02.660Z (12 months ago)
- Topics: bitmask, enums, enumset, package, unity, unity3d, upm-package
- Language: C#
- Homepage:
- Size: 120 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# EnumBitSet
[](https://openupm.com/packages/com.gilzoide.enum-bitset/)
Fast and memory efficient implementation of C#'s `ISet` for enums, storing data as bit masks:
- `EnumBitSet32`: uses `EnumBitMask32` as data, supporting enums with up to 32 values.
- `EnumBitSet64`: uses `EnumBitMask64` as data, supporting enums with up to 64 values.
Bit masks are readonly structs implementing `IReadOnlySet` for enums:
- `EnumBitMask32`: uses `int` as data, supporting enums with up to 32 values.
- `EnumBitMask64`: uses `long` as data, supporting enums with up to 64 values.
All implementations support enums both with and without `[Flags]` attributes.
Conversions between enum values and integer types are non-boxing where possible by using unsafe utilities from Unity 2018+, .NET 5+ or .NET Core 3.0+
## Installing the package
This package is available on the [openupm registry](https://openupm.com/) and can be installed using the [openupm-cli](https://github.com/openupm/openupm-cli):
```
openupm add com.gilzoide.enum-bitset
```
Otherwise, you can install directly using the [Unity Package Manager](https://docs.unity3d.com/Manual/Packages.html) with the following URL:
```
https://github.com/gilzoide/unity-enum-bitset.git#1.1.0
```
## Unity 2020+ Serialization and Property Drawer
In Unity 2020+, `EnumBitSet`s are serializable directly as generic classes.
There's also a custom property drawer for selecting the containing enums:
```cs
using System;
using UnityEngine;
using Gilzoide.EnumBitSet;
public enum TestEnum
{
Zero, One, Two, Three
}
[Flags]
public enum TestEnumFlags
{
Zero = 1 << 0, One = 1 << 1, Two = 1 << 2, Three = 1 << 3
}
public class ScriptWithBitSet : MonoBehaviour
{
public EnumBitSet32 aBitset;
public EnumBitSet64 anotherBitset;
}
```

## Unity 2019- Serialization and Property Drawer
In Unity 2019 and earlier, create non-generic classes that
inherit from the generic ones:
```cs
using System;
using UnityEngine;
using Gilzoide.EnumBitSet;
public enum TestEnum
{
Zero, One, Two, Three
}
[Flags]
public enum TestEnumFlags
{
Zero = 1 << 0, One = 1 << 1, Two = 1 << 2, Three = 1 << 3
}
[Serializable]
public class TestEnumBitSet32 : EnumBitSet32 {}
[Serializable]
public class TestEnumFlagsBitSet64 : EnumBitSet64 {}
public class ScriptWithBitSet : MonoBehaviour
{
public EnumBitSet32 aBitset;
public EnumBitSet64 anotherBitset;
}
```
For the custom Property Drawer, the same applies.
Create a class that inherits from `EnumBitSetPropertyDrawer`:
```cs
using UnityEditor;
using Gilzoide.EnumBitSet.Editor;
[CustomPropertyDrawer(typeof(TestEnumBitSet32))]
public class TestEnumBitSet32PropertyDrawer : EnumBitSetPropertyDrawer {}
```