Modify source assets from code
Make temporary or permanent modifications to source assets from your runtime application code.
Read time 3 minutesLast updated 20 days ago
You can modify assets from your runtime code. Depending on the asset type and method you use, the modifications can either be temporary or permanent. Temporary modifications reset on exiting Play mode, and permanent modifications persist on exiting Play mode.
Temporary modification
Modifications to assets at application runtime are usually temporary. For example, you might want to modify the shader component of the material for a character to represent a temporary invincibility power-up. The shader modification in this case is not permanent and does not persist on exiting Play mode.
The following example demonstrates this by changing the property of the component:
shadermaterialusing UnityEngine;public class ExampleScript : MonoBehaviour{ private Shader invincibleShader; void Start() { invincibleShader = Shader.Find("Specular"); } public void StartInvincibility() { if (TryGetComponent<Renderer>(out Renderer renderer)) { renderer.material.shader = invincibleShader; } }}
On exiting Play mode, the state of the Material resets to whatever it was before entering Play mode. Accessing automatically instantiates the material and returns the instance. This instance is simultaneously and automatically applied to the renderer, so any changes you make aren't permanent.
renderer.materialPermanent modification
To avoid the material resetting on exiting Play mode, you can use Renderer.sharedMaterial. The property returns the actual asset used by the renderer.
sharedMaterialThe following example permanently changes the material to use the Specular shader and does not reset the material to the state it was in before Play mode:
using UnityEngine;public class ExampleScript : MonoBehaviour{ private Shader invincibleShader; void Start() { invincibleShader = Shader.Find("Specular"); } public void StartInvincibility() { if (TryGetComponent<Renderer>(out Renderer renderer)) { renderer.sharedMaterial.shader = invincibleShader; } }}
Applicable classes and properties
The previously described programming pattern of using local or shared properties for temporary or permanent changes respectively applies for the following asset types:
Asset type | Property for temporary changes | Property for permanent changes |
|---|---|---|
| Material | | |
| Mesh | | |
| Physics Materials | | |
Assets that are not automatically instantiated
The following asset types are not automatically instantiated when modified:
Any modifications made to these assets from code are always permanent, and can't be undone. If, for example, you change a terrain's heightmap or the pixels of a texture file from code, you'll need to account for instantiating and assigning values manually.