Materials From Textures Through a Context Menu in Unity

I sometimes find it painful to create materials in Unity. The process goes like this:

Developer,

  1. Clicks on Create->Material
  2. Names the Material asset
  3. Chooses a shader for the material from the material inspector shader drop-down
  4. Finds the texture to be used in this material, drags and drops it to the respective texture channel

This seems like a simple job if you are to create a single material for a single texture. But this seemingly simple thing gets out of hand and becomes a click-fest too quickly when you have to create say 10+ materials. So Unity, having an awesome extensible editor, lets me ease this pain so I have programmed a very simple editor script as you can see below.

To understand how it works please consult the included -needlessly verbose- comments.

It can be improved still, so I’ve made it available and it’s your job to do so and share your version!

Here is a nice starting point to improve it, currently it only assigns the “mainTexture” field. Why not assign the specular/normal/etc. maps too? Using the texture filenames could be a good start to add such a feature.

Please contact me through the comments or the contact form here if you have any questions/suggestions, cheers!

4 Comments Materials From Textures Through a Context Menu in Unity

  1. Pingback: A Custom Asset Importer for Unity | Sarper Soher

  2. Alex Coulombe

    This is AWESOME, except I can’t get it to work with skybox/cubemap shaders. It’ll create the material, but it won’t put the texture in.

    Reply
    1. Alex Coulombe

      Ah! Nm I got it. For anyone trying to do this, just change:

      var material = new Material(shader) {mainTexture = selectedTexture};

      to
      var material = new Material(shader);
      //works for non-Cubemaps
      material.mainTexture = selectedTexture;
      //only way to get Cubemaps to work!
      material.SetTexture(“_Tex”, selectedTexture);

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *