Skip to content

Instantly share code, notes, and snippets.

@knoff

knoff/main.cs Secret

Last active February 13, 2022 23:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knoff/8bb75160b17d3f75924e to your computer and use it in GitHub Desktop.
Save knoff/8bb75160b17d3f75924e to your computer and use it in GitHub Desktop.
Unity 3D. Create material at runtime
Material material = new Material (Shader.Find("Diffuse")); // Common names are: "Diffuse", "Bumped Diffuse",
// "VertexLit", "Transparent/Diffuse" etc.
material.SetTextureScale("_MainTex", new Vector2(100,0)); // "_MainTex" is the main diffuse texture.
// This can also be accessed via mainTextureScale property.
// "_BumpMap" is the normal map.
// "_Cube" is the reflection cubemap.
material.mainTexture = Resources.Load("Floor") as Texture2D; // Texture resource name
AssetDatabase.CreateAsset(material,("Assets/Resources/material.mat")); // material path in assets folder
floor.renderer.material = material;
@ChuckSavage
Copy link

ChuckSavage commented Sep 28, 2016

The last line "floor.renderer.material = material;", no floor defined in your code.

Can you supply the full code file for this? And maybe an example YouTube of you using it?

I ended up creating the following script. It seems easy enough to use a color picker for a mesh.

using UnityEngine;
using System.Collections;

public class MaterialColor : MonoBehaviour
{
    public Color materialColor;
    void Start()
    {
        GetComponent<MeshRenderer>().material.color = materialColor;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment