0

My Question Is Regarding With Unity3D. I have a list of image urls downloaded from http. I just want to show these images on GUILayout.BeginScrollView. I searched About it from few days but not get any appropriate answer.

Here is my code Sample,

public void OnSuccess(object responseFromServer)
{
  File imageObj = (File)responseFromServer;
  IList<File.Image> imageList = imageObj.GetFileList();
  for (int i = 0; i < imageList.Count; i++)
   {
    Debug.Log ("Downloaded Image Url Is  : " + imageList[i].GetUrl());
   }
}

Now, I have a number of Image Urls, But how to show images of these urls on GUILayout.BeginScrollview. Thanx for any help.

2 Answers 2

1

@Akshay hi, You are all set, just try this ...

if you have a list of images , then add them in an IList, got it, and use texture2D inOrder to show these images to scrollview. Look at this , i think its your answer...

public void OnSuccess(object responseFromServer)
{
  File imageObj = (File)responseFromServer;
  IList<File.Image> imageList = imageObj.GetFileList();
  for (int i = 0; i < imageList.Count; i++)
   {
    Debug.Log ("Downloaded Image Url Is  : " + imageList[i].GetUrl());
// just add this in your callBack response. ClassName shoud be where you want to show your // images i.e OnGUI defined..
    (Your_MonoBehaviour_ClassName).GetInstance().ExecuteShow(imageList[i].GetUrl());

   }
}

Now write this restcall in your main class from where you want to add these images to scrollview... it is really quite simple..

private static Your_class_Name con = null;

    public static Your_class_Name GetInstance ()
                    {
                            if (con == null) {
                                    con = (new GameObject ("Your_class_Name")).AddComponent<Your_class_Name> ();
                                    return con;
                            } else {
                                    return con;
                            }
                    } 

public string ExecuteShow (string url)
        {
               string responseFromServer = null;
               StartCoroutine (ShowAllImages (url));
               return responseFromServer;
        }

            IEnumerator ShowAllImages (string uri)
            {
                    IEnumerator e = executeShowAll (uri);
                    while (e.MoveNext())
                    {
                            yield return e.Current;
                    }
            }


    IEnumerator executeShowAll (string url)
            {
                    WWW www = new WWW (url);
                    while (!www.isDone) 
                            {

                                    yield return null;  
                            }
                            if (www.isDone)
                    {
                      listOfImages.Add(www.texture);        
                    }

            }

now you are finding what's that listOfImages.. this is just a "IList listOfImages = new List();" , the purpose behind this is.. see that..

//========Setting Up ScrollView====================================================        
                scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(155));
                if(listOfImages.Count > 1)
                {
                        for(int i=0; i<listOfImages.Count; i++)
                        {
                                Texture2D myImage = (Texture2D)listOfImages[i];
                                GUILayout.Label(myImage,GUILayout.Height(100),GUILayout.Width(100));
                        }
                }
         GUILayout.EndScrollView();
                //========ScrollView=============================================================== 

I think, you got it... coz i worked on it .. nd it is reallly very simple..

0

If you can get your images in Texture2D, I have the working code for the scrollview.

void OnGUI()
{ 
        savedMatrix=GUI.matrix;
        scrollPosition = GUI.BeginScrollView (new Rect (250f,10f,500f,1000f),scrollPosition, new Rect (10f, 0f, 250f, snaps.Length*150f));
        for(int i=0;i<snaps.Length;i++)
        {
            GUI.DrawTexture(new Rect(0f,10f+i*140f,250f,200f),snaps[i]);
        }
        GUI.EndScrollView();
        GUI.matrix=savedMatrix;
}

For texture2D from server use this

IEnumerator Start() 
{
    WWW www = new WWW(url);
    yield return www;
    snap.Add(www.texture);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.