2013-11-29 93 views
0

我的問題是關於Unity3D。 我有一個從http下載的圖像網址列表。 我只想在GUILayout.BeginScrollView上顯示這些圖像。 我搜索了幾天,但沒有得到任何合適的答案。GUILayout.BeginScrollView - 如何加載從http下載的圖像

這裏是我的代碼示例,

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()); 
    } 
} 

現在,我有很多圖片的URL,但如何顯示在GUILayout.BeginScrollview這些URL的圖像。對於任何幫助Thanx。

回答

1

@Akshay嗨,你所有的設置,只是試試這個...

,如果你有一個圖像列表,然後將它們添加在IList中,獲取它,並使用texture2D inOrder將這些圖像顯示爲滾動視圖。看看這個,我認爲它的答案...

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()); 

    } 
} 

現在寫在你的主類此restcall從那裏你要添加這些圖片滾動型......這實在是很簡單..

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);   
        } 

      } 

現在你正在尋找什麼是listOfImages ..這只是一個「IList listOfImages = new List();」 ,這背後的目的..看到..

//========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=============================================================== 

我想,你有這...怎麼我在它的工作..第二它是reallly很簡單..

0

如果你可以在Texture2D中獲得你的圖像,我有滾動視圖的工作代碼。

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; 
} 

對於從的Texture2D服務器使用該

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