Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Thundernerd/Unity3D-EnumAsIndex
An attribute that allows you to use an enum as an index for arrays and lists
https://github.com/Thundernerd/Unity3D-EnumAsIndex
Last synced: 3 months ago
JSON representation
An attribute that allows you to use an enum as an index for arrays and lists
- Host: GitHub
- URL: https://github.com/Thundernerd/Unity3D-EnumAsIndex
- Owner: Thundernerd
- License: mit
- Created: 2020-07-13T20:40:21.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-08T21:06:34.000Z (almost 4 years ago)
- Last Synced: 2024-04-24T16:42:10.030Z (7 months ago)
- Language: C#
- Homepage:
- Size: 42 KB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Enum As Index
An attribute that allows you to use an enum as an index for arrays and lists.
## Installation
1. The package is available on the [openupm registry](https://openupm.com). You can install it via [openupm-cli](https://github.com/openupm/openupm-cli).
```
openupm add net.tnrd.enumasindex
```
2. Installing through a [Unity Package](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.enumasindex?registry=https://package.openupm.com) created by the [Package Installer Creator](https://package-installer.glitch.me) from [Needle](https://needle.tools)[](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.enumasindex?registry=https://package.openupm.com)
## Usage
Usage is easy, let's assume we have the following enum:
```c#
public enum MyEnum
{
Hello,
World,
Goodbye,
Foo,
Bar
}
```We can apply the EnumAsIndex attribute easily to both arrays and lists as shown below, using typeof with the enum we want to use as an index.
In the Awake method you can access the value in the list by converting the enum value to an integer to serve as an index.
```c#
public class Dummy : MonoBehaviour
{
[SerializeField, EnumAsIndex(typeof(MyEnum))] private List fooList;
[SerializeField, EnumAsIndex(typeof(MyEnum))] private int[] barArray;private void Awake()
{
var item = fooList[(int) MyEnum.Goodbye];
}
}
```The result in the editor will be as below. Note that you **need to change to size manually once** for it to work. Afterwards it'll automatically update the size.
![in-editor example](./~Documentation/dummy.png)
You cannot modify the value of the dropdown boxes. They are decorative and only serve to show you what index the value is for.
## Support
**Enum As Index** is a small and open-source utility that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating.[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/J3J11GEYY)
## Contributions
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.