Created
October 3, 2021 21:01
-
-
Save baobao/664ab9e5a26363bf0b3d850643d620d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[RequireComponent (typeof(MeshRenderer))] | |
[RequireComponent (typeof(MeshFilter))] | |
public class DynamicCreateMesh : MonoBehaviour | |
{ | |
[SerializeField] Material _colorMaterial; | |
private void Start () | |
{ | |
var mesh = new Mesh (); | |
mesh.vertices = new Vector3[] { | |
new Vector3 (0, 1f), | |
new Vector3 (1f, -1f), | |
new Vector3 (-1f, -1f), | |
}; | |
mesh.triangles = new int[] { | |
0, 1, 2 | |
}; | |
float texSize = 256f; | |
mesh.uv = new Vector2[] { | |
new Vector2 (31f / texSize, 234f / texSize), | |
new Vector2 (241f / texSize, 66f / texSize), | |
new Vector2 (64f / texSize, 27f / texSize), | |
}; | |
mesh.RecalculateNormals (); | |
var filter = GetComponent<MeshFilter> (); | |
filter.sharedMesh = mesh; | |
var meshRenderer = GetComponent<MeshRenderer>(); | |
meshRenderer.material = _colorMaterial; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment