Stream assets directly into a build
Distribute assets as packages
Modify source assets from codeYou 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 modificationModifications to assets at application runtime are usually temporary. For example, you might want to modify the shaderA program that runs on the GPU. More info
See in Glossary 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 shader
property of the material
component:
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.material.shader = invincibleShader;
}
}
}
On exiting Play mode, the state of the MaterialAn asset that defines how a surface should be rendered. More info
See in Glossary resets to whatever it was before entering Play mode. Accessing renderer.material
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.
Note: If you declare a public variable that holds a reference to the material and modify that instead of modifying the renderer.material.shader
class member directly, you wonât get the benefits of automatic instantiation before the modifications are applied.
Important: The method presented here modifies actual source asset files used within Unity. You canât undo these modifications. Use them with caution.
To avoid the material resetting on exiting Play mode, you can use Renderer.sharedMaterial. The sharedMaterial
property returns the actual asset used by the renderer.
The 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:
Note: If you declare a public variable of any of these classes and make modifications to the asset using that variable instead of using the relevant class member, you wonât get the benefits of automatic instantiation before the modifications are applied.
Assets that are not automatically instantiatedThe 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 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 heightmapA 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 or 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 from code, youâll need to account for instantiating and assigning values manually.
Stream assets directly into a build
Distribute assets as packages
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4