Unity Shader Graph - Changing Parameters in Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so hello and welcome back to another unity shadowgraph tutorial it's been a while since I've made one but I've had a few people asking over time about how do you change the parameters of a shader like a shader graph shader in code so that maybe like the shader changes depending on the character's health or you know it might change some value depending on whatever happening in code normally so far all of the things have been changed in the inspector or by time like I'm showing you now but I'll be showing you how to make this dissolve shader go down with like damage taken for example I mean I'm not gonna add enemies in this but I'll add it so that we can control the health which will then in turn control the shader and with this you can apply it to anything any shade you've got where you want a parameter to be able to be changed by a code you can just put it in it's just the same as you would do with a normal like written shader but obviously most people watching these videos haven't coded shaders normally before because it's generally this is like an entry-level kind of thing now to get you into shaders before you learn how to code them properly it's quite handy obviously it's also a lot faster to do things simple things but it's a lot you know slower to do more complex things that aren't quite achievable yet but anyway let's get into it so this video shouldn't be too long anyway here we've got the simple dissolve shader which on a time let's see if we go onto a capsule when we go down we can't actually see it moving right now because this this isn't hooked up in the shader to controlling it we will change that there currently the same time is just going between like 1 and minus 1 to make it appear and disappear what we're gonna do is we're gonna make it so that let's pause it first so let's go into the shader I made this a while back obviously you can use whatever shader you want for this so it's in this folder and it's here dissolve shader so basically if you I mean you should already know parameters are what we've got here these are values we take in that can change things and currently I'm not using dissolve value I'm using time so I scared of this we're gonna put in dissolve value instead so now if I save this that's all you need to do you can then close this so now when I press play it's not going to animate it's going to change only when I change this slider so capsule slider now do you know in an actual game you can't sit in the inspector tweaking things so let's say we want this to you know be set to the characters percentage of HP so if you're on full health it's like this thrown half health it's like this and if they're dead it's like this for example so obviously this isn't gonna look very nice but it's going to change this slider depending on the health of the character so that in the actual game running time it can actually change this value which is what we want so what we need to do we first only tell some code onto this object so we're gonna add a code I made one earlier when I was messing around just called capsule health because I mean for the sake of this test it's just the health of the capsule and then once this is compiled we will open it up now what I'll do is I'm just gonna think about yeah I'll I'll just do this very simply I'm sure anyone watching this you can apply it to your own game there's obviously I'm not making this and a game is just in a test scene so let's just GERD these now first of all we're going to need to have a let's think we need a public material which is so that we can refer to the material of the shader on and I'll call it a dissolve and that that's going to be the material and I also have a health but it's not a health it's a float health and I'll also have a max health so that I can work out a percentage now in void start we will oops what am i doing do not want that okay so in void start we are going to well we can either get just to show how it works first we can get float to find the value in a shader so you could actually if you wanted change code depending on the shader rather than shading changing the shader depending on code it's up to you completely what we want for this is we want to change the shaders value on the slider to be the percentage of the player's health now if you want a percentage of a player's health you can take the health divided by the max health so in the start we will do dissolve mat dot and then basically inside hey this is referring to the shader on the material because what a material is in unity all the material is is the interface on here if material isn't actually a thing a search is it's quite hard to explain what I'm trying to say here think of it as the material is just this window and the shader is behind the seams changing all these values whatever that's what shader is on a standard material it's a standard shader which is called like standard physical based on low render black white light weight render pipeline it's called standard physically-based or whatever everyone using these are all different shaders and we just use a material to plop it onto so if we get this material with a shader on we can then do get not get sort well you can get if you want you can get flow you can get vector you can whatever but you can also do set float to set a value in the parameters so as you see here sets unnamed float value and it takes in a string and a float so the string we're gonna take in I'll get in a second and the float is gonna be the percentage of Health so we're going to take Health / max health that will give us a percentage but here we need a string which is the name of the float inside the material we're going to change now when I tried this ages ago I just for shader graph wasn't ready for this yet boys actually just me being stupid I thought that the float well let's find our material first so scenes of 5 dissolve shader here we go here's our information on our shader here's the properties so we have edge width edge color and dissolve value which we can change in the code the c-sharp code i when when i put you know set float and get the name of the flow I called it you know dissolve value but it's not actually called there's all value it's called you know vector one underscore seven eight nine eight seven b 5d I don't know why well I just assumed that would make sense I kind of ignored what this was I thought this is some kind of a secret like code name behind the scenes but that's actually the name we're referring to so whatever your value is you want to change you get this kind of code left and you put that into the string here so it's vector 1 7 a 9 8 7 b 5d and now that's referring to the shader and we can set the float this is a float to this value and what we're going to do is to make this actually functional as we're gonna do we're gonna in the update to make this you know reacts to what we do we're going to say if input don't get oopsy-daisy input dot get key down key code dark space and then we'll say so when we press spacebar we want health will take off and ten health maybe will take off five health and then as soon as we've taken off health we want to update it again so this start want isn't really necessary it just make sure that when the object spawns it's full you know the shader is completely visible and then as we take off health we then update it we could just put this in the update would it be wasting like it would be doing it every frame when nothing's happening this only does it when it gets updated if you add code on your enemy while like you had a function for it taking damage then you would put this in the function you put this wherever you think you need to to be updated with the thing so now when we press spacebar it's going to take five off the health then it's going to set it to the percentage of health now this video as I said isn't gonna be very long this is just showing you how to set this up for your shaders which is very useful if you want them to actually be useful shaders so now oops we have got an error no we don't yes we do so on the capsule we put a public material and I haven't actually dragged it in so you can either go and find your material and drag it in or you can literally just drag it from here the dissolve mat there so if I set the health to 100 and the max health let's see what happens so we press play and we see that it's visible and if I press spacebar enough then this right as you see down here it's gone to point nine five the actual values changing and on our shader we see a tiny bit there dissolved for us again every time I press it keeps going down point five I'll keep going negative well obviously by this point enemy would be dead anyway now say I can just drag it back up and then just actually not going to work if I want to reset it I'd have to set the health back and then press spacebar so as you see every time it takes damage updates and the shader slowly dissolves more and more and to be honest there isn't much more to this video you should be able to take this and apply it to yours I hope you know the video is helpful and it was you know it's pretty short but that's all I needed to cover there wasn't much more than that so obviously I'm just gonna say a few more things at the end you can obviously stop watching now if you want but obviously leaving a like and subscribe to the channel would be lovely that's the most I can ask for I can also suggest you to join our disco channel in the description which is where we discuss unity JavaScript c-sharp whatever whatever games just anything relate to this channel you can discuss in the server and we're slowly growing in members you know we're getting quite a few more people and tonight if you if you're seeing this video as soon as it comes out we're actually doing a game jam this weekend on the server the theme I will release at midnight tonight British Thunder time starts in an hour and a half from now or so two hours and a half from now and you know it's just gonna be a friendly competition there's not gonna be any prizes though the first second and third place is gonna be show K are going to be showcased in a video that I'm going to do just after the event on maybe Monday or Tuesday we're gonna mean the other admins are gonna rank the the submissions and then pick a first second third place winner and then do a video showcasing those games so you get a bit bit of publicity it's also practice the main reason people do game jams on it's quite fun to get together and all make something on a particular topic but if you've heard this already pretty sure you know you'll heard this already offs if you want to join and you can stall this off time to join any of you that already watched my channel that haven't joined it ice that can then obviously I recommend doing it but I'm not gonna force anyone so anyway yeah I hope this video was informative I hope you all understood it obviously comment if you need help or anything I'm always willing to give help to anyone I think have replied to every single comment on my channel so far but since we're slowly growing over 500 subs now I'm getting quite a few comments so I might end up struggling to keep up I'll try my best so anyway thanks for watching and good bye
Info
Channel: Dapper Dino
Views: 48,716
Rating: undefined out of 5
Keywords: Shader, Shader graph, Unity, Tutorial, Script, C#, Coding, Programming
Id: IQ7qnMv01Vs
Channel Id: undefined
Length: 11min 14sec (674 seconds)
Published: Fri Jun 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.