Search Unity

create materials at runtime

Discussion in 'Scripting' started by appels, Jan 6, 2011.

  1. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Is it possible to create materials at runtime like primitives ?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, look up Material in the docs.

    --Eric
     
  3. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Code (csharp):
    1. floor.renderer.material = new Material (Shader.Find(" Diffuse"));
    2. floor.renderer.material.SetTextureScale("Tiling", new Vector2(100,0));
    3. floor.renderer.material.mainTexture = Resources.Load("Floor") as Texture2D;
    The tiling doesn't seem to have an effect, it works when i have a material created in the editor.
    Any ideas ?
     
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Solved:
    SetTextureScale("_MainTex", new Vector2(100,100));