MeshRenderer
Renders the mesh from the MeshFilter component on the same GameObject.
Read time 9 minutesLast updated 4 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: Renderer
public class MeshRenderer : Renderer
Remarks
This component requires a MeshFilter to be attached to the same GameObject. When the component is active, it renders the Mesh specified in the MeshFilter, including any submeshes.
You can disable the MeshRenderer to make the GameObject invisible.
To render deformable meshes, use the SkinnedMeshRenderer component instead.
Additional Resources: MeshFilter, Mesh.
The following code sample creates 10 randomly-colored renderers at random positions in the scene. To use it, attach it to a GameObject and run the project.
Examples
using UnityEngine;public class PlaceRandomCubes : MonoBehaviour{ void Start() { Mesh cubeMesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx"); for (int i = 0; i < 10; i++) { // Create a GameObject at a random position GameObject go = new GameObject(); go.transform.position = Random.insideUnitSphere * 10; // Add a MeshFilter to supply the mesh data MeshFilter filter = go.AddComponent<MeshFilter>(); filter.sharedMesh = cubeMesh; // Add a MeshRenderer to render the mesh, and assign a random color MeshRenderer meshRenderer = go.AddComponent<MeshRenderer>(); meshRenderer.material.color = Random.ColorHSV(); } }}
Properties
Property | Description |
|---|---|
| additionalVertexStreams | Vertex attributes in this mesh will override or add attributes of the primary mesh in the MeshRenderer. |
| globalIlluminationMeshLod | Mesh LOD to use for Global Illumination. |
| receiveGI | Determines how the object will receive global illumination. (Editor only) |
| scaleInLightmap | Specifies the relative lightmap resolution of this object. (Editor only) |
| stitchLightmapSeams | When enabled, seams in baked lightmaps will get smoothed. (Editor only) |
| subMeshStartIndex | Index of the first sub-mesh to use from the Mesh associated with this MeshRenderer (Read Only). |
Methods
Method | Description |
|---|---|
| GetShaderUserValue | Returns the custom value assigned to this renderer. |
| SetShaderUserValue | Assign a custom value to this renderer. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |