Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Loading an image from www, can i save it in playerprefs ?

Discussion in 'Scripting' started by jessica1986, Dec 6, 2013.

  1. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    If not then can i save it locally by any means ?
     
  2. DiogoLeao

    DiogoLeao

    Joined:
    Oct 3, 2013
    Posts:
    28
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4.  
    5. public class Test : MonoBehaviour {
    6.     public IEnumerator Start () {
    7.         WWW loader = new WWW("http://unity3d.com/sites/default/files/frontpage/learn.jpg");
    8.         yield return loader;
    9.         string path = string.Format("{0}/image.png", Application.persistentDataPath);
    10.         Debug.Log(path);
    11.         File.WriteAllBytes(path, loader.texture.EncodeToPNG());
    12.     }
    13. }
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You can only save strings in the player prefs. That means you have to convert the byte array to a string before you can save. As you load, you do the other way. Auntie google knows how to do both of them.
    Take care because there is a size limit: