Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Daxx2692 · Jun 05, 2020 at 08:01 AM · texturesloading during runtimeloading textures

Loading multiple textures from the files ystem and using them as textures.

Hello

I'm trying to figure out how to load a lot of small pngs from the file system without consuming all the available RAM.

The images will be used in at least two ways:

  • Character creation screen, where the player can change the clothing, face & body. With multiple characters visible at the same time that can be edited.

  • NPC generation, taking the textures from the list and displaying them on a prefab via RawImages

The problem:

Currently, I'm trying to load 10,915 images with most of them being 1kb, while the biggest ones are around 3kb. All images considered, it's about 311 MB worth of disk space.

I've tried using WWW, UnityWebRequestTexture and File.ReadAllBytes but all of them yield similar results.

Loading all the images at the start of a scene, the memory usage balloons to around 4gb, and using Unity's profiler, about 3.5gb are used by the textures.

My, question is, how do i go from 311MB in disk to over 10x the memory usage at 3.5gb in RAM and Is there a way to optimize this?

This is the code I'm using to load and test them.

  using System.Collections.Generic;
  using System.IO;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class ImageTest : MonoBehaviour
  {
      // Testing elements
      public RawImage image; // Where to display the images
      public float updatePawnsEverySecs = 5.0f; // How long between updates
      private float _timeLeft = 0f; // How long before we have to update the texture
      
      // Stores the images for later use
      private List<Texture2D> _texture2Ds = new List<Texture2D>();
  
      
      private void Update()
      {
          if (_timeLeft <= Time.deltaTime)
          {
              // Time's up! Update the pawn
              image.texture = _texture2Ds[Random.Range(0, _texture2Ds.Count)];
              // Reset the timer
              _timeLeft = updatePawnsEverySecs;
              Debug.Log("Updated texture");
          }
          else
          {
              // update the timer
              _timeLeft -= Time.deltaTime;
          }
      }
      
      private void Awake()
      {
          int _totalImages = 10915; // Number of images in the folder for testing purposes
          string path = "E:/Unity/Projects/CharacterCreation/Data/Img/"; // The path were the images are stored 
          
          // Load all images into memory
          for (int i = 0; i < _totalImages; ++i)
          {
              
              Texture2D tex2d = LoadTexture(path + i + ".png"); // Image names are in the form of 0.png, 1.png, 2.png up to 10915.png 
              if (tex2d == null)
              {
                  Debug.Log("No texture loaded");
              }
              else
              {
                  _texture2Ds.Add(tex2d);
                  //Destroy(tex2d); // This lines makes it so that the memory is freed, but the textures are also null
              }
          }
      }
      // Taken from: https://forum.unity.com/threads/generating-sprites-dynamically-from-png-or-jpeg-files-in-c.343735/#post-4068442
      public Texture2D LoadTexture(string filePath)
      {
          // Load a PNG or JPG file from disk to a Texture2D
          // Returns null if load fails
  
          if (File.Exists(filePath))
          {
              var fileData = File.ReadAllBytes(filePath);
              var tex2D = new Texture2D(2, 2);
              if (tex2D.LoadImage(fileData))  // Load the imagedata into the texture (size is set automatically)
                  return tex2D;               // If data = readable -> return texture
          }
  
          return null; // Return null if load failed
      }
      
  }

I'm skipping Unity's resources and assets since this images should come from mods and putting a couple of images inside a folder is far easier.

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

0 Replies

· Add your reply
  • Sort: 

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

127 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 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

Snapping image and display it on next scene 2 Answers

Loading in textures dynamically without using Resources 1 Answer

Why do my imported textures look a lot darker with odd shadows? 2 Answers

Missing half of textures after Sketchup export 0 Answers

Is there a way to re-import all assets? 0 Answers


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