- Home /
Load Texture at Run time from file
Hi,
The below code will load a texture from the resources - how would i load C:\test\tex.png ?
function Start () {
var go = new GameObject.CreatePrimitive(PrimitiveType.Plane);
go.renderer.material.mainTexture = Resources.Load("glass");
}
There does not seem to be a load method accepting system paths :( [can i cheat this with mono?]
David
You can't use system paths from Resources.Load, but you can have folders beneath your resources folder, e.g. Resources/test/tex.png could be loaded with Resources.Load("test/tex"). Not exactly what you're asking, but might be relevant.
Answer by duck · Apr 07, 2010 at 10:47 AM
Use the WWW class, and provide a file url in the form of:
var url = "file://C:/test/tex.png";
You can then read out the www.texture value once the www request has completed. For example:
function Start () { // Start a download of the given URL var www : WWW = new WWW (url);
// Wait for download to complete
yield www;
// assign texture
renderer.material.mainTexture = www.texture;
}
This should work in standalone builds, but won't work for webplayer builds.
If you're using C#, see this answer for more information on how to use WWW in c#.
in C#
// load it www = new WWW ("file://C:/test/tex.png");
// display it renderer.material.mainTexture = www.texture;
Is possible to make this work in webplayer builds? Even when the texture is in the server?
Answer by astracat111 · Feb 23, 2018 at 04:57 PM
I believe you use persistentDataPath after extacting the data into it at runtime:
function Start () {
// Start a download of the given URL
var www : WWW = new WWW (Application.persistentDataPath + "filepathinPersistentData");
// Wait for download to complete
yield www;
// assign texture
renderer.material.mainTexture = www.texture;
}
That will work on android, might work on webgl depending on permissions, not sure about that one.