2016-08-07 32 views
0

我有三個場景。 1)你在哪裏組建你的團隊。 2)建立關卡的地方。 3)遊戲。如何將sprite保存到playerPref中?

在我的團隊中,每個團隊成員有5個選擇。 我想弄清楚如何設置播放器,然後在其他場景中調用該播放器的ImageSprite

我想playerPref會工作,但似乎這不是一個選項。

什麼是從一個場景保存圖像並在不同場景中調用圖像的好方法?

回答

0

你可以只保存精靈PlayerPrefs的名稱,然後從資源加載:Resources.Load(spriteName);

+0

這樣保存爲一個字符串? –

+0

@TimCooley是的。 – Iggy

+0

GetComponentInChildren ().name我現在試圖捕獲精靈的名字。 –

2

你可以存儲在playerprefs精靈爲base64的質地,那麼你可以創建存儲紋理精靈。但紋理必須是支持讀/寫的格式,如ARGB32,RGBA32,RGB24等。以下是一個示例;

using UnityEngine; 
using System.Collections; 

public class TextureStore 
{ 

    public static void WriteTextureToPlayerPrefs (string tag, Texture2D tex) 
    { 
     // if texture is png otherwise you can use tex.EncodeToJPG(). 
     byte[] texByte = tex.EncodeToPNG(); 

     // convert byte array to base64 string 
     string base64Tex = System.Convert.ToBase64String (texByte); 

     // write string to playerpref 
     PlayerPrefs.SetString (tag, base64Tex); 
     PlayerPrefs.Save(); 
    } 

    public static Texture2D ReadTextureFromPlayerPrefs (string tag) 
    { 
     // load string from playerpref 
     string base64Tex = PlayerPrefs.GetString (tag, null); 

     if (!string.IsNullOrEmpty (base64Tex)) { 
      // convert it to byte array 
      byte[] texByte = System.Convert.FromBase64String (base64Tex); 
      Texture2D tex = new Texture2D (2, 2); 

      //load texture from byte array 
      if (tex.LoadImage (texByte)) { 
       return tex; 
      } 
     } 

     return null; 
    } 
} 
+0

實際上這非常酷,特別是如果你想通過網絡發送自定義紋理。 – Iggy

相關問題