- Home /
assign texture to asset material via script
Hi, i dont know if it's even possible or if i'm doing something wrong but here's the issue: I have to create many many objects as assets reading a super long csv file. For each one, i'm creating a material and my problem is to assign it's texture (which i already have in the resources folder)from the editor in the Assets NOT IN THE SCENE. eg: i read the object name from the .csv ,lets say the object its called "302". I then create a "302.mat" material as asset .At this point i have to assign "302.png" (from Resources) to the material. I'm trying this but it doesnt seem to work
material.SetTexture("_MainTex", (Texture2D)Resources.Load("302.png"));
maybe the path its wrong? i dont know!Loading assets and setting textures is a shady subject in the docs imho
Answer by Areait · May 15, 2018 at 09:33 AM
Found the solution, i'll leave it here in case someone needs help: material.SetTexture("_MainTex", (Texture2D)AssetDatabase. LoadAssetAtPath("Assets/Resources/302.png",typeof(Texture2D)));
@Areait AssetDataBase might not work in build. Check if you type the resources folder name wrong.