Search Unity

Loading and Assigning multiple textures at once

Discussion in 'Scripting' started by unitynoob24, Dec 10, 2018.

  1. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Hey guys!

    So I am working on a small project where I am constructing a book from a bunch of page textures which I have now in my resources directory. Prior to this I just had public lists in Unity and drag / dropped each page and this worked perfectly. But I wanted to clean up the project and remove having to drag and drop in the editor and let the chapter build itself just to speed up having to drag and drop each chapter.

    So basically, when I have the game create a chapter now, I stuff each texture from xml into the sprite lists which is what I was doing manually before, only now I have a good chunk of hang time when the chapter is building. I know that is because I am using Resources.Load but was wondering if there was a better way to do this that would not involve drag and drop in the editor and still allow me to build these chapters on the fly. I am aware of Streaming Assets however, I do not want these pages to be visible in the final build.

    Code (csharp):
    1.  
    2.  
    3. public void BuildChapter(int val)
    4. {
    5.  
    6.  bookXML = xDoc.Root.Elements("Chapters").Descendants("Chapter").Where
    7.             (e => (int)e.Attribute("Number") == val).Select(n => n).FirstOrDefault();
    8.  
    9.         for(int i = 0; i < bookXML.Descendants("Pages").Descendants("Page").Count(); i ++)
    10.         {
    11.             string tempStr = bookXML.Descendants("Pages").Descendants("Page").ElementAt(i).Value.ToString();
    12.             CHAPTER_PAGES.Add(Resources.Load<Sprite>(tempStr));
    13.         }
    14. }
    15.  
    Thanks guys!
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    You can use Resource.LoadAsync to load them while not having the game freeze up. No matter what its going to take time to upload the textures to the video memory, its a matter of when you want that to happen. I would recommend using Asset Bundles in this situation so you can load the bundle async and get the whole chapter in one go. Then once your done with the chapter you can unload the textures from the video memory using the asset bundle.
     
    unitynoob24 likes this.
  3. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Hm I was looking into LoadAsync as well as Asset Bundling. I wonder though, if I switched to Asset Bundle, would I still need to LoadAsync from the AB? If that is the case, I would rather just load async from the Resources that I already have set up. I am wondering then, would it be better to just load the entire book with a preloading screen then all the chapters are there in memory and I should be able to 'build' them to the screen super fast (Like I was doing before when they were drag and dropped) The only other question is.. how would I go about loading the entire XML at once? Just Load all async or something?

    Follow up question. There is 0 lag if the list is created via drag and drop in the editor. But when I do it like I have it now I have noticeable like 1-3 second lag. Isn't this doing the exact same thing? It would be nice to have it act as though I had it drag and dropped in the editor and just work super fast like before.
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Yes would work the same as loading the texture.

    This would also remove them from memory when changing chapters/scenes.

    You can load all async at once and then wait for all of them to be loaded, which is what the asset bundle would do for you only much easier. You would then use the XML to setup the pages gabbing the textures from the asset bundle.

    In the end it seems that the chapter/scene idea is the easiest. You will probably spend more time with a resource loading system than just using the already built in scene system.
     
    unitynoob24 likes this.
  5. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Alright, so I got everything working great now with an AssetBundle! First time using them really. They are growing on me.. extremely fast build time is a huge perk!

    So anyway.. still something that kind of rubs me the wrong way lol Is there anyway to avoid using Streaming Assets? Like right now I have it so my bundle is loaded from file which is at StreamingAssets>AssetBundle>book - Then once I do the build I can navigate to this and view the manifest and everything. This is not something that I would prefer to have public. Is there anything I could do to address this?

    Thanks!
     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I have no idea how to circumvent people from being able to view the files even without them being in streaming assets. I've thought about this for quite some time. Trying to prevent people from tampering with save files and the like. I have come to the conclusion that it's pretty much pointless. Encryption is breakable, it used to take months to decode 8 bit encryption now it takes seconds to do 256 bit. No matter what you do to stop someone, there is always a way around it. You can hack anything, if you can see the binary you can see what it is. Let people ruin the experience for them selves. The real gamer's won't bother doing anything other than playing the game.

    Now all this changes with online of course, but then you can keep all the files on a server then do a www and grab the bundles from the server as needed/unlocked.
     
    unitynoob24 likes this.
  7. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Yea makes sense to me. I was just double checking I guess if there was something pre-baked into Unity that I was overlooking. That is pretty much my outlook on pirates as well. I launched my first big game in 2015 and pretty much the day of it was on TPB and I spent way too much time on YouTube reporting 'How To Torrent UnityNoob24's Game' and overall it was just a massive time sink - and the moral of the story was; true fans will support you and there will always be people who get their kicks from stealing content and those aren't fans you really want to begin with. So why waste valuable development time attempting to combat them - especially when often times the more 'fail safes' you implement, the more bragging rights they get when they do ultimately steal your products.
     
    Chris-Trueman likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    This!

    and this!

    Bravo gentlemen for not wasting your efforts trying to do the impossible. There are more script kiddies out there than you will ever be able to defeat from breaking into your game's data. Even one motivated 11-year-old has more time on his hands than you ever will, and he will break into your game data "just because."

    As for using Asset Bundles, they are awesome. They are especially awesome if you have a story (hm, sounds like you do!) and it might want to grow unbounded, with additional chapters, sections, updates, etc. If you host it all off the device on your own server, you can update your content without updating the actual game binaries out in the wild.
     
    unitynoob24 and Chris-Trueman like this.