- Home /
How do I attach a Created Material A Texture, NormalMap and OcclussionMap?
Good day.
I would like to have over a Script automatically create materials, which are each equipped with a Albedo , NormalMap and OcclussionMap . The material is created without any problems and he does not give me any errors. But the images available are not included. If I do it all by hand, it works (via the Unity GUI).
He also finds the OCC , NRM and COLOR files. Have I already checked over debut.log. Can someone help me there?
private void CreateMaterial(string path) {
Texture2D albedo = new Texture2D(2,2);
Texture2D normal = new Texture2D(2,2);
Texture2D occlusion = new Texture2D(2,2);
string dir = null;
foreach (string file in Directory.GetFiles(path)) {
if (file.Contains ("OCC") && !file.Contains(".meta")) {
occlusion.LoadImage (File.ReadAllBytes (file));
} else if (file.Contains ("NRM") && !file.Contains(".meta")) {
normal.LoadImage (File.ReadAllBytes (file));
} else if (file.Contains ("COLOR") && !file.Contains(".meta")) {
albedo.LoadImage (File.ReadAllBytes (file));
}
int index = path.LastIndexOf ("/");
dir = path.Substring(index);
}
if (!File.Exists(Application.dataPath + "/Materials/RoomThemes" + dir + ".mat")) {
Debug.Log ("File " + dir + ".mat nicht gefunden.\n");
Material material = new Material (Shader.Find ("Standard"));
if (albedo != null) {
material.SetTexture ("_MainTex", albedo);
Debug.Log ("albedo gesetzt");
}
if (normal != null) {
material.SetTexture ("_BumpMap", normal);
}
if (occlusion != null) {
material.SetTexture ("_OcclusionMap", occlusion);
}
AssetDatabase.CreateAsset (material, "Assets/Materials/RoomThemes" + dir + ".mat");
}
}
Interesting idea. The code for creating the material and assigning the textures seems to be correct, so the issue is somewhere else. On the other hand, the loading of the textures looks strange to me. Have you tried using Resources.Load()
?
To understand your question better, why do you want to create the materials from a script? Is this an Editor script, or is it to run during the application execution? In the second case, you cannot use AssetDatabase
calls, because they are only available in the Editor.
Finally, does calling this function give any error in the console?
Error issues, I get no. I would like to achieve that later I simply have to push pictures into a specific folder and he automatically creates materials, which I assign again automatically around new objects. This is to create an automatic gallery which continues to expand. I read some time ago that you should not use Resource.Load for any reason. I'll try it anyway.
No. It come the same Result
It seems AssetDatabase.CreateAsset()
doesn't work with materials since 5.3... (see this "answer"). I will investigate a bit more, maybe there is a workaround.
UPDATE 1: this seems to be the issue: material.Enable$$anonymous$$eyword()
needs to be called. However, it does not yet work for me.
UPDATE 2: I could not find any way to make the material take the textures, even though I tried various ways. If you find a solution to your question, please share it, because this should work.
Your answer
Follow this Question
Related Questions
Why the gui texture is not in the middle of the screen and how can i change the texture size ? 1 Answer
How to change material that is being scrolled for Material.SetTextureOffset 2 Answers
Moving and scaling an object according to hand position. 1 Answer
Multiple Cars not working 1 Answer
Material Instances Problem. 1 Answer