Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lean Loader - Load from the web or a cache

Discussion in 'Assets and Asset Store' started by dentedpixel, Nov 4, 2013.

  1. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    $header.png

    Asset Store $10

    Load assets from either the web or from a cached backup. Great for times when the user is offline or just to speed up performance!

    - Load images, sound, and text data (XML etc)
    - Minimal coding required
    - Simplifies tasks such as posting data to a server.
    - Set objects to cache and specify an expiration date.

    Examples provided in C# and Javascript.
    View Full Documentation
    Because PlayerPrefs is limited to 1mb of data on the Web Player, caching isn’t as useful on this platform.
     
    Last edited: Nov 12, 2013
  2. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
  3. spartan

    spartan

    Joined:
    Mar 10, 2010
    Posts:
    174
    Where do you store the assets cache?
     
  4. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    The assets cache in the PlayerPrefs area as a string (best for cross-compatibility across different devices).
     
  5. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Wouldn't PlayerPref be limited to very small files then?
     
  6. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    It can load pretty substantial files, I have loaded files up to 1.5mb or so (it should be able to do larger, I just have not tested it). If you are deploying to the Web Player the total storage for PlayerPrefs is limited to 1mb, so it is not optimal on that platform for large files.
     
  7. Doeko

    Doeko

    Joined:
    Aug 8, 2009
    Posts:
    17
    Do textures take up their non-compressed size in the game's memory (RAM not vRAM)? As that is usually the case with loading image data at runtime.
     
  8. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    They are stored in a PNG format and once they are loaded in to memory they are in the default format for textures according to the constructor: http://docs.unity3d.com/Documentation/ScriptReference/Texture2D-ctor.html

    I could give you finer control over the format used on the images if that is necessary.

    The images once loaded should only exist in VRAM, unless you keep a reference to the texture yourself which would keep it from being garbage collected...
     
  9. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Nice product. Bookmarked. Have you thinking about a saving/upload tool?
     
  10. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Thanks ZJP :)

    I hadn't really thought of that, but I have done similar things in the past. Did you mean by a saving/upload tool, saving to a server, or save to the local disk an image/music/text ? I think those situations might be too specialized to bother trying to write an all encompassing wrapper :) but there might be a good way to do it...
     
  11. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    I've thinking about the opposite of your tool.
    - Upload (to a server) images, sound, and text data (XML etc)
    - Minimal coding required
    ... :D
     
  12. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    LeanLoader actually gives you an easy way to post data to a server, so in a way it kind of accomplishes this. But it would require you to convert it to text and then back to whatever asset it is on the server-side.

    It's a good idea, but since everyone has a different favorite server-side technology (php, ruby, .net), it would be pretty tough to find the right one to implement :)

    Maybe the best way would be to have more helpers on the front-end for serializing an asset. That could be easily added, let me know if anybody needs it...
     
  13. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    LeanLoader now supports JSON parsing with it's own built-in JSON parser! You can now load JSON directly from a server and have it be automatically returned as a JSON object ready to access the data like:
    Code (CSharp):
    1. function Start(){
    2.    LeanLoader.load("http://dentedpixel.com/assets/advertising_json.php", new LLOptions().setOnLoad(onAdvertisingJSONLoaded);
    3. }
    4.  
    5. void onAdvertisingJSONLoaded( LeanJSON json ){
    6.    Debug.Log("link one:"+json.Array("ads")[0]["link"]);
    7. }
    Other features that have been added are:
    • Automaticaly download Facebook profile images without having to manually parse through the returned data
    • Fix for blurry images when an images has been cached.
    • More code samples...
     
  14. Tmtakala

    Tmtakala

    Joined:
    Mar 12, 2014
    Posts:
    16
    I'm curious about LeanLoader, and whether it can help me:

    A. Can I use it to read files from the disk, similar to what can already be done like this:
    Code (CSharp):
    1. www = new WWW("file://C:/images/image.png");
    B. In this offline case, is LeanLoader any leaner than the above method, and in which way?

    C. I have a very particular case where I'm loading a new offline png image on every Update() into a Texture2D that needs to be in RGBA32 format, but unfortunately png-files loaded with LoadImageIntoTexture() come out in ARGB32-format by default:
    Code (CSharp):
    1. // Setting up a texture in RGBA32-format (in vain)
    2. Texture2D image = new Texture2D(width, height, TextureFormat.RGBA32, mipMap, linear);
    3. www = new WWW("file://C:/images/image.png");
    4. www.LoadImageIntoTexture(image); // image.format (read-only) becomes now ARGB32, but I need it in RGBA32
    Is there anyway I can load a png-file into Texture2D in run-time so that it assumes RGBA32-format?

    If by any chance LeanLoader could help with issue C., or if you have any suggestions that lead to solving this problem in a practical way, I'm more than happy to purchase LeanLoader.
     
  15. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi Tmtakala,

    For a very basic loading it may not be any leaner than that one line, but it makes things like loading a cached version quite simple. Here is an example of basic cached image loading:

    Code (JavaScript):
    1. private var imageCached:Texture2D;
    2.  
    3. function Start () {
    4.     LeanLoader.load("http://dentedpixel.com/assets/Monkeyshines.png", LLOptions().setOnLoad(onImageLoaded).setUseFile(true).setUseCache(true).setCacheLife(60*5));
    5. }
    6.  
    7. function OnGUI(){
    8.     if(imageCached)
    9.         GUI.DrawTexture( new Rect(0.0f,0.0f,Screen.width*0.2f,Screen.width*0.2f), imageCached);
    10. }
    11.  
    12. private function onImageLoaded( tex:Texture2D ){
    13.     Debug.Log("Your image texture ready to use! :"+tex);
    14.     imageCached = tex;
    15. }
    Right now images are loaded by default in the TextureFormat.RGB24, but I am planning on making this an option, so you can specify whatever format you wish...
     
  16. Tmtakala

    Tmtakala

    Joined:
    Mar 12, 2014
    Posts:
    16
    Thanks for the answer!
    Just to clarify, LeanLoader loads PNG files into 2D textures as ARGB32 right? That seems to be the PNG default in Unity, at least for the following function:
    http://docs.unity3d.com/ScriptReference/WWW.LoadImageIntoTexture.html
     
  17. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey Tmtakala,
    That is correct ARGB32 is the default. Although I am making a slight optimization for the next release so that if it is a JPG it is saved as RGB24 (which is the default for JPGs because they don't have alpha). The next version will be released some time this week...
     
  18. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Cool Asset,

    Does this support loading of other types of unity assets ?
     
  19. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey Rocki!

    Yes it supports Images/JSON/Audio/XML and just general data...
     
  20. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
  21. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Let me know if you can think of anything else of it to load :D . Those seem liked the most important things to cover though.
     
  22. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Here is a script for loading a skybox at runtime:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SkyboxCS : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.        
    9.         Material mat = this.gameObject.GetComponent<Skybox>().material;
    10.  
    11.         LeanLoader.load("http://dentedpixel.com/assets/alpine_sky/alpine_front.jpg", new LLOptions().setUseCache(true).setOnLoad( ( Texture2D tex )=>{
    12.             mat.SetTexture("_FrontTex", tex);
    13.         }));
    14.         LeanLoader.load("http://dentedpixel.com/assets/alpine_sky/alpine_back.jpg", new LLOptions().setOnLoad( ( Texture2D tex )=>{
    15.             mat.SetTexture("_BackTex", tex);
    16.         }));
    17.         LeanLoader.load("http://dentedpixel.com/assets/alpine_sky/alpine_left.jpg", new LLOptions().setOnLoad( ( Texture2D tex )=>{
    18.             mat.SetTexture("_LeftTex", tex);
    19.         }));
    20.         LeanLoader.load("http://dentedpixel.com/assets/alpine_sky/alpine_right.jpg", new LLOptions().setOnLoad( ( Texture2D tex )=>{
    21.             mat.SetTexture("_RightTex", tex);
    22.         }));
    23.         LeanLoader.load("http://dentedpixel.com/assets/alpine_sky/alpine_top.jpg", new LLOptions().setOnLoad( ( Texture2D tex )=>{
    24.             mat.SetTexture("_UpTex", tex);
    25.         }));
    26.     }
    27.  
    28. }
    29.  
    Just make sure you have the Skybox component attached to the camera!
     
    idurvesh likes this.
  23. URGr8

    URGr8

    Joined:
    Sep 10, 2012
    Posts:
    23
    This is nice, but does it support the new IOS 9 and apple requirements for mobile to use HTTPS with the NSAppTransportSecurity? would it be hard to fix it for that?
     
  24. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey there URGr8,
    It does support HTTPS, but how I think Unity is still working on how they can support NSAppTransportSecurity with publishing. I think the current workaround is just to disable NSAppTransportSecurity which, I guess is not a great workaround...
     
  25. calinu4

    calinu4

    Joined:
    Nov 29, 2015
    Posts:
    5
    I'm working on a inventory application. Mainly is a list of products, and when selected a product. It will display a new Panel displaying title, description, price and one image. Is the asset able to read all that data from mysql table and save it into a json file on the device for persistent data when offline and retrieve new updates when online?
     
  26. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi Calinu4,

    Lean Loader does not read SQL data directly, but if you have something server-side that spits out JSON from the SQL data, it should be able to load it and have it available for offline usage (and then update it with something fresh when you are online again).
     
  27. calinu4

    calinu4

    Joined:
    Nov 29, 2015
    Posts:
    5
    So you are saying if I have a JSON file in my public html folder of my website containing the sql data on my server, I can read it with your asset and kind of save it as a json file on my device.Do you know if I can save images in json file using your loader, and read from that json file?
     
  28. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    That is correct, and yes it can load images (jpg or png) and cache those for later use offline similar to the JSON.

    You could either have the JSON in your public html folder, or have it created on the fly with a server-side programming language like PHP.
     
  29. calinu4

    calinu4

    Joined:
    Nov 29, 2015
    Posts:
    5
    Another question about the Lean Loader, I will pay extra if you can customize it. I might need to display a webpage content into the app itself, whenever I update the webpage, i don't need to update the page again, and also keep a cache file of the webpage for offline usage. Would that be possible?
     
  30. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Unfortunately there
    Unfortunately there is no built-in way to display a webpage within Unity. There are text fields that can display some rich text however http://docs.unity3d.com/Manual/StyledText.html , so you could have a page with HTML-ish like text that was downloaded from a webserver and cached for offline usage. I could code an example like that pretty easily into LeanLoader but I am not sure if it would meet your needs...
     
  31. calinu4

    calinu4

    Joined:
    Nov 29, 2015
    Posts:
    5
    I manage to read image on at a time, but is possible to read the whole folder. And split the byte array to get all the images separately?
     
  32. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    You would need to do some server-side processing for this, have a process that spits out all the objects in the directory into JSON then you could download them one at a time with Lean Loader. Here is a method to do it with php: http://php.net/manual/en/function.scandir.php
     
  33. PIGSSS-GAMES

    PIGSSS-GAMES

    Joined:
    Jul 2, 2014
    Posts:
    8
    How can I stop loading mid-way?

    Have issue where scene changed/object got destroyed before loading finish and then callback try to refer to deleted object.
     
  34. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    There is not currently a method for that, but I can add one.

    Could you post the specific line-number or portion that it fails at? I would rather fix it at the failing point, so it can fail silently instead of having to cancel whenever exiting a scene.
     
  35. houzy

    houzy

    Joined:
    Oct 24, 2015
    Posts:
    8
    Hi, I have purchased Lean Loader via Unity asset store, and I use it on Android and IOS device for downloading image from url. When I called LeanLoader.load function for the first time, my Android device would suck for a little while, and for the second time, it had no such problem. Please solve it, thanks.
     
  36. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Could you try forcing the device to use the file writing version of the cache? Like:

    LeanLoader.load("http://dentedpixel.com/assets/Monkeyshines.png", LLOptions().setOnLoad(onImageLoaded).setUseFile(true).setUseCache(true).setCacheLife(60*5));

    I think that might be where your performance issues might be coming from.
     
  37. houzy

    houzy

    Joined:
    Oct 24, 2015
    Posts:
    8
    Thanks for your reply. OK, setUseFile(true) really improves the performance, but still sucks for a very little while. Maybe it is because the image is big, the size is 1.5Mb.
     
  38. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Yeah that is a pretty large file, I am sending you an updated version that makes the performance better (still a few bottlenecks that might be hard to get it silky smooth with that large of a file, but I think this is an improvement)
     
  39. houzy

    houzy

    Joined:
    Oct 24, 2015
    Posts:
    8
    Wow!! You are very very nice!! Thank you very much, buddy.
     
  40. houzy

    houzy

    Joined:
    Oct 24, 2015
    Posts:
    8
    Unity reports an error:
    Assets/Plugins/LeanLoader.cs(1039,69): error CS1525: Unexpected symbol `.', expecting `)', `,', `;', `[', or `='
     
  41. adi4x

    adi4x

    Joined:
    Jan 9, 2014
    Posts:
    7
    Hello

    With 5.3.5f1 i have these errors (only on windows store build)

    Assets/Tools/LeanLoader/Examples/LoadingCS.cs(45,123): error CS1502: The best overloaded method match for `LLOptions.setOnLoadParam(System.Collections.Generic.Dictionary<string,object>)' has some invalid arguments

    Assets/Tools/LeanLoader/Examples/LoadingCS.cs(45,123): error CS1503: Argument `#1' cannot convert `System.Collections.Hashtable' expression to type `System.Collections.Generic.Dictionary<string,object>'

    Assets/Tools/LeanLoader/Examples/LoadingCS.cs(45,36): error CS1502: The best overloaded method match for `LeanLoader.load(string, LLOptions)' has some invalid arguments

    Assets/Tools/LeanLoader/Examples/LoadingCS.cs(45,36): error CS1503: Argument `#2' cannot convert `object' expression to type `LLOptions'

    Assets/Tools/LeanLoader/Examples/LoadingJS.js(18,161): BCE0017: The best overload for the method 'LLOptions.setPostParams(System.Collections.Generic.Dictionary.<String, Object>)' is not compatible with the argument list '(System.Collections.Hashtable)'.

    Assets/Tools/LeanLoader/Examples/LoadingJS.js(40,125): BCE0017: The best overload for the method 'LLOptions.setOnLoadParam(System.Collections.Generic.Dictionary.<String, Object>)' is not compatible with the argument list '(Boo.Lang.Hash)'.


    Any help ?
     
    Last edited: Jun 4, 2016
  42. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    427
    Before I buy, can I check that this does what I need?
    1. Load all the images listed in a json file, specifically one dynamically created on the server (e.g. http://baroquedub.co.uk/unity/textures/fractals.php ... NB. I'm assuming the JSON parser will correctly parse the escaped forward slashes in the filepath urls?)
    2. On next load (or if offline) images don't need to be reloaded from the web.
    Also.. I'm confused by comments in this forum re. caching size limit (using PlayerPrefs) My image files are large (each on is at least 2.5MB, and I'm hoping to also eventually load videos) Is this going to be a problem? Is there a limit to the size of each asset that is remotely loaded? Or a total limit?

    Many thanks
     
  43. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey Jerome,
    Sorry for the slow response, it's been a super busy week at work.

    1. Yup, it does escape those slashes, I did a test.

    2. At the moment, LeanLoader is not setup well to handle very large files, but I am working on an update for that (but if you need something immediately, you may want to consider other options)

    Thanks!
    Russ
     
  44. Wimouss21

    Wimouss21

    Joined:
    Aug 11, 2016
    Posts:
    3
    Hello,

    Awesome job with this lean loader. It's super useful for adding dynamic content to a game.

    But I have an issue when I load an image during the game session the main thread freeze when loading the image.
    I tried your suggestion using setUseFile(true) but that still freeze and the image returned is just empty (empty texture)

    Do you have any idea how to fix this kind of issue? Or do you have an async / coroutine / threaded version of the load function?

    Thanks

    Btw thanks for the Unity 5.4 updates!
     
    Last edited: Aug 11, 2016
  45. Boegi

    Boegi

    Joined:
    Dec 6, 2014
    Posts:
    19
    There is a error/typo in your latest json feed. http://dentedpixel.com/assets/test_json.txt
    It returns ...
    "texture":"ttp://dentedpixel.com/assets/PrincessPiano.jpg"
    (h from http is missing)

    This causes the LoadJson Example to raise an error in Unity.

    I fixed this typo and saved the file on my domain. Which lead to the following questions:

    1) How can I check if the files in the feed really exist?
    2) While hosting your feedfile on my server - I got another error.

    --
    Error: Rejected because no crossdomain.xml policy file was found while loading:--myserver---/test_json.txt
    UnityEngine.Debug:Log(Object)
    <sendData>c__Iterator2:MoveNext() (at Assets/Plugins/LeanLoader.cs:1016)
    --

    How to handle this? Is there a workaround to use files from servers without a crossdomain.xml?

    Thanks for your reply
     
  46. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi there! I have been working on trying to improve performance. I will PM you what I have so far.
     
  47. SpiralConDave

    SpiralConDave

    Joined:
    Dec 29, 2014
    Posts:
    37
    I'm making a movie veiwer. I'd like to cache movies up to 2 gigs in size. Possible?
     
  48. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    I don't think this would be the best for this, right now it is best setup for a small range of XML/Json/Image/Sound file types, so it's not really setup for movie files. I would like to make it work for all file types in an update, but at the moment, it might be easier to roll your own solution.
     
  49. Ruud3DV

    Ruud3DV

    Joined:
    Jan 25, 2016
    Posts:
    7
    This is a great plugin, thanks!

    I'm trying to get some better performance with it when loading a lot of large imagefiles on Android, without using the caching. When downloading for example 12 files of 1,5Mb each (in this case 6 sides of a panorama cube, for left and right eye), would it be better to wait with loading the next image until the previous image is loaded, or does LeanLoader take care of the queueing itself in an efficient way already?

    Right now I'm simply performing 12 LeanLoader.load actions at once, it works ok, but the app is unresponsive for a short while. I'm not sure wether that's the 12 requests to the webserver being processed or the processing of the loaded textures to apply them as texture in a material.

    I read above that you've been working on improving performance, will there be an update soonish?
     
  50. Ruud3DV

    Ruud3DV

    Joined:
    Jan 25, 2016
    Posts:
    7
    Is it possible to add a parameter to the setOnLoad action? The first parameter is obviously the texture itself, but I'd like to add a number or string to identify the image when loaded.

    When loading 12 images, I'd like to keep track of when a specific image is loaded. Right now I'm using 12 pseudo functions (imageProcess0, imageProcess1 etc) that simply call the real function with the added parameter describing the texture, but I was hoping that could be done more efficient. Or maybe I'm just missing some C# basics with which this is already possible, I'm not that experienced yet with it.

    [EDIT] Nevermind, found your example on how to do it, it was lack of C# basics from me...
    setOnLoad((Texture2D tex)=>{ProcessImage (0, tex);})
     
    Last edited: Oct 11, 2016