DoubleVoid
DoubleVoid

Reputation: 795

Load image asset at runtime with Unity 3D

I am loading a PNG image from the asset folder like so:

string texture = "Assets/Sprites/Maps/Map1.png";
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(texture, typeof(Texture2D));

This works fine, but upon building the game it doesn't work. I guess it's because this is a debug/editor specific function that the Unity Player can't handle.

My question: Is there another way to load image assets that works with the compiled game?

Upvotes: 1

Views: 5922

Answers (1)

Everts
Everts

Reputation: 10701

Asset folder no longer exists after you built. You need to store your asset in Resources or StreamingAssets folders.

https://docs.unity3d.com/ScriptReference/Resources.html

https://docs.unity3d.com/Manual/StreamingAssets.html

Upvotes: 3

Related Questions