2014-07-14 33 views
0

我曾嘗試閱讀大部分類似的文章在互聯網上,但我仍然失去了這一點。所以我希望任何人都能給我一些指導。十分感謝!我想在列表中顯示ICON,並且ICON列表是恆定圖像。我怎麼能打電話給這個文件?我做的項目是2D,我只想在GUI上顯示ICON。有關其他信息,我只是試圖用編程的方式來調出圖像,但失敗。下面的代碼是我實現圖像代碼之前的原始代碼。Unity2D,我如何在列表中的GUI.Label上顯示ICON圖像?

GUILayout.BeginVertical ("box"); 
      GUILayout.Label ("Staff"); 
      int maxHeight = 15; 
      int maxHeightRange = maxHeight * 35; 
      scrollPosition = GUI.BeginScrollView(new Rect(50, 25, 400, 175), scrollPosition, new Rect(0, 0, 200, maxHeightRange)); 
      for(int i = 0; i < maxHeight; i++) 
      { 
       GUILayout.BeginHorizontal ("box"); 
       GUILayout.Label ("first"); 
       GUILayout.Label (i.ToString()); 
       GUILayout.Label ("satu"); 
       GUILayout.Label ("icon"); //Icon i want to show is here. 
       GUILayout.EndHorizontal(); 
      } 
      GUI.EndScrollView(); 
      GUILayout.FlexibleSpace(); 
      if (GUILayout.Button ("Exit Shrink Fade")) { 
       LzTbStaff.exitShrinkFade (0.25f); 
       LzShwStaff = false; 
       print("xxx"); 
      } 
      GUILayout.EndVertical(); 

和我在下面添加的部分。

Texture iconsample; 
      iconsample = (Texture)Resources.Load("iconsample.jpg"); 
       GUILayout.Label (iconsample,GUILayout.Width(30)); 
+1

GUI.DrawTexture ...在谷歌搜索... – Savlon

回答

1

我在遊戲中也有類似的情況,我想基於使用GUI高分頁玩家國度展現一個國家的標誌,所以我在「資產/資源/標誌」有一個文件夾與所有國家代碼(例如:br.png,us.png,jp.png,kr.png等)。

函數Resources.Load()僅適用於文件夾「Assets/Resources」中的資源,並且名稱必須只是沒有擴展名的文件名。

例子:

Texture2D myTexture = Resources.Load("Flags/br") as Texture2D; 

而且我的代碼在我的項目中使用:

Texture2D myTexture = Resources.Load("Flags/" + scoreRow.ISOCountryCode.ToLower()) as Texture2D; 
if (myTexture != null) 
{ 
    GUI.DrawTexture(new Rect(marginRight, marginTop, lineWidth, lineHeight), myTexture); 
} 
else 
{ 
    //Debug.Log("invalid flag: " + scoreRow.ISOCountryCode); 
} 

通過的Texture2D您可以使用GUI.DrawTexture畫到屏幕()。

0
Texture2D iconsample; 
      //iconsample = (Texture)Resources.Load("iconsample.jpg"); 
      iconsample = Resources.LoadAssetAtPath("Assets/Textures/UI/Resources/iconsample.jpg",typeof(Texture2D)) as Texture2D; 
+0

此帖被自動標記爲低質量,因爲它是如此短/唯一的代碼。您是否可以通過添加一些文字來解釋它如何解決問題,從而擴展它一點? – gung