Unity - Weird Bug , Build vs Build and Run

Viewed 599

I am working on a unity project , where I apply a texture on a sphere game object . I get a the texture from a .jpg file. It is specified that the .jpg file isn't placed in Resources or Assets folder , but on a folder inside the project , where the .exe of the project would be also , after building it . So the path for the .jpg is /project-name/Build/texture.jpg . In order to access the .jpg , I use the UnityWebRequestTexture class , which is modified for accessing files on the disk , using also a relative path , as you can see on my code .

// For accessing the .jpg and using it as file we use the UnityWebRequestTexture.GetTexture (see uinty docs)
// The path is a URL , but we modify it to work for local computer with relative path


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;
    
    public class LoadTexture : MonoBehaviour
    {
    
        Texture2D texture;
        Texture2D texture2;
    
        public GameObject sph;
    
        bool on = false;
       
        public Renderer sphRenderer;
    
        void Start()
        {
    
            sphRenderer = sph.GetComponent<Renderer>();
    
            StartCoroutine(GetText());
    
                IEnumerator GetText()
                {
                    using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture("file://" + "/../Build/texture-sphere.jpg")) // Relative path
                    {
                        yield return uwr.SendWebRequest();
    
                        if (uwr.isNetworkError || uwr.isHttpError)
                        {
                            // Display error
                            Debug.Log(uwr.error);
                        }
                        else
                        {
                            // Get downloaded asset bundle
                            texture = DownloadHandlerTexture.GetContent(uwr);
    
    
                        }
                    }
                }
        }
    
    
        void Update()
        {
    
            // Implemenation of toggle by pressing T key
    
    
            // Press T
            if (Input.GetKeyDown(KeyCode.T))
                on = !on;
    
    
            if (on)
            {
                
                // Change to white color first
                 sphRenderer.material.color = Color.white;
    
    
                //Resources.Load("texture", typeof(Texture2D));
                // Set the Texture to the SPH Renderer
                sphRenderer.material.mainTexture = texture as Texture2D;
                                     
            }
    
            else if (!on)
            {
                //set texture to null
                sphRenderer.material.mainTexture = null;
    
                // change color to red
                sphRenderer.material.color = Color.red;
    
            }
    
        }
                
    }

Essentially what I'm doing is toggling shpere's appearance between red color and the texture applied on it , by pressing the T key . I came upon a very weird bug :

If I Build and Run my project , the texture is applied as desired when toggling , but If I click on .exe file (from the same build) and run it again , it doesn't , just stays white (cause , as you can see on my code , I first make the sphere white and then apply the texture) . I've also tried all the possible build and run , and build combinations by changing locations etc and the problem persists . Also when I hit play , in the Game window of unity editor it runs as expected . Only when clicking on the .exe is the problem .

I guess , with Build and Run , unity still uses editor's Assets ? Maybe the devil lies in the dll's or the Resources folder ?

I'm posting also images , of the states I said above .

Build & Run : enter image description here

when I click on .exe : enter image description here

By the way , color apperas different , cause a Random.Range code run .

1 Answers

This is what the StreamingAssets folder is for. Put your runtime-loading graphics in there and access them using the Application.streamingAssetsPath variable.

See the manual for more info.

Related