Search Unity

Change Materials Element 0 of Mesh Renderer to a selected image as a shader.

Discussion in 'Scripting' started by iqer2000, Jul 28, 2017.

  1. iqer2000

    iqer2000

    Joined:
    Mar 4, 2017
    Posts:
    3
    As Unity does not provide triangle object, I need to create it by C#.

    After adding Mesh Filter and Mesh Renderer by C#, the object cannot add such components again in scene.

    I can only change the color of object created.

    Now, I need to change Materials Element 0 of Mesh Renderer to a selected image as a shader.

    I have tried Resources.Load () and Shader.Find () but they don't work.

    How can I do it in C#? Please explain with code.

    Here is my code triangle.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class triangle : MonoBehaviour {
    6.     public Vector3 center;
    7.     public float x1, y1, x2, y2, radius;
    8.     public string color;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         gameObject.AddComponent<MeshFilter>();
    13.         gameObject.AddComponent<MeshRenderer>();
    14.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    15.         Material mat = GetComponent<Renderer>().material;
    16.         SphereCollider sc = gameObject.AddComponent(typeof(SphereCollider)) as SphereCollider;
    17.         sc.center = center;
    18.         sc.radius = radius;
    19.         mesh.Clear();
    20.  
    21.         // make changes to the Mesh by creating arrays which contain the new values
    22.         mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(x1, y1, 0), new Vector3(x2, y2, 0)};
    23.         mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
    24.         mesh.triangles =  new int[] {0, 1, 2};
    25.         switch (color) {
    26.             case "black": mat.color = new Color (0, 0, 0, 1);  break;
    27.             case "blue": mat.color = new Color (0, 0, 255, 1);  break;
    28.             case "clear": mat.color = new Color (0, 0, 0, 0);  break;
    29.             case "cyan": mat.color = new Color (0, 255, 255, 1);  break;
    30.             case "green": mat.color = new Color (0, 255, 0, 1);  break;
    31.             case "magenta": mat.color = new Color (255, 0, 255, 1);  break;
    32.             case "purple": mat.color = new Color (1, 0, 1, 1);  break;
    33.             case "red": mat.color = new Color (255, 0, 0, 1);  break;
    34.             case "white": mat.color = new Color (255, 255, 255, 1);  break;
    35.             case "yellow": mat.color = new Color (255, 255, 0, 1);  break;
    36.         }
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update () {
    41.      
    42.     }
    43. }
     
    Last edited: Jul 28, 2017
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745