c# - Change Materials Element 0 of Mesh Renderer to a selected image as a shader -


as unity not provide triangle object, need create c#.

after adding mesh filter , mesh renderer c#, object cannot add such components again in scene.

i can change color of object created.

now, need change materials element 0 of mesh renderer selected image shader.

i have tried resources.load () , shader.find () don't work.

how can in c#? please explain code.

here code triangle.cs

using system.collections; using system.collections.generic; using unityengine;  public class triangle : monobehaviour {     public vector3 center;     public float x1, y1, x2, y2, radius;     public string color;      // use initialization     void start () {         gameobject.addcomponent<meshfilter>();         gameobject.addcomponent<meshrenderer>();         mesh mesh = getcomponent<meshfilter>().mesh;         material mat = getcomponent<renderer>().material;         spherecollider sc = gameobject.addcomponent(typeof(spherecollider)) spherecollider;         sc.center = center;         sc.radius = radius;         mesh.clear();          // make changes mesh creating arrays contain new values         mesh.vertices = new vector3[] {new vector3(0, 0, 0), new vector3(x1, y1, 0), new vector3(x2, y2, 0)};         mesh.uv = new vector2[] {new vector2(0, 0), new vector2(0, 1), new vector2(1, 1)};         mesh.triangles =  new int[] {0, 1, 2};         switch (color) {             case "black": mat.color = new color (0, 0, 0, 1);  break;             case "blue": mat.color = new color (0, 0, 255, 1);  break;             case "clear": mat.color = new color (0, 0, 0, 0);  break;             case "cyan": mat.color = new color (0, 255, 255, 1);  break;             case "green": mat.color = new color (0, 255, 0, 1);  break;             case "magenta": mat.color = new color (255, 0, 255, 1);  break;             case "purple": mat.color = new color (1, 0, 1, 1);  break;             case "red": mat.color = new color (255, 0, 0, 1);  break;             case "white": mat.color = new color (255, 255, 255, 1);  break;             case "yellow": mat.color = new color (255, 255, 0, 1);  break;         }     }      // update called once per frame     void update () {      } } 


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -