- Home /
how to load images automatically from user's hard disk?
i want to assign a picture from a folder in my computer to a texture2d, but not in the inspector! i want to do it in script. I used Resources Load but there is a problem: I built the game but there was no folder called Resources! how should the user put some extra images in that folder? only I can put images in the folder before building the game, but end user can't.
my purpose is to use images in some folder and load them as texture for some objects. but not in editor! i want to allow the user do this. i've tried many ways like www and this is my last hope. is there any way to do it?
Answer by Uzquiano · May 01, 2011 at 07:44 AM
Hi,
you have several options:
1) Something like this
private var file : String = ""file://c:/files/file.txt";
var reader : StreamReader = File.OpenText(file);
2) using the WWW class
// Get the latest webcam shot from outside "Friday's" in Times Square var url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg"; 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; }
many thanx bro. but the www make the game crashed. and its becuase of using buttons for loading those images. I want the images to load into buttons automatically so i need to do in OnGUI() function. any other idea?
If by "the www make the game crashed" , you mean there's a script error like "The OnGUI() cannot be a coroutine", then you should call the WWW class from another function, which, the later is called inside OnGUI(). Diagrammatically:
function OnGUI()
{
if button pressed
{
URLdownload()
}
}
function URLdownload()
{
var url...
var www…
}
I think the problem is that OnGUI() is called every frame.
(I know it's been a while since the question was asked, but maybe someone will be helped anyhow. Also my program$$anonymous$$g knowledge is very weak, so feel free to correct me)