Unity Character Customization Using Scriptable Objects | Beginner Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to be taking a look at how we can implement character customization in unity using modular character assets so you can see here i'm using this modular rpg hero kit which is from the asset store but if you want to follow along feel free to use any of your own assets or other asset packs the important thing is that this workflow uses skinned meshes which will swap out through code and ui just so we're all on the same page skinned meshes are 3d models that are all influenced by the same armature this means that no matter what mesh you're using they will deform the same way to a skeleton's animations this allows us to be modular and swap out the models and they will all work with the one set of animations so you can see here that all of these animations it doesn't really matter which meshes we're using we can do it seamlessly and they all deform how you would expect to the underlying skeleton so before we dive into the code let's plan out exactly what we want to do so for planning this out i'm going to be using xtiles who are the sponsor of today's video so the actual coding of this is going to be into two scripts is we've got a scriptable object and a mono behavior so let's start with the scriptable object so using xtiles we can drag out a tile here and we'll call this our scriptable object class so this scriptable object class is going to be responsible for allowing us to create modular sets in the inspector and this will allow us to gate off certain things if we wish so for example you might want multiple scriptable objects that say at this stage a player can only use three hairstyles but once the player reaches a certain level they can use all of the hairstyles and you'll be able to swap out which scriptable objects the character customizer is referencing in our case we're just going to create one scriptable object with which has access to all of the items at the start and i'm doing it this way so that if you want to expand it and put other use cases you can do so so with the asset pack that i'm using um i've got access to a few modular assets so these are and we'll make a bullet list here so we've got um access to a few different belts some different clothes faces gloves hair shoes and shoulder pads and for each of these we're going to need a mesh array to contain all of the possible options so we can see here that we've got all of these different assets that are available so if we just open this in a new image so we've got access to these different clothes these shoes gloves heads hair helmets um you know there's a good range of stuff here so let's just copy this image and we'll drop it in here just so we can visually reference that and let's just expand this to here so this is what we're looking at so this is our script of logic class and we can reference these different items in these different arrays we also might want a color array or skin colors i'll show you how to implement this however it doesn't really work that great with these uh this pack but you can see that the the heads have a few different skin tones if you wanted to do this what you're going to need to do is you're going to have to have a few basic textures which you can then tint in the material but these textures aren't set up for that but i'll i'll implement that and show you how we may be able to use that so we're going to then need a second script and this script is going to be um the actual character customization so this script is going to live on our character game objects the actual thing that's running around in the world and is representing our player in this video specifically we're just going to be implementing a randomize function and this is just going to call into our scriptable object class and access these different pieces of data pick a random one and assign it to our character obviously if you want to take this further you could uh hook this up to a ui and that would allow players to manually scroll through the array and then choose exactly which items they want that should be quite easy to set up if you have any experience with ui stuff but if you want to see a follow-up video from me uh actually implementing this let me know in the comments below and i'll do a follow-up video but as well as this randomized function we're going to need um references to the skinned mesh renderer for all of these um so variables to reference following skinned meshes so this is our mono behavior and this is our scriptable object so before we jump into unity and get coding let me tell you a little bit more about x tiles which i'm using here so xtiles is a tool for visually organizing your ideas projects plans and any useful information into one workspace it combines the best practices from whiteboards and classic documents and it has a great simplicity to it with a clean and intuitive visual interface the best thing is you can start using xtiles for free or better yet you can take 25 off a paid plan which allows for you to put down unlimited blocks and also to upload files up to a 500 megabyte allowance thank you to xtiles for supporting this video head down to the link in the description to learn more okay so here i am in unity and the actual terrain behind me is a another i think it was a free asset i got or part of a pack uh the terrain's not important what's important is our little character here so you can see here we've got um i dragged in the basic female character from the modular rpg heroes and you can see here that we've got an animator which is pointing to the characters uh avatar which if we open up this avatar so you see that every single item here um that we saw in this image here they're all parented in one fbx file to this this uh the same skeleton here and i can show you this a bit better over in blender so we've got our skeleton here and you see underneath this character we've got all of these different um items so if i select all of them and hide them you can see we've just got the skeleton left now this skeleton isn't set up to pose properly within blender unfortunately so we can't control it properly like you would be able to um but you can see how this is working so if i go to um this cloth and i enable this cloth outfit you can see that this is overlapping this skeleton if we deform the skeleton and again it's not going to move properly but you see that this is controlling that mesh underneath it if i then um swap out for the other cloth and again if i start controlling it you can see now how this one skeleton is driving all of these individual um meshes so if you wanted to set this up yourself in blender with your own characters you've got your one armature and then you just um set that armature as the armature for each of the individual kind of modular pieces of clothing or the sort of like helmets and faces etc so these are all just individual pieces that are all parented to the one skeleton and then as we move the skeleton around that controls the different pieces of the mesh so hopefully that made sense so back over in unity you see here that this character has every single item equipped and it looks a bit ridiculous but the basic walk cycles and so this uh walking left um everything is animating to that skeleton and we can just swap out the skinned mesh renderer so we've got our uh say the hair here we're to be swapping out this for one of the other hairs so we've got this hair now set up here so like i said what we're going to need is um two scripts so we're going to need a scriptable object script so we'll create a new c-sharp script and we'll call this um asset so and let's make another script and we'll call this one um customizer and we'll open up this one in our ide as well so we've got our two scripts here so this first one is going to be our scriptable object so up here we can do a create asset menu we'll do menu name and we can say um character assets yeah and we'll just leave it as that and this is gonna derive from scriptable object and this one we're gonna leave as a mono behavior so what this is going to contain is if we refer back to our um xtiles board so we need mesh arrays for all of these different items so i'm just going to copy these over into our and i'll just comment them out just so we know exactly what we need to make so we can have a public mesh array and we'll call this one belt and then we'll duplicate that and we'll have cloves duplicate that gloves ah shoes uh shoulder pads and let's also put in a public color array or skin color and we can get rid of this so that's this scriptable object done so let's just save that i'm just going to jump back over to unity and let that compile and then we can create and we've got our character assets which is this new scriptable object that we've just made and we'll call this all assets so now you can see if we select this over here we can choose from our different belts clothes gloves hair shoes shoulder pads and we've also got an array or skin color so we'll fill those out in a second i just want to go back over and start working on this customize the script so instead of having a mesh array what we need over here is um a skinned mesh renderer reference and we need it for all of this so we could say belt clothes gloves shoes shoulder pads and then we also need a reference to this asset that we've just created so we can make a public um asset so reference and i'll just call this assets and then i'm just going to make a public void randomize method this doesn't need to take in anything but what it does need to do is is assign this mesh renderer based on a random element from our assets scriptable object so we can say um belt is equal to assets dot belts and then we can do random dot range between zero and belts dot and assets dot belt dot length and we actually have to do belt dot shared mesh sorry and then again just duplicate this for um clothes clothes clothes then we could do gloves gloves gloves and hair shoes shoes and shoes then let's also outside of our class i'm gonna make a custom editor um script just so we can have a button in the inspector that'll allow us just click randomize so we'll do a custom editor link tank at our custom editor and this is going to be custom editor type of customizer this is going to sit over a public class and we can just call this a customizer editor and this can derive from editor so the first thing we need to do is override our um on inspector gui and the first thing we want to do is draw the default inspector and then we want to say var customizer is equal and target but we want to cast that to the type of customizer and we say if customizer is not equal to null then we can draw a button so we can say if gui layout dot button and we'll call this uh just randomize and we're just going to call to our customizer and called the run device function on it and really we don't even need this because we know it's not going to be null um based on the fact that this is only going to be showing up when we're actually looking at the customizer class so this will be fine okay so let's pop back over to unity and hook stuff up so we've got our object here i'm just going to add the customizer class to this we can drag in our scriptable object and then we've got um our belt reference our clothes reference um oh i've not made one for the face in this case i probably should have done uh we've got gloves air shoe and shoulder pads and let's correct that let's uh put one in for face so i'm going to call this face and we need to make a faces array and i've also forgot to do shoulder pads so shoulder pads shoulder pads and shoulder pads again let's jump back over to unity let that compile and drag in the face reference over here so currently if we randomize nothing will happen we'll get an error and that's because we haven't actually put anything in our um array so we've got our faces so let's just add the different elements to this so if i i'm going to lock this inspector so we don't lose it and then i can go over into where these are stored so we've got um so i've got default character here and i'm gonna look for belt so we've got belt one two three we've got belt one belt two belt three so we can drag them over into our belt array and i think it needs to be that these lighter colored ones uh we've got faces so let's go to um base one two three four five we drag our faces in we've got our clothes and you get the picture i will just speed through this section okay so they're all assigned now so we've got all of our different um assets into the relevant arrays and then if we go back over to our basic female character here and then we can hit the randomize button and you see this is now doing what we wanted so it's swapping out parts um based on that scriptable object and now obviously you can see that we're mixing here some mage like mage type armor with wizard type robes again that's another use for this scriptable object that we've made so this is obviously all of the assets if you had different like archetypes and you only wanted certain characters but to choose from a certain pool of clothes you could right click create character assets then we could do a you know wizard assets and then if on a character customization screen they choose that they want to play as the wizard class then they would also you just then assign this as the wizard like asset list and then just choose all of the wizardy type stuff but you can see how this is quite a powerful customizable um sort of system if you wanted to implement the um skin color which we're not doing here and this is in from my test uh project so all i'm doing there is i've got a obviously the skinned mesh renderer for the face and i'm storing a reference to the face material in start and then when we randomize the character i'm just setting that materials um color to whatever the random skin color might be from the array so if i go up to this folder you see i've got this range of skin colors and we're just going to pick from a random um color in this list and then we're going to tint the shared material of the face so the face has its own material which has got this base map with this um grayscale and then we can obviously tint this color here to whatever we want whatever's in that array so you could have you could give them the full spectrum and then allow them to be green and orchy um or obviously you could pick from the available list of colors so on this character if i hit randomize you can see that it changes the skin color based on that array um so yeah to make that work what you need is uh the face the texture of the face it has to be sort of like a gray scaly kind of color and then you'll be able to tint it um and that kind of multiplies the color with the base value and you see we can get a range of different skin tones different sort of item types and if we hit play i've got this to randomize on play and it's all animating quite nicely so there you have it that's the kind of character customization system um thanks again for exiles for sponsoring this video do check them out and again if you can get 25 off a paid plan and there's a link for that in the description below i'll be popping these two scripts up on to patreon obviously they're quite simple scripts um but if you do want to kind of get them then feel free to do so over on patreon.com but in the meantime thanks for watching and i'll catch you in the next one bye as always i want to take this opportunity to thank my amazing patrons in the 10 000 xp tier we have john smart and trey briggs and you can see all of the amazing 4000 xpta members on screen now thank you everyone for your support
Info
Channel: Dan Pos
Views: 13,395
Rating: undefined out of 5
Keywords: unity tutorial, game dev, indie dev, game design, unity 3d, made with unity
Id: TW1RSrjbmVk
Channel Id: undefined
Length: 17min 59sec (1079 seconds)
Published: Wed Jul 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.