How To Change The Mesh In Tango
In this commodity, nosotros will run across how to change material and its properties at runtime in a unity application.
Let's set up a simple scene in unity for this commodity. I accept just added a Sphere GameObject in the empty scene.
Change Material at Runtime
We can utilize multiple materials on a GameObject in unity. You can see these materials in the Mesh Renderer component of the GameObject.
To alter the material at runtime, add below script to the Sphere GameObject.
using UnityEngine ; public class MaterialDemo : MonoBehaviour { Material SphereMaterial ; // Use this for initialization void Start ( ) { SphereMaterial = Resources . Load < Cloth > ( "SphereMaterial" ) ; MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; // Get the current cloth applied on the GameObject Cloth oldMaterial = meshRenderer . material ; Debug . Log ( "Practical Fabric: " + oldMaterial . name ) ; // Set the new material on the GameObject meshRenderer . textile = SphereMaterial ; } } |
We can besides change the materials assortment from the MeshRenderer component. Actually, the GetComponent<MeshRenderer>().material belongings returns the start element of the materials array only.
Material [ ] materials = meshRenderer . materials ; // Get the current material applied on the GameObject Material oldMaterial = materials [ 0 ] ; // Gear up the new material on the GameObject Material [ ] newMaterials = new Material [ ] { SphereMaterial } ; meshRenderer . materials = newMaterials ; |
Change Fabric Properties at Runtime
Material properties is directly related to the shader attached to the cloth. You tin see the shader attached to the material in the Inspector Window.
If nosotros create a new material and then unity already attaches Standard shader to information technology. However, the shader attached to the cloth can exist changed from the Inspector window and as well at runtime.
The properties of the cloth can be changed past editing shader backdrop. You can run into the detailed listing of Shader property in the Inspector.
Later clicking on EditShader in Inpector window, Y'all can run into the shader property proper name and its type (int, float, color etc.).
Modify Texture of a Textile
Change Chief Texture
using UnityEngine ; public form MaterialDemo : MonoBehaviour { public Texture2D SampleTexture ; private void Start ( ) { MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; GetComponent < MeshRenderer > ( ) . textile . mainTexture = SampleTexture ; } } |
Material.mainTexture is the main diffuse texture. This can also exist inverse past "_MainTex" belongings name for the texture from unity'due south builtin standard shader. Many shaders uses more one texture. In that case, make sure to utilize right texture belongings name to modify information technology at the runtime.
using UnityEngine ; public form MaterialDemo : MonoBehaviour { public Texture2D SampleTexture ; // Use this for initialization void Starting time ( ) { MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; meshRenderer . fabric . SetTexture ( "_MainTex" , SampleTexture ) ; } } |
You tin can apply GetTexture property to get current chief texture of the cloth.
meshRenderer . material . GetTexture ( "_MainTex" ) |
Alter Normal Map
Normal map can also be changed by SetTexture property of the material. "_BumpMap" is holding name of the texture in the unity standard shader.
using UnityEngine ; public class MaterialDemo : MonoBehaviour { public Texture2D NormalMapTexture ; // Use this for initialization void Get-go ( ) { MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; meshRenderer . cloth . SetTexture ( "_BumpMap" , NormalMapTexture ) ; } } |
While setting the material texture using the Standard Shader, sometime you may need to use EnableKeyword to enable features of the shader earlier calling SetTexture property.
meshRenderer . fabric . EnableKeyword ( "_NORMALMAP" ) ; |
Alter Metal Texture
// if non enabled m_Renderer . material . EnableKeyword ( "_METALLICGLOSSMAP" ) ; // set texture meshRenderer . material . SetTexture ( "_MetallicGlossMap" , SampleTexture ) ; |
Change Color of the Cloth
Alter Primary fabric's color
Use Material.color belongings to go and fix the principal material colour.
MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; // change color to blue meshRenderer . fabric . colour = Colour . blue ; |
The aforementioned can exist besides inverse by SetColor property. "_Color" is the belongings name of the color in the Standard shader.
// get color meshRenderer . material . GetColor ( "_Color" ) ; // set color meshRenderer . material . SetColor ( "_Color" , Colour . blue ) ; |
Change material color at real-time
Attach below code to the GameObject.
using UnityEngine ; public form MaterialDemo : MonoBehaviour { public Colour newColor ; private void Update ( ) { MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; meshRenderer . material . color = newColor ; } } |
Effect:
Enable Shader Property
Modify Emission Values
As I mentioned earlier, some of the property should be enabled commencement; then simply it can be inverse. For instance, to change the emission color, first we demand to enable Emission belongings then it's colour tin can be inverse.
using UnityEngine ; public class MaterialDemo : MonoBehaviour { private void Get-go ( ) { MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; meshRenderer . cloth . EnableKeyword ( "_EMISSION" ) ; meshRenderer . material . SetColor ( "_EmissionColor" , Color . bluish ) ; } } |
Change Float Value
Many shader property tin can be changed using Cloth.SetFloat property. Example – Metaalic Range, UV Set.
Modify Metal Range
meshRenderer . material . SetFloat ( "_Metallic" , 0.9f ) ; |
Change Shader at runtime
Utilize below code to change shader at the runtime. Make sure the shader name is correct otherwise it will throw null reference exception.
Shader specularShader = Shader . Find ( "Standard (Specular setup)" ) ; MeshRenderer meshRenderer = GetComponent < MeshRenderer > ( ) ; meshRenderer . textile . shader = specularShader ; |
HDRP Materials
HDRP shaders has different backdrop proper name than standard unity shader. Beneath is the shader properties of "HDRP/Lit" shader.
So, while changing shader properties at runtime apply always correct property name. For instance, to alter the color, use "_BaseColor" property.
MeshRenderer mr = TestGameObject . GetComponent < MeshRenderer > ( ) ; mr . fabric . SetColor ( "_BaseColor" , Color . green ) ; |
Also, while creating material brand sure to laissez passer correct shader proper name.
That's information technology for this tutorial. I hope you go an idea about how to change material and its properties at runtime in unity application. Please mail your comments for queries and feedback. Thanks for reading.
The following ii tabs change content below.
- Bio
- Latest Posts
Gyanendu Shekhar is a technology enthusiast. He loves to larn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.
Source: http://gyanendushekhar.com/2018/09/16/change-material-and-its-properties-at-runtime-unity-tutorial/
Posted by: delgadogated1935.blogspot.com
0 Response to "How To Change The Mesh In Tango"
Post a Comment