Dany Maor
Dany Maor

Reputation: 2411

Unity code works in the editor but does not work on Build (EXE File) C#

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
        };
    }

}

Upvotes: 0

Views: 1438

Answers (1)

Immorality
Immorality

Reputation: 2174

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.

Upvotes: 1

Related Questions