Assigning JPGs to a Texture 2D?

Has anyone experience with loading JPG files from the App’s data directory and then assgning them to a Texture 2D ?
Basically I want to read a JPG file and display it using UnityGUI stuff…
Can anyone help ?

      var fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
      var thebytes = new byte[fs.Length];
      fs.Read(thebytes, 0, fs.Length);
      var texture = new Texture2D(1,1);
      texture.LoadImage(thebytes);
      renderer.material.mainTexture = texture;

That seems to work beautifully (at least in the Unity editor), thanks !

There is one last hurdle: how can I tell the SDK to include my folder with jpg files in the project and put it in the Data folder inside the app package ?

If I do it manually the SDK complains saying that “A signed resource has been added, modifiedor deleted”.

Issue solved using iPhoneStreamingAssets folder… But there is still one issue: it works from the Unity editor, but when compile for iPhone the correct image is not displaied; I checked the Raw folder inside the App and the files I need are there…

This is the code:

var dspTexture : Texture2D;
var imageTextAsset : TextAsset;


function Start(){
	readImage();
}

function OnGUI(){
	GUI.Label(Rect(0,-3,480,326),dspTexture);
}



function readImage(){
	imagePath = Application.dataPath + "/Raw/" + "S1-V8_81.jpg";
	var fs = new System.IO.FileStream(imagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
	var theBytes = new byte[fs.Length];
	fs.Read(theBytes,0,fs.Length);
	dspTexture.LoadImage(theBytes);
}

Any idea why this is working in Unity editor but not on the iPhone ? As said, the files are there in the right folder…

Hi,
i just tryed ways to load jpgs and tryd your approach too. But after loading like 10 Images my app crashed on the device. I don´t know why.
There is no unload Command and i have no idea where the leak is.
This doesn´t seem to happen when loading “normal” unity prepared images.

See my thread.

you likely run out of memory.

a 1024x1024 pixel image is 4MB of RAM (and 4 - 6mb of vram when uploaded there).
thats a lot as you only have 30-40mb of RAM and 22MB of VRAM available on pre 3GS devices

Generally: I think its important to get used to read the crash report (the trace within) and to use the instruments in xcode, otherwise you will not be able to identify any stability problem and deeper missbehavior on the iphone at all