Using Attributes to Manage Data in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if like me you're designing a rather largely scoped game you're probably someone who also likes to design tools that make their life much easier whenever they can i frequently found myself strictly and methodically writing out lines and lines of code to populate lists menus and dropdowns in the editor with the types of data i want and then programming more lines of code to tell the editor or runtime application what to do with it i hadn't realized until recently that there was a much much easier way to do some of the things i've been doing one of the most powerful and flexible items in your inventory as a tools programmer is being able to tag parts of your code with custom attributes and have your code figure out what to do with it later via reflection this can be a great way to populate custom menus and present data to designers in the editor without needing to fetch all of it manually hi there i'm matt and welcome to gamedev guide in this video we're going to take a look at how to use custom attributes and reflection in c-sharp to dynamically populate our editor tools and create automatic functionality at runtime we're going to write our own custom attributes to fetch data from our project and then look at how we can use the reflection namespace in c-sharp to iterate through our project and reference data from these attributes we're also going to look at a few different examples of how reflection can be used to fetch data values at runtime and why that might be useful to you in your project so let's go ahead and get started let's imagine that we're working on a game with a team and we've been tasked with presenting data in the game to the designers the designer would like to be able to reference data values directly sometimes so that the localization can print them at other times so that they can do logic checks on the data i have a couple of scripts here with some fields of data that i'd like to expose in the editor for now i just want to use this editor window to show a list of these fields let's start by creating a new script called exposed field attribute this will be used above any field we want to expose in our editor window attributes are essentially tag objects we can use within our code so we can add properties to them for other parts of our code to reference at the top of our attribute class we can coincidentally add an attribute to tell c sharp where this attribute can be used we want it to expose fields in our editor window so we'll set the attribute usage to fields only then let's go through and add this attribute to the fields we want to expose in our editor now we've tagged our types in our script with our own custom attributes let's head into our editor window and use reflection to display the fields reflection is a c-sharp namespace that allows us to dynamically create an instance of a type bind the type to an existing object or get the type from an existing object and invoke its methods or access its fields and properties in a nutshell it lets you gather and iterate over data in your project dynamically let's start by getting all of the assemblies in our application using the app domain dot current domain reference then let's iterate through all of our assemblies and get the types within that assembly let's then iterate through those types and get info about its members the member info returns info about a types properties methods constructors fields events and nested types we could use the get fields method instead but i like this approach because it keeps the search agnostic in case an attribute is used on more than just fields by default this method only searches for public members so we need to pass in some binding flags to make sure we get our private fields returned too from here we'll iterate along our member info and see if the current member has any custom attributes if it does we'll see if it has our exposed field attribute and then add this member to a members list now we have a list of members from across our project all we need to do is display that info in our editor window as you can see we now have a dynamic list in our editor that returns any field with our exposed field attribute if we go into our code and add more attributes these will also show in our editor window retrieving information from a type and using attributes like this can be extremely useful let's suppose we want to give these fields a display name in our editor we can do so by extending our attribute to have parameters in our exposed field attribute let's create a new display name field and define this as a parameter when declaring our attribute then let's go through and assign some display names for our fields back in our editor window i've declared a struct that takes both the attribute and member info and adds it to our list then when we're displaying the info we just pull the display name from the stored attribute and now our window is full of more readable names passing data through attributes like this can be incredibly powerful when it comes to building custom editors so our window is populated but it's not instant what if we want to dynamically retrieve data at runtime i want our editor to show and track these values so we're going to need to change things up a bit our game manager class creates instances of our data classes at runtime so we just need to update our editor to use the instances from our game manager this is actually pretty straightforward in our editor we can cast our member info to field info and from here we can read the field using the get value method and pass in our instance type when our game is running our editor window now displays the values from our instance class this little button increases how much money we have by performing some labour as you can see when i press it our reflected reference updates to display the new value we're now dynamically retrieving and displaying this data so we now know how to get references to instance data dynamically let's look at some of the things we can do with it one of the most common things we do in game development is inserting data into text now usually the data would be presented by the designer placing tags into the text and then you'd probably write a pretty lengthy script to pass those tags and replace them with the right data i hate this method because it requires a lot from both the programmer and designer you need to keep memory of all the tags and their syntax and adding new data or tags tend to have quite a significant overhead let's look at how we can use our attributes to replace these strings and dynamically pass data into our text without the messy overhead of a strict text syntax i've created a new class called exposed value selector in here we simply need a string called field name this will act as a reference to the reflected value we want our text to display let's create another script called exposed value text this will be the script that does the work replacing the text with our selected values in here we'll create a text area string we'll then also create an array of exposed values now if we add this to one of our text boxes we can see that our exposed value elements are just string text fields we could manually type the field name in here but let's do a bit of work and present something that we can easily select let's create a property drawer for our value selector in here we'll perform some reflection and add all of our exposed fields to a list i'm using a separator in the field names to break our pop-up into submenus the final thing we need to do is tell our property to assign our selection to the field name and vice versa in our inspector our list should now display all of the exposed fields let's just remove these tags from the text here and replace them with our identifiers the last thing we need to do is tell our text script to dynamically pass the identifiers and our values at runtime for performance reasons let's grab the exposed reference info once and store it in a static dictionary then we just have to go through our exposed value selector and grab the field from the dictionary and then we just get the value from the field like before as you can see this now takes the pressure off to remember all of the tag syntax in your text system you can now dynamically assign values to your text and if the data can't be presented yet simply adding the attribute will add it to this list and automatically present the value in our text saving a significant amount of time and effort in both stages of writing our text and that's about it for this video obviously these are just a couple of examples of how we can harness the power of attributes and reflection but hopefully it's given you a few ideas of how you might be able to take advantage of them in your own project just before i go i am pleased to announce i have finally opened up a patreon account as you may or may not know i'm a full-time game developer and i don't plan on stopping that anytime soon frankly it's one of the main reasons why i'm able to create the videos i do my day job is basically research and development for these videos however one of the biggest challenges i have is making these videos fit into a regular schedule with some financial support i know i'd be able to dedicate and justify spending more of my free time making videos for you there's a few tiers available but a single dollar regularly from just one percent of you watching would definitely go a long way of course nothing is going to change everything will still be here on the channel but if you appreciate the content and can contribute a few dollars each month then please do consider following the link below and joining one of the tiers anyway if you enjoyed the video be sure to give it a thumbs up and let me know your thoughts down below if you're new to the channel please consider hitting that subscribe button additionally if you want to see more videos from me there's a couple of recent ones on screen now as always thank you very much for watching and i'll see you again next time
Info
Channel: Game Dev Guide
Views: 38,224
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games
Id: vLKeqS1PeTU
Channel Id: undefined
Length: 9min 13sec (553 seconds)
Published: Mon Mar 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.