I'm trying to load a material that has a Bumpmap added to it.
If it can't find the material for the object, then it'll look for just a basic Texture2D texture.
try
{
sphere.renderer.material = Resources.Load("Planets/" + PlanetType.ToString()+".mat") as Material;
if (sphere.renderer.material == null) throw new Exception();
}
catch (Exception)
{
sphere.renderer.material.mainTexture = Resources.Load("Planets/"+PlanetType.ToString()) as Texture2D;
}
if (Texture != null)
{
sphere.renderer.material.mainTexture = Texture;
}
Now it's not working. It's just loading an empty material in there. I rechecked the material to make sure that I didn't save it incorrectly. But it shows that the textures are still there. I checked the Material property to make sure it not read only. but it isn't..
So what am I doing wrong here?? If this will not work, how would I go about doing this.
Files are :
Acura.png = texture
Acure_BM.png = bumpmap
Acure.mat = material (including all bumpmap and information)
The material load is wrong for sure - you can't add .mat on the end of it. If you have two items with the same name but different types, use the overload of Resources.Load which takes a second parameter for the type.
On top of that, you're currently assigning a null material to the renderer, throwing an exception, then using the null material as if nothing happened.
If it were me, I'd do something more like this:
Material mat = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Material)) as Material;
if (mat != null)
{
sphere.renderer.material = mat;
Texture2D tex = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;
if (tex != null)
sphere.renderer.material.mainTexture = tex;
}
else if (Texture != null)
sphere.renderer.material.mainTexture = Texture;
try
{
Material mat = Resources.Load(“Planets/” + PlanetType.ToString(), typeof(Material)) as Material;
if (mat != null)
{
sphere.renderer.material = mat;
}
else // mat is null, load the texture
{
Texture2D tex = Resources.Load(“Planets/” + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;