2016-08-07 4 views
0

세 장면이 있습니다. 1) 팀을 만드는 곳. 2) 레벨이 빌드 된 곳. 3) 게임.스프라이트를 playerPref에 저장하는 방법은 무엇입니까?

내 팀에는 각 팀원별로 5 가지 선택 사항이 있습니다. 플레이어를 설정 한 다음 다른 플레이어에서 해당 플레이어의 Image 또는 Sprite을 다른 장면에서 불러 오려고합니다.

나는 playerPref이 작동한다고 생각했지만, 이것은 옵션이 아닌 것처럼 보입니다.

한 장면에서 이미지를 저장하고 다른 장면에서 이미지를 불러오는 좋은 방법은 무엇입니까?

답변

0

당신은 PlayerPrefs에 스프라이트의 이름을 저장 한 다음 자원에서로드 수 : Resources.Load(spriteName);

+0

그래서 문자열로 저장 하시겠습니까? –

+0

@TimCooley 예. – Iggy

+0

GetComponentInChildren () .name 이제 스프라이트의 이름을 캡처하려고합니다. –

2

당신은 playerprefs에서 64 기수로 스프라이트의 질감을 저장할 수 있습니다, 당신은 저장 질감에서 스프라이트를 만들 수 있습니다. 그러나 텍스처는 읽기/쓰기가 가능해야하며 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

관련 문제