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 /
avatar image
0
Question by U_Guendeli · Jul 01, 2019 at 06:34 AM · memoryasset bundles

Estimate Assetbundle memory Size

I'm working on a small tool on top of the Asset bundle browser to estimate how much size in RAM a bundle will take so I can put a hard limit / validation before a bundle is submitted to version control by artists.

My approach is to:

  • Load a bundle using bundle.LoadAllAssets()

  • Make a serialized object from bundle

  • Access the serialized property "m_PreloadTable"

  • loop through every objectReferenceValue

  • Get it Type then size using Profiler.GetRuntimeMemorySizeLong(object)

  • Adding that to a Dictionnary

Here is the full Editor Class if you want to do Tests:

 [CustomEditor(typeof(LoadBundle))] public class LoadBundleEditor: Editor {
     public string BundleName;

 private AssetBundle _bundle;
 private GameObject _prefab;
 private Dictionary<Type, float> TotalMemory = new Dictionary<Type, float>();
 List<string> PreloadedObjects = new List<string>();
 public override void OnInspectorGUI()
 {
     DrawDefaultInspector();

     if (GUILayout.Button("Load Bundle"))
     {
         LoadBundle loader = (LoadBundle)target;
         EditorCoroutineUtility.StartCoroutine(LoadPrefabRequest(loader.BundleName + ".bundle"), this);
     }
 }


 public IEnumerator LoadPrefabRequest(string bundleStr)
 {
     if (_bundle != null)
     {
         _bundle.Unload(true);
         yield return null;
     }

     TotalMemory.Clear();
     PreloadedObjects.Clear();
     AssetBundle bundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, bundleStr));

     bundle.LoadAllAssets();
     _bundle = bundle;

     yield return null;
     float total = 0;
     SerializedObject sBundle = new SerializedObject(bundle);


     SerializedProperty allAssets = sBundle.FindProperty("m_PreloadTable");
     foreach (SerializedProperty asset in allAssets)
     {
         if (asset.objectReferenceValue != null)
         {
             float size = GetAssetSize(asset, asset.objectReferenceValue.GetType());
             if (PreloadedObjects.Contains(asset.objectReferenceValue.name + size))
                 continue;
             if (TotalMemory.ContainsKey(asset.objectReferenceValue.GetType()))
             {
                 TotalMemory[asset.objectReferenceValue.GetType()] += size;
             }
             else
             {
                 TotalMemory.Add(asset.objectReferenceValue.GetType(), size);
             }
             PreloadedObjects.Add(asset.objectReferenceValue.name + size);
         }
     }

     foreach (KeyValuePair<Type, float> key in TotalMemory)
     {
         total += key.Value;
         Debug.Log(key.Key + "<color=blue> memory should be: </color>" + key.Value / 1048576f);
     }

     Debug.Log("<color=green> Total memory should be: </color>" + (total / 1048576f));
 }

 float GetAssetSize(SerializedProperty s, Type t)
 {
     float mem = Profiler.GetRuntimeMemorySizeLong(s.objectReferenceValue);
     

     return mem;
 }
 public float GetTextureSize(SerializedProperty s)
 {
     float mem = Profiler.GetRuntimeMemorySizeLong((Texture2D)s.objectReferenceValue);
     Debug.Log("<color=cyan> Texture size of " + s.objectReferenceValue.name + " is </color>" + mem);
     return mem;
 }
 }


In the end I show the memory of each Type as well as the Total Memory of that bundle.

I do a comparison by loading an AssetBundle in an empty scene project in Android and taking a Memory snapshot from the profiler. then I compare that data with my Editor tool by loading the same bundle (and also checking an editor memory snapshot to see file sizes).

And the values are different, In the Editor it feels like if i'm loading a Desktop bundle and not an Android one. Although the asset files are android ones, and the test project project is set to Android.

Do you have any idea how Unity behaves under the hood concerning assetbundles loading in the editor ?

Thanks.

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

110 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

Related Questions

AudioManager memory usage on iOS when using Asset Bundles 3 Answers

Asset bundles using double memory! 1 Answer

AssetBundles, memory, and loading 0 Answers

How to import the object from server to unity 2 Answers

How do "QualitySettings" affect Asset Bundles? 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