using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoaderScript : MonoBehaviour
{
public GameObject TestObject;
private SpriteRenderer spriteR;
// Start is called before the first frame update
void Start()
{
spriteR = gameObject.GetComponent();
InvokeRepeating(“SpriteLoader”, 9f, 1800f);
}
// Update is called once per frame
void Update()
{
}
void SpriteLoader()
{
spriteR.sprite = Resources.Load(“1”);
}
}
In the editor, after 9 seconds the sprite swaps to “1.png” added into the Assets/Resources folder
But after being built, if the same image “1.png” is added into the MyProject_Data\Resources fails to load and sets the current sprite for the testobject as none
Editor is seen in test1.gif
The built project is seen in test2.gif
If anyone can help me out with understanding how to load assets after the project’s been built and let me know if this is just not the way it’s done at all then I’d be very grateful, thanks in advance!
Because it isn’t in the master database if it wasn’t built in.
Not only that but outside of Unity, the 1.PNG file is NOT a sprite, it is just a texture. The Unity Editor can be configured to cut a sprite out of that if it is correctly imported, but the PNG file itself is never the sprite.
If you want to add a Sprite (or any data) to a project AFTER it is built, your options are:
use Addressables (or the older Asset Bundles)
implement your own runtime texture loading and sprite cutting code
Tutorials for both of the above can be found all over the internet.
To make this absolutely clear: The Resources folder inside a Unity project is an authoring folder that does not exist in a built game. Assets placed and imported in that folder during edit time are packed into the asset database and Resources.Load loads those assets from the internal database.
Note that the Unity editor does support many different media formats to be imported at edit time. The Unity player (the runtime of a built game) however only has very limited support for loading assets. AssetBundles are essentially created with the Untiy editor and are also stored in Unity’s own internal asset format. So they can be loaded externally at build time. Unity does support loading PNG, JPG and TGA images at runtime. For audio, afaik, it only supports OGG and WAV. It doesn’t have any built-in support for loading models or meshes. However you can roll your own loader if you really need to load a certain format that the Unity runtime does not support.
However in your case, since you want to load a png file, that’s not really an issue. Though you would not load it through the Resources class but instead use a UnityWebRequest or usual System.IO.File methods to load the raw data and pass it to the LoadImage extension method of a Texture2D.
The question is why you actually need / want to load the image externally. If possible you should just import them into Unity directly and let it compile into the project itself.