0

My code load external images and build from them a SkyBox for unity3d. All the files of the SkyBox are on the right paths (Copied manually).

The code loads 6 external images and then builds a SkyBox And I think there's a problem in the loading when it's in build stat. (But not sure)

Or maybe Unity prevents me from doing it?

And i have no error or warning code. It really drives me crazy!!!

using System.IO;
using UnityEngine;

public class ChangeSkyBox : MonoBehaviour
{

public static Texture2D LoadPNG(string filePath)
{

    Texture2D tex = null;
    byte[] fileData;

    if (File.Exists(filePath))
    {
        fileData = File.ReadAllBytes(filePath);
        tex = new Texture2D(1024, 1024);
        tex.LoadImage(fileData);
    }
    tex.wrapMode = TextureWrapMode.Clamp;
    return tex;
}




public static Material CreateSkyboxMaterial(SkyboxManifest manifest)
{
    Material result = new Material(Shader.Find("RenderFX/Skybox"));
    result.SetTexture("_FrontTex", manifest.textures[0]);
    result.SetTexture("_BackTex", manifest.textures[1]);
    result.SetTexture("_LeftTex", manifest.textures[2]);
    result.SetTexture("_RightTex", manifest.textures[3]);
    result.SetTexture("_UpTex", manifest.textures[4]);
    result.SetTexture("_DownTex", manifest.textures[5]);

    return result;
}

private Texture2D[] textures;



private void Start()
{

    Texture2D xt1 = LoadPNG(Directory.GetCurrentDirectory() + "\\Assets\\SkyBox\\Front.png");
    Texture2D xt2 = LoadPNG(Directory.GetCurrentDirectory() + "\\Assets\\SkyBox\\Back.png");
    Texture2D xt3 = LoadPNG(Directory.GetCurrentDirectory() + "\\Assets\\SkyBox\\Left.png");
    Texture2D xt4 = LoadPNG(Directory.GetCurrentDirectory() + "\\Assets\\SkyBox\\Right.png");
    Texture2D xt5 = LoadPNG(Directory.GetCurrentDirectory() + "\\Assets\\SkyBox\\Top.png");
    Texture2D xt6 = LoadPNG(Directory.GetCurrentDirectory() + "\\Assets\\SkyBox\\Bottom.png");
    SkyboxManifest manifest = new SkyboxManifest(xt1, xt2, xt3, xt4, xt5, xt6);

    Material newMat = new Material(Shader.Find("RenderFX/Skybox"));
    newMat = CreateSkyboxMaterial(manifest);

    RenderSettings.skybox = newMat;

    DynamicGI.UpdateEnvironment();
}

}

public struct SkyboxManifest
{
    public Texture2D[] textures;

    public SkyboxManifest(Texture2D front, Texture2D back, Texture2D left, Texture2D right, Texture2D up, Texture2D down)
    {
        textures = new Texture2D[6]
        {
             front,
             back,
             left,
             right,
             up,
             down
        };
    }

}
2
  • after this if (File.Exists(filePath)) put an else { Debug.Log(filePath + " not found :("); } to see if it actually find the file. Also, assets doesnt feel like an external path Commented Jul 15, 2018 at 12:05
  • Thanks. I opened the folder myself and put the files there Commented Jul 15, 2018 at 12:06

1 Answer 1

1

I believe your problem lies on this Line.

 Material result = new Material(Shader.Find("RenderFX/Skybox"));

Unity cannot find it at runtime. To fix it, make the "base" Material by hand in Unity and attach it to your script throurgh the Inspector.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad i could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.