Make changes to material's texture in asset folder using unity editor script

I am writing an editor script to update all my material’s texture in a folder under Asset (Asset > Mtl > all .mat files) using texture in another folder (Asset > Texture > all .png)

This is a subset of my code:

Material mtl = new Material(Shader.Find(“Standard”));

// code to store all .mat files in an array

foreach mtl in array

mtl.SetTexture(“_MainTex”, a png texture);
mtl.SetTexture(“_BumpMap”, a png texture);
mtl.SetFloat(“_BumpScale”, bumpScale);
mtl.SetColor(“_Color”, new Color(r, g, b));

EditorUtility.SetDirty(mtl);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

The changes are reflected in the project window:

2968931--220484--upload_2017-2-22_19-1-13.png

But in the inspector and in the scene, only the float changes are saved. The textures are not reflected nor saved:

2968931--220485--upload_2017-2-22_19-1-48.png

I also tried the following method:

GameObject cube;
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
Renderer cubeRenderer = cube.GetComponent();
cubeRenderer.material = mtl;

cubeRenderer.sharedMaterial.SetTexture(“_MainTex”, a png texture);
cubeRenderer.sharedMaterial.SetTexture(“_BumpMap”, a png texture);
cubeRenderer.sharedMaterial.SetFloat(“_BumpScale”, bumpScale);
cubeRenderer.sharedMaterial.SetColor(“_Color”, new Color(r, g, b));

The changes is reflected in project window, inspector and scene

2968931--220486--upload_2017-2-22_19-7-36.png2968931--220487--upload_2017-2-22_19-7-50.png2968931--220488--upload_2017-2-22_19-8-6.png

But all disappears when i save the scene.

What should I do to ensure that my changes will be saved permanently?

2 Likes

Bump this one, did you ever find a solution?

2 Likes

Bump. I tried all of those solutions (dummy game cube, setdirty, etc). I can’t get any variables to save… well randomly a material is generated that saves, but it’s all random. It makes no sense.

1 Like
    static void SetTextures()
    {
        List<Material> assets = FindMaterialAssets();
        List<Texture> assetsT = FindTextureAssets();
        foreach (Material mat in assets)
        {
            mat.mainTexture = assetsT[8];
        } 
    }

This works for me (v2021). Reopening the scene had the new texture assigned to the materials.

2 Likes

I haven tried that, but I was able to finally find a solution.

Before I was doing this…

  1. Material mat = new Material(shader)
  2. mat.CopyPropertiesFromMaterial(otherMaterial);
  3. mat.SetFloat(“_MyParameter”, 8.88f); // SetTexture(), etc
  4. AssetDatabase.CreateAsset(mat, path);
  5. AssetDatabase.SaveAssets();

It seems like that would work just fine. But no!!
I had to change it to:

  1. Material mat = new Material(shader)
  2. AssetDatabase.CreateAsset(mat, path);
  3. mat.CopyPropertiesFromMaterial(otherMaterial);
  4. EditorUtility.SetDirty(mat);
  5. AssetDatabase.SaveAssets(); // middle save
  6. mat.SetFloat(“_MyParameter”, 8.88f); // SetTexture(), etc
  7. EditorUtility.SetDirty(mat);
  8. AssetDatabase.SaveAssets(); // final save

One big change was moving CreateAsset() to the beginning so that I could call save earlier.

The other was setting dirty and saving after CopyPropertiesFromMaterial() but before calling SetFloat(). Without that, all changes go bye-bye. Something funky is going on inside CopyPropertiesFromMaterial().

1 Like

What about if you want just update and not create?

I tried several ways to change the textures but the material doesnt saved it to disk

What about normal, emission textures?

Yes mainTexture is working, but setTexture not.

Also we cant change the normal and other textures because there is not a normalTexture in it.

As I saw the code the mainTexture is calling an ID not the text on the shader.

If we can bypass the id of the normal, emission etc, this would work.