Manipulating Your Materials In Unity Via Code

If you’re anything like me than you probably want to keep your Assets folder as clean as can be. One of the most annoying things for me is to create an entirely new file for a small change on an object like, creating an entirely new material just for a different colour for example. One called Laser_Green and another called Laser_Blue. That just feels like a waste of space to me.

Luckily for us, Unity will create another instance of a material during runtime. That means that every GameObject will have it’s own copy of the material. This is an important property to note because now I can have 4 different lasers in my game that all use the same Material.

Technically, what you’re doing is accessing some properties on the Shader of the material and then adjusting them. By default, most if not all properties are locked which means you can’t normally access them through code. In order to do so we need to enable the keyword of the corresponding property like so:

To be totally honest, sometimes it just works without enabling the keyword, but I enable it every time just to be safe.

Now, how does one go about finding those keywords? Which properties can you enable? I Google searched for hours trying to find a comprehensive list of them, but nobody would provide a concrete answer. So, I’m going to show you where they are after to make your life easier after kicking myself when I realized that the list was in Unity all along!

You can simply select the Shader of the material and all of the properties will be revealed to you.

For anyone not experienced in Shaders, I’m sure a lot of these look like gibberish and it would take a very long time to go over all of them so for our purposes right now, I simply used the “_Color” and “_EmissionColor” keywords.

So now, code wise we need to get access to the correct instanced material and then it accordingly. If you’re instantiating objects like in my case, then you can simply assign the instance as a variable and get it that way like so:

And if your eye is as keen as I think it is then you’ll notice that I’m passing that material into method called “ExampleMaterialManipulation(Material mat)”. That method looks like this:

It sets a random colour to the emission and base color of the material it was given so now every time I press the space bar we should have 4 lasers that all have random colours

Indeed we do! I really wish someone had told me that just looking at the shaders would show you the properties when I first started. I spent many hours searching for such a list and seriously felt silly when I discovered that they were in Unity all along.

Hope that helps! d^_^

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Randomize Your Fake Data

Setup Multiple Git Accounts on Machine

iOS Biometric Local Authentication

My favorite Linux commands for monitoring network

Setting Permissions to AWS Lambda

Push Notification in iOS Mobile Application

7 Tools That Will Make Your Dev Team More Productive While Working From Home

Creating a Quantum Circuit to Simulate Rolling a Die, Part2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Justen Chong

Justen Chong

More from Medium

My journey becoming a Unity game developer: Main Menu: Main Menu Music and Start button activated

Cleaning up the Homing Missile code

Creating MineField In Unity (Part 2)

How to Remove a Large File from Commit History in Git?