Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting texture for NON game object material

Discussion in 'Shaders' started by chanfort, Aug 21, 2014.

  1. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    I am keep finding only answers how to set texture for material, which is assigned for gameObject in this way:

    Code (CSharp):
    1. renderer.material.mainTexture = myTexture;
    However, when I am applying this rule for material, which just created and saved into .mat file, nothing happens. Here is the example how I am doing it:

    Code (CSharp):
    1.             string filePath = "Assets/MyMaterial.mat";
    2.             var material = new Material (Shader.Find("Particles/Alpha Blended"));
    3.            
    4.             material.mainTexture = myTexture;
    5.            
    6.             AssetDatabase.CreateAsset(material, filePath);
    There are the same results if I am creating material first and later assigning texture.

    So I was interested if there is a way to assign textures to materials, which are saved into .mat files and not attached to game objects?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,591
    Assuming myTexture is a reference to a texture inside your project, not a reference to a texture created via scripting or instantiated, it worked in a test I did a minute ago.

    I just copy/pasted your code into an editor script and the created material holds a reference to the texture:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public static class CreateMatTest
    6. {
    7.   [MenuItem("Test/CreateMat")]
    8.   static void DoWork()
    9.   {
    10.     string filePath = "Assets/MyMaterial.mat";
    11.     var material = new Material (Shader.Find("Particles/Alpha Blended"));
    12.     material.mainTexture = (Texture)AssetDatabase.LoadMainAssetAtPath("Assets/chewbacca.jpg");
    13.  
    14.     AssetDatabase.CreateAsset(material, filePath);
    15.   }
    16. }
    However, I remember I ran into various issues in the past with the mainTexture property of a material, not really setting the texture. I think, and I could be completely wrong here, that the mainTexture property assumes the sampler inside the shader to be named _MainTex and refuses to work if it's using a different name. I ended up using material.SetTexture instead, but in this case the sampler has the correct name.
     
  3. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    Thanks for answer. By the way, is there a way to assign just generated texture (i.e. taken png snapshot) to material which I want to create? Now this works, but it needs two times to run my simulation. First time it is creating png textures and empty materials, as applying texture fails. Second time I need to run when Unity reloads all assets. At the second time I run the same script, everything is the same, except that when material texture is applied, it is using textures generated at the first run and imported in Unity between these two runs. Then my materials are getting ready to use. Is there a way to avoid this second run, i.e. maybe there is possible to reimport refresh assets in Unity during script life time?