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

Save Load Sprite Possible?

Discussion in '2D' started by Faestus, Dec 4, 2014.

  1. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Is it possible to load a sprite file from a different file except resources?

    I need a way to save and load sprites while the application is runnig.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Its possible, you'd have to access the file system and maybe like load in a byte array and decompress it from png or something
     
  3. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Is there an editor or anything like that?
     
  4. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    you have to do it in a script.
     
  5. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Is there a tutorial on that?
     
  6. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    there are many threads talking about loading files from the filesystem
     
  7. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Would this work?
    Code (CSharp):
    1. void LoadTexture(string FileName)
    2.     {
    3.         string url = "file://"+Application.dataPath+FileName;
    4.         WWW www = new WWW(url);
    5.      
    6.         Texture2D texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
    7.         if(www.isDone)
    8.         {
    9.             texture = www.texture;
    10.         }
    11.  
    12.        Sprite.Create (texture,new Rect(0, 0, SpriteWidth,SpriteHeight),new Vector2(0.5f,0.5f));
    13.     }
    If yes, where would I need to put my texture for the file to load properly?
     
  8. DryreL

    DryreL

    Joined:
    Feb 23, 2020
    Posts:
    49
    Does PlayerPrefs save & load the sprites?
     
  9. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
  10. Uri1990

    Uri1990

    Joined:
    Mar 20, 2015
    Posts:
    1
    DryreL yes you can by doing this workaround of saving the sprite as a string. Just loading it is the tricky part