Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by rmail2006 · May 30, 2017 at 06:06 PM · serverassetsresourcesresource.load

DLC - Help downloading assets and saving locally

Hi All,

I'm currently working on a game that is server oriented. The game downloads it save file from the server as well as telling the server about each action.

The next part of the game is to be able to download new assets e.g. New player texture.

I currently have my textures in the Resource folder and I use Resource.Load() to load them at runtime.

Is there anyway to download a new image and save it in the resource folder?

Also is it possible to download the new image, and have it stored locally, so next time the game is loaded it won't need downloading again?

Thanks Thomas

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by rmail2006 · Jun 01, 2017 at 10:44 PM

I figured out a better way to do this.

I couldn't get an image into the resources so I figured another way around it.

First thing I do is check if the image exists in the Resources folder, if that doesn't exist i then use File.exists to check if the image is in the persistantDataPath subdirectory, if not then use a WWW to download it from my server, and then File.WriteAllBytes to write it to the persistantDataPath.

So from now on, when the game loads it will look in the persistantDataPath and it will load it without checking my server.

It works for what I need and after testing multiple times, the game loads at similar times with the image in the resource folder compared to downloading it from the server.


EXAMPLE

jn is a json node

 m.image = Resources.Load<Sprite>("Sprites/" + Path.GetFileNameWithoutExtension(jn["IMG"]));
 
             if (m.image == null)
             {
                 if (!File.Exists(Application.persistentDataPath + "/Sprites/" + jn["IMG"]))
                 {
                     string imageURL = "http://localhost/game/Sprites/";
                     www = new WWW(imageURL + jn["IMG"]);
                     yield return www;
 
                     byte[] bytes = www.texture.EncodeToPNG();
                     File.WriteAllBytes(Application.persistentDataPath + "/Sprites/" + jn["IMG"], bytes);
                     Debug.Log("Downloaded Sprite");
                 }
                 byte[] data = File.ReadAllBytes(Application.persistentDataPath + "/Sprites/" + jn["IMG"]);
                 Texture2D tex = new Texture2D(450, 500, TextureFormat.ARGB32, false);
                 tex.LoadImage(data);
                 Sprite s = Sprite.Create(tex, new Rect(0, 0, 450, 500), new Vector2(0, 0));
                 m.image = s;
             }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Needed objects destroyed after deserialization 0 Answers

How to make an exetuable file lighter 1 Answer

Loading Assets from server on as needed basis. 0 Answers

Warning:Trying to move asset to location it came from...[Please Help] 0 Answers

How do you import non-hierarcy assets from an Asset Bundle? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges