Unity ECS For Beginners (DOTS)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome a while back Unity introduced a new feature called dots well dots stands for data oriented technology stack and it's basically a combination of Technologies and packages that deliver a data oriented design approach to building games in unity applying data oriented design to game's architecture empowers game creators to scale processing in a highly performant manner so basically what is is saying is they create cre a new technology which is faster so dots is made of three things the first one is ECS which stands for entity component system in this video we're going to cover this there is also burst compiler which by the way using this is very easy you're just going to put a tag on your methods and it's just going to do its thing and the next thing is C job system so Unity team has been developing the dots for a while now and it's keep changing so in this video I'm going to tell you about the basics of entity component systems now to start let's open a Unity project I'm going to use 2023 and I'm going to use 3D urp and I'm going to name the project ECS demo I'm going to put it on my desktop let's create the project now that I have a new project let's remove the read me assets and maybe move this to the settings now I am in the sample scene so to start working with ECS first first we need to install the packages so in the package manager and in the unity registry let's find entities package here it is I'm just going to install that and also I'm going to install entities Graphics okay now we have both packages installed so normally we refer to the elements in our scene as objects so this is a game object this is a game object this one as well and this Cube here is a game object to and when we want to control this Cube somehow we could just create a script for it let's actually create a folder called Script and here I'm going to create a new script Cube controller and when we open this script you see that we are inheriting from mono behavior and we could just create variables as data like a variable let's say called speed and we can can run different process inside this mono behavior for example let's rotate the transform every frame based on that speed so as you can see the data and the process that we run using that data are all inside the mono Behavior so now if I go to the cube and let's name this Cube object and attach this mono behavior and play this game you see that my object starts rotating now if we want to do the same process using entity component system it's going to be a little more complicated but it's going to be faster regarding performance now in order to start working with entity component system we need to create a subscene so in order to do that we're going to go to new subscene and we're going to create an empty scene and we're going to choose a name for it I'm going to name it just subscene and let's save now as you can see it's going to create a subscene within our sample scene and if we go to the scenes here you see a folder has been created and that is our subscene now anything we create inside this subscene will automatically is going to be converted to entity so you might ask what is an entity so as I mentioned before we used to call these game objects so in ECS World we're just going to call these entities so for example if I create a cube here inside this subscene this cube is going to be an entity so let's name it Cube entity now let me save this so here you see a circle which you can use to switch the inspector so if we select runtime this is how the entity looks like now if I click on the object you see that it it still has its component but if I click on the entity it looks different so this is going to be an entity and this is just a regular game object now in order to start rotating this Cube first let me disable this now we have this Cube which is an entity now let's align the camera with our view now we have that Cube and let's go to the scripts first we're going to create a script and I'm just going to name it Cube data so let's open this now if we take a look at the cube controller we saw that inside mono Behavior we created the the data and we also created the process that we run on that data but in the ECS we're going to create the data separately and the process that we run on the data separately so in order to do that I'm going to remove this we need to say using unity. entities so we are no longer going to use Unity engine we're going to use Unity entities if I remove Unity engine you see that we are going to get an error because mono behavior is within Unity engine so instead we're going to say public struct so we're going to change the class to struct it's going to be Cube data and we're going to inherit from I component data so we can just go ahead and copy the speed right over here and it does not get a default value so remove the default value so let's save this and go back to the editor so you expect to attach this data to our entity T but actually you can't do that let's actually go ahead and try that here I'm going to put it on automatic which is authoring so now if I try to attach this Cube data to my Cube entity it says you cannot add this script because it needs to drive from monob Behavior so basically this Cube entity is not an entity yet it's going to be converted to entity when you play the game so right now it's just a game object and you can only attach mono Behavior to game objects so in order to fix that we can create another script which is mono behavior let's go ahead and do that and I'm going to name it Cube authoring now let's open this and let's clear that so cube authoring is actually going to drive from mono behavior and we're going to add our speed variable here so basically we're going to attach this this is a mono Behavior to our game object and and when the game object converts to an entity we're going to use this speed and we're going to attach this I component data to The Entity and we're going to put the value of this speed inside this speed it's a little bit complicated I know but this is how it is now to actually do that here we can create a baker so let's say public class and we choose a name for it let's name it Cube Baker and then we're going to drive from the Baker our Baker is going to be for cube authoring so we're going to use Cube authoring we're going to open curly brackets so we're going to do our baking here so if you take a look we're going to get an error which is going to say you need to implement the abstract class which is public override void bake okay so let's do that so basically this function is going to get called whenever we are going to convert our game object to entity and and here we're going to assign the value of this speed which is the mono Behavior inside our inspector to The Entity so first let's get the entity we're going to say War entity and it's going to equal to get entity and we need to pass a flag which just pass Dynamic and we're going to use add component because we want to add the I component data to The Entity so let's add component to The Entity we're going to pass the entity first after that we are going to pass a new Cube data which I'm going to open and close the curly brackets so let's bring this here actually now we're going to say speed equals to authoring speed so now if we go back to the editor we can select our entity and we can attach Cube authoring and now if we go and take a look at the runtime let me disable and select that again you see now we have Cube data attached to our ENT entity while if we go back to the authoring and disable the cube authoring and let's go back to runtime you see that we don't have Cube data attached to our entity so that's the first step we are going to attach the data to The Entity now it's time for us to actually rotate the cube based on that speed so in order to do that we're going to use system the name is entity component system so this was The Entity this was the component and now we need to create the system so let's create a new script I'm going to name it Cube system and you don't need to attach this system to anything if you write the code for it it's just going to work you just open the script we're going to be using Unity entities and let's remove that so when it comes to the system there are two ways you could do this if you just want to run your code on the main thread you can just use system base but if you want to use multi thread and if you want to use the job system then you need to use I system so I'm going to do them both first let's do the system base now we're going to change this to public partial class and we're going to change it to system base now you see that we have an error which says it's missing in class so the class is going to be on update let's actually Implement that so we're going to say override on update this is similar to the monob behavior update that we typically use so if we take a look at the cube controller you see that we are using transform. rotate to rotate our transform so we need Delta time and speed so let's go back to the system first I'm going to get the Delta time you can access the Delta time in system api. time. Delta time basically you do everything using this system API in order to rotate the cube first we need to access the cube data and then we need to access the transform data so we're going to do that by saying for each so basically we're going to run a query and we're going to get those components so if you want to just get one component you could do it just like VAR Cube data in the query that we are running but since we need the cube data and the transform we're going to say VAR Cube data and transform but by the way this is not the transform we have here that's just the name I chose for it you can choose any name inside and now we're going to use system api. query and we need to determine the type of the query so the first one is Cube data and the second one is local transform which is inside Unity transforms in order to move position rotation or a scale you need to access the local transform of that entity it's not like mono Behavior anymore when you can just call transform and you have access to it actually in this system you need to run a query and get the local transform of that entity and it starts making changes on it so in every update it is going to find any entity with Cube data and local transform and bring it here for you now before we start writing the transform logic there's one more thing you need to know so whenever you query the components you you need to determine whether you want to only read data or you want to change data as well so right now in the cube data we only need to read the speed that's all we need we don't make any changes on the cube data we just read from it but the local transform we actually want to change it so we want to write data on the local transform so in order to do that so for the cube data we're going to say ref r o which stands for red only and then we're going to open this and close this so it's going to be ref r o which is stands for read only and for this one we're going to say ref RW and let's open and close that and this W is for right which means we're going to change this local transform we're going to get any entity with Cube data and local transform we're going to put the cube data inside this variable and we're going to put the local transform inside this variable now let's rotate this transform by saying transform. value or W because we want to write to this transform we're going to change the rotation equals to we're going to say transform. value so we're going to write inside this transform before the equal sign but anything after that we're just going to read from it so you don't need to use RW we can just use r o and we're going to call let's say rotate y because as you can see here we are rotating the game object around Vector 3 up in the mono Behavior here we're going to use rotate y instead and this rotate Y is going to get an angle we are going to use the cube data speed so we're going to say Cube data do value rid only do speed and we're going to multiply it by Delta time and that should do it let me save this and let's go to the unity editor and now if I play this you see that object is start rotating but as you might notice it's rotating too fast so let me stop this and if we go back to the code this angle actually is in rodians not degrees so in order to fix that we can just say math dot which math exists inside Unity mathematics so we're going to say math. Rodan and we're going to put this inside side parenthesis now if I go to my unity and play it it should rotate in normal speed now there's something strange that I want to address here if we go back to the scene you see that the cube is not rotating but it's only rotating in front of the camera which is kind of strange I don't know if this is a bug or it's supposed to be like this I don't know I'll definitely do some research on this now let's stop this so this was system base now let's go and do it using I system so I'm going to put all of this inside the comment and for the I system we need to say public partial struct and I'm going to name it Cube system and it's going to inherit from is system now inside I system we can say public vo an update and it's going to get a system state so I'm going to create a job by saying public partial struct and let's choose a name for it I want to name it Cube job and it's going can I inherit from ijob entity now when you create a job you need to create a function inside it called execute so this is how you create a job now in order to run this job you can do it inside the cube system on update we can just say War job equals to New Cube job and once we created the job we're going to say job you have the option to use run by the way using run doesn't make any sense because it's going to be just like the system base so it would be better to just say schedule or schedule parallel so I'm going to use schedule parallel now we have the job and we are running the job so now let's start writing the logic first of all we need the Delta time you cannot get the Delta time inside the job so you need to assign it to the job in order to do that we're just going to create a variable called Delta time inside the cube job by the way this is the cube you job so when you create the Delta time inside the job now here when you create the job you can assign the value of the Delta Time by saying Delta time equals to system api. time. Delta time so when you create the job we're going to assign the value of that and here inside the execute we're going to write our logic but now if you remember in the system base we used query to access the Q data and local transform but inside is system job you don't need to do that you don't need to use Query all you need to do is put the components you want inside this execute parenthesis as parameters and it's automatically is going to pass it to you so we're going to say ref and you're going to use ref to do that Cube data and let's name it Cube data and ref local transform let's just name it transform so you don't need to run anything it's just going to pass it to this execute for you so all we need to do is use this line of code so I'm just going to bring it right here so let's just use transform rotate Y and that's it now if I save this and if we go to the unity editor and play you see that it is giving us the expected Behavior so that that's it that's how you use entity component system I understand that it is complicated and a little bit confusing the unity team is keep improving this and there's a lot of things that I didn't cover in this video I was trying to just tell you the basics if you had any questions feel free to ask me and don't forget to like And subscribe I hope you enjoyed the video thanks for watching
Info
Channel: Developers Hub
Views: 14,872
Rating: undefined out of 5
Keywords:
Id: AHXUh_3IBkY
Channel Id: undefined
Length: 18min 52sec (1132 seconds)
Published: Thu Oct 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.