Enum Flags In Unity

  Рет қаралды 4,433

Warped Imagination

2 жыл бұрын

Do you want to select many options on an enum for a component in Unity?
Well this is the video for you.
🏪 Check out my new Unity Asset Store page
assetstore.unity.com/publishers/78356?aid=1101liVpX
If you would like to support this channel, feel free to donate to:
www.ko-fi.com/warpedimagination
Enum Extensions Video
kzbin.info/www/bejne/hXTOg6J-n9SYf5Y
Asset Links
Skeleton Mage Asset
assetstore.unity.com/packages/3d/characters/humanoids/fantasy/skeleton-mage-wizard-210606?aid=1101liVpX
Documentation Links
Flags Attribute
learn.microsoft.com/en-us/dotnet/api/system.flagsattribute?view=net-7.0
#unity #unity3d #unitytutorial #madewithunity #gamesdevelopment
FTC Disclosure: Some links provided are linked to my affiliate account & support the channel at no extra cost to you.

Пікірлер: 44
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Let me know what extension methods you would create for the magic source enum .. maybe something to do with Bunnies! Enum Extensions video can be found here kzbin.info/www/bejne/hXTOg6J-n9SYf5Y
@jaxowaraxa
@jaxowaraxa 2 жыл бұрын
You can use bit shift operation for numbering you Enum, so you don't need to keep math in your head 1
@WarpedImagination
@WarpedImagination 2 жыл бұрын
love it
@GameDevNerd
@GameDevNerd 2 жыл бұрын
I have some helpful, additional advice on this you guys will probably like a lot, haha, I love bit flag/mask types and use them extensively in C/C++ and C# for all kinds of stuff, so I've got some little tricks of my own ... First time/effort-saver trick, rather than specify the underlying numeric values as a constant literal in decimal form, there's a better way (and an additional/tertiary way that's a little more work but super handy in a very specific situation) ... what we do is instead of 1, 2, 4, 8, 16, 32, and so on (gets really big and becomes a hassle if there's many constants) we can do this: [Flags] public enum BetterFlags: uint { None = 0x00, // 0 First = 0x01, // 1 Second = 1
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Great stuff
@DaylightDev
@DaylightDev Жыл бұрын
I'm just amazed by how much useful information you have on your channel. And yes, these videos are getting way less attention than they should and I have no idea why
@WarpedImagination
@WarpedImagination Жыл бұрын
Thanks for the kind words .. the content is quite niche, pitched towards a higher level developer and I probably don't put enough effort into sharing the content far and wide. That and I probably forgot to tick a box somewhere .. lol
@martin-schoen
@martin-schoen 2 жыл бұрын
very useful, I will stop complaining about the length. ;)
@WarpedImagination
@WarpedImagination 2 жыл бұрын
They are so short though :)
@orwell235
@orwell235 2 жыл бұрын
Definitely subscribe. Such useful yet neglected topic hasn't been covered yet through out my whole unity career
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Yep I couldn't find it in a quick search so guessed I would cover it in a quick video
@Hazzel31337
@Hazzel31337 2 жыл бұрын
great tip, didnt knew about that one, a skeleton mage with wind and bunny magic what a fierce combination
@WarpedImagination
@WarpedImagination 2 жыл бұрын
After I wrote it I wondered about what bunny magic would be
@NowKnownMAWO
@NowKnownMAWO 2 жыл бұрын
Just discovered this channel. You got my sub and I think you deserve way more subs :) Keep up the good quality and content!
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Much appreciated!
@FoxApo
@FoxApo 2 жыл бұрын
Just in time :-D I was about to take a look on flags couple days ago. Thank you for this.
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Perfect!
@alec_almartson
@alec_almartson 2 жыл бұрын
Excellent Tip, excellent video (straight to the point...or Enum on this case 😄👍🏻)
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Glad you liked it.. more on the way
@zekiozdemir420
@zekiozdemir420 2 жыл бұрын
Thank you!
@WarpedImagination
@WarpedImagination 2 жыл бұрын
You're welcome
@phildonahue3158
@phildonahue3158 2 жыл бұрын
Something like an extension method to handle the enum numbering more like a non flagged enum. I like the "homework" approach btw! It could really help with viewers learning as apposed to watching, thinking "neat", and moving on before internalizing the lesson.
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Good point! .. I might try and add it to more of the videos in the future, even if they dont engage yet stop and think for a second might help them soak it in
@iPlayGamesX
@iPlayGamesX Жыл бұрын
Implemented everything as shown in the video, however the tooltip simply doesn't show up in Unity, hence I cannot select anything. What could be wrong?
@WarpedImagination
@WarpedImagination Жыл бұрын
How odd .. I just rewrote the code in a clean Unity project to check and everything works ok as in the video.
@iPlayGamesX
@iPlayGamesX Жыл бұрын
@@WarpedImagination I just rewrote the script today and it worked. Faulty indentation maybe? Sorry to be a bother, heh. Thanks!
@WarpedImagination
@WarpedImagination Жыл бұрын
@@iPlayGamesX No bother at all .. glad you got it to work 😀
@iPlayGamesX
@iPlayGamesX Жыл бұрын
@@WarpedImagination tyy
@iPlayGamesX
@iPlayGamesX Жыл бұрын
@@WarpedImagination Hey sorry for being a bother again, but how do I actually recall one of these values? For example if I have my upgrades in one of these lists, and I want to check if (PlayerUpgrades._dash) or something? I can't figure it out :(
@slygamer01
@slygamer01 Жыл бұрын
The test against None at 2:03 is unnecessary as the bitwise test already caters for that. The HasFlag() method on Enum used to have performance issues (it boxed the values), but this was fixed in .NET in 2017 so it is comparable in performance to the bitwise test now.
@WarpedImagination
@WarpedImagination Жыл бұрын
Good stuff
@Goomero
@Goomero 2 жыл бұрын
rly need that tool you talked about on jasons podcast that creates projects, sets up folder structures, and adds the required plugins
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Funny you should mention that .. I have been working on that video as one of the longer form tutorials.
@Goomero
@Goomero 2 жыл бұрын
@@WarpedImagination perfect, cant wait
@jameslafritz2867
@jameslafritz2867 9 ай бұрын
I would do a HasMagicSourceSet and return (int)self != 0; as others have stated I would probably not do the example in the video as Enum already has HasFlag. Anither tip to mention is you can set the flag in code by m_magicSource = Eath | Wind; or m_magicSource = m_magicSource | Earth; to add Earth to it. To remove a flag in code m_magicSource = m_magicSoure & ~Earth;
@WarpedImagination
@WarpedImagination 9 ай бұрын
Good tip to add
@mikicerise6250
@mikicerise6250 2 жыл бұрын
noice
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Cheers
@MarekNijaki
@MarekNijaki 2 жыл бұрын
Up again xd
@WarpedImagination
@WarpedImagination 2 жыл бұрын
- C, Down, C, Left, C, Right, C, A + Start
@mojochin7004
@mojochin7004 2 жыл бұрын
Why does my comment keep getting removed? Just pointing out there is already a built in method (HasFlag) on an enum marked as [Flags]. No need for this extra function. m_magicSource.HasFlag(MagicSource.Earth)
@WarpedImagination
@WarpedImagination 2 жыл бұрын
Weird that they are getting removed, I know sometimes links get comments bumped. Your right I should have pointed out HasFlag .. I would still wrap the function on this one as a function like HasMagicSource as being provided MagicSource.None would return true if none were selected which feels a little odd.
@mojochin7004
@mojochin7004 2 жыл бұрын
@@WarpedImagination Haha! I did notice MagicSource(dot)Earth was getting flagged as a link. Not in the above comment though. Weird! Keep up the good work anyway!
@WarpedImagination
@WarpedImagination 2 жыл бұрын
That is so odd and I bet super annoying for dropping code examples in comments .. I will have to take a look at the settings maybe I have something switched on that does that.