- Home /
How to Create a new Material from Images Via Script (C#)?
So I understand how to assign materials from a script, but I need help making the materials from images via a script. I would just do it manually but I am working on a virtual art gallery that will need 100+ images converted into materials every week to half a week. As of right now i am trying to access a folder with the images and assign all of them to a game object to make all of the materials to put into a folder to reference later. All answers welcome.,So I understand how to change the actual material but I cannot figure out how to create materials from particular images by assigning them to a cube (any physical game object would be fine). I would do it manually but I am making a virtual art gallery that will have 100+ pictures coming in every week so manually would be a nightmare. Even if it is in a different way than I am trying , all responses welcome.
$$anonymous$$aterials and textures (images) are two different things. A material indicates how something must be rendered by a camera (how is it viewed by the camera, does it react to light - if so, how, ...). A texture is "just" an image mapped onto the object to have a "graphically detailed" surface. I don't see why you would need to create a material for each image. $$anonymous$$ake a single material and apply dynamically the texture afterwards.
Answer by thomh_unity · Oct 01, 2019 at 02:18 PM
Hi cloudstrife869520023,
If I understand correctly you want to automate the creation of materials from large batches of images in the editor?
If so you can do this with the following editor script. Add this script to your Editor script folder and then right click on any number of textures and select 'Create Materials'. This will create new materials based on these textures.
You can tweak the shader used for the materials in the script.
I hope this helps.
Best regards,
using UnityEngine;
using UnityEditor;
public class MaterialGenerator
{
[MenuItem("Assets/Create Materials")]
private static void CreateMaterials()
{
foreach (Object o in Selection.objects)
{
if (o.GetType() != typeof(Texture2D))
{
Debug.LogError("This isn't a texture: " + o);
continue;
}
Debug.Log("Creating material from: " + o);
Texture2D selected = o as Texture2D;
Material material = new Material(Shader.Find("Unlit/Texture"));
material.mainTexture = (Texture)o;
string savePath = AssetDatabase.GetAssetPath(selected);
savePath = savePath.Substring(0, savePath.LastIndexOf('/') + 1);
string newAssetName = savePath + selected.name + ".mat";
AssetDatabase.CreateAsset(material, newAssetName);
AssetDatabase.SaveAssets();
}
Debug.Log("Done!");
}
}
Thank you so much @thomh_unity , this worked perfectly!
Hello! Can you pls explain me this part of code?
savePath = savePath.Substring(0, savePath.LastIndexOf('/') + 1);
I would like to change the path where the materials are saved.
Supposing the texture is located here Assets/$$anonymous$$yFolder/texture.png
, savePath = savePath.Substring(0, savePath.LastIndexOf('/') + 1);
will just remove the file name from thee path and return Assets/$$anonymous$$yFolder/
. Finally string newAssetName = savePath + selected.name + ".mat";
will just append the material name to this path: Assets/$$anonymous$$yFolder/texture.mat
Thank you for posting this. Helped me out significantly today. :-)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to change children's material of gameobject on trigger? 1 Answer
Script executing through walls 1 Answer
How to call child overide function? 1 Answer