No, I’m not implying that what you’re doing is necessarily wrong, just stating what you’re doing out loud. Apart from that, there is also very little benefit to be had from casting to uint specifically, because enums are already implicitly of type int.
Of course, there is no implicit conversion, however you might do this with an extension if you’d like your code to be more readable. You can do for example
public enum AudioID {
Step = 0,
Fall = 1,
Fly = 2
}
static public class AudioIDExtensions {
static public void AudioClip Get(this IList<AudioClip> clips, AudioID id)
=> clips[(int)id];
}
And now you can
public AudioClip[] vr = new AudioClip[3];
vr.Get(AudioID.Fall);
But this still doesn’t address your inspector problem.
I can’t really understand what you’re trying to do here, because normally you either use an enum, or you manually set the values in the inspector. Either of these things serve as a ground truth, but it seems that you want one to copy over to the other. And this makes zero sense to me. Where do you connect the actual audio file?
If you start from enum, you have to have an audio file hooked up somewhere.
If you start from inspector serialization, then you can’t build an enum out of that. I mean you can, but you don’t have any access to it outside of runtime, and you wish to program with it!
What you really want is a way to simply refer to inspector data by something other than pure integers.
And this is where comment from DevDunk above fits in.
You make a struct based on key/value pairs, where the key is some name, and the value is the clip.
using UnityEngine;
[System.Serializable]
public struct NamedAudioClip {
[SerializeField] public string name;
[SerializeField] public AudioClip clip;
}
You then expose an array of this type
using UnityEngine;
public class MyScript : MonoBehaviour {
[SerializeField] NamedAudioClip[] namedClips;
}
After you set these fields up manually, you introduce an auto-registration of names in Awake of this script, to be able to access the entries without having to search for them
void Awake() {
autoRegisterClipNames()
}
static Dictionary<string, NamedAudioClip> _reg;
void autoRegisterClipNames() {
_reg = new Dictionary<string, NamedAudioClip>();
foreach(var item in namedClips) {
_reg[item.name] = item;
}
}
Now you can reach for any item in O(1) by naming it
var myClip = _reg["Fall"];
But this is prone to errors by mistyping.
Finally, add your enum to match these names as well. The actual values (and order) of these enumerations are irrelevant, which is excellent. The actual names are important!
public enum AudioID {
Step,
Fall,
Fly
}
static public class AudioIDExtensions {
static public void AudioClip GetClip(this IDictionary<string, NamedAudioClip> namedClips, AudioID id)
=> namedClips[id.ToString()].clip;
}
Now you can
var myClip = _reg.GetClip(AudioID.Fall);
Make sure the letter casing is the same in inspector and enum. Also do not use duplicate names in the inspector, because this will only expose one of those names. If you want to make sure that a duplicate is caught early, change this
_reg[item.name] = item;
to this
_reg.Add(item.name, item); // will throw exception on duplicate key
Edit: fixed typos in the last extension