Usually when you want to make a modification to any sort of game asset, you want it to happen at runtime and you want it to be temporary. For example, if your character picks up an invincibility power-up, you might want to change the shaderA small script that contains the mathematical calculations and algorithms for calculating the Color of each pixel rendered, based on the lighting input and the Material configuration. More info
See in Glossary of the materialAn asset that defines how a surface should be rendered, by including references to the Textures it uses, tiling information, Color tints and more. The available options for a Material depend on which Shader the Material is using. More info
See in Glossary for the player character to visually demonstrate the invincible state. This action involves modifying the material that’s being used. This modification is not permanent because we don’t want the material to have a different shader when we exit Play Mode.
However, it is possible in Unity to write scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary that will permanently modify a source asset. Let’s use the above material example as a starting point.
To temporarily change the material’s shader, we change the shader property of the material component.
private var invincibleShader = Shader.Find ("Specular");
function StartInvincibility {
renderer.material.shader = invincibleShader;
}
When using this script and exiting Play Mode, the state of the material will be reset to whatever it was before entering Play Mode initially. This happens because whenever renderer.material is accessed, the material is automatically instantiated and the instance is returned. This instance is simultaneously and automatically applied to the renderer. So you can make any changes that your heart desires without fear of permanence.
The method presented below will modify actual source asset files used within Unity. These modifications are not undoable. Use them with caution.
Now let’s say that we don’t want the material to reset when we exit play mode. For this, you can use renderer.sharedMaterial. The sharedMaterial property will return the actual asset used by this renderer (and maybe others).
The code below will permanently change the material to use the Specular shader. It will not reset the material to the state it was in before Play Mode.
private var invincibleShader = Shader.Find ("Specular");
function StartInvincibility {
renderer.sharedMaterial.shader = invincibleShader;
}
As you can see, making any changes to a sharedMaterial can be both useful and risky. Any change made to a sharedMaterial will be permanent, and not undoable.
The same formula described above can be applied to more than just materials. The full list of assets that follow this convention is as follows:
If you declare a public variable of any above class: Material, Mesh, or Physic Material, and make modifications to the asset using that variable instead of using the relevant class member, you will not receive the benefits of automatic instantiation before the modifications are applied.
There are two different assets that are never automatically instantiated when modifying them.
Any modifications made to these assets through scripting are always permanent, and never undoable. So if you’re changing your terrainThe landscape in your scene. A Terrain GameObject adds a large flat plane to your scene and you can use the Terrain’s Inspector window to create a detailed landscape. More info
See in Glossary’s__ heightmap__A greyscale Texture that stores height data for an object. Each pixel stores the height difference perpendicular to the face that pixel represents.
See in Glossary through scripting, you’ll need to account for instantiating and assigning values on your own. Same goes for Textures. If you change the pixelsThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary of a texture file, the change is permanent.
Texture2D assets are never automatically instantiated when modifying them in iOSApple’s mobile operating system. More info
See in Glossary and Android projects. Any modifications made to these assets through scripting are always permanent, and never undoable. So if you change the pixels of a texture file, the change is permanent.
Did you find this page useful? Please give it a rating: