Hi,I want to draw a lot of large Texture2Ds in Unity GUI Window.
In MS window system,all of the pictures will save in memory,and when finished all the picture painting in a picture,post the finished picuture( actual this is surface) to GUP.
How about unity GUI system?
It seems all of the Texuture2Ds will save in GUP,at first?
I write a test,like this:
public class test : MonoBehaviour {
List<ShowTexutre> showList=new List<ShowTexutre>();
Vector2 scroll_pos=Vector2.zero;
// Use this for initialization
void Start () {
for(int i=0;i<100;i++){
ShowTexutre st=new ShowTexutre("http://myDomain/test/1.jpg");
StartCoroutine(st.DownLoad());
showList.Add(st);
}
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
scroll_pos=GUI.BeginScrollView(new Rect(100,100,600,500),scroll_pos,new Rect(0,0,600,16*100));
for(int r=0;r<16;r++){
for(int c=0;c<6;c++){
int index=r*6+c;
Rect rt=new Rect(c*100,r*100,100,100);
if(showList[index].PictrueTextrue){
GUI.DrawTexture(rt,showList[index].PictrueTextrue);
}else{
GUI.Box(rt,"");
}
}
}
GUI.EndScrollView();
}
class ShowTexutre{
string _url="";
private Texture2D _pic=null;
public ShowTexutre(string url){
_url=url;
}
public IEnumerator DownLoad(){
WWW downloader=null;
yield return downloader = new WWW(_url);
if (downloader.error == null)
{
_pic = downloader.texture;
}
}
public Texture2D PictrueTextrue
{
get
{
return _pic;
}
}
}
}
the size of 1.jpg is 1M.
the error is:system out of memory
You should really get rid of the IEnumerator stuff because (IMHO) it is evil.
You could check out eDriven (asynchronous framework for Unity) - with it you can write a more elegant and error proof code .
You could also check out the source of this demo, to see the usage.
Hi,I am already see this page–GitHub - dkozar/edriven: Unity3d event-driven framework.
It’s really good idea,wo want to try.