Hi,
I’ve been testing resource management using asset bundles in Unity 5.4.5 and it’s not working as I hoped and I can’t change unity version.
I’ve created two particle systems using one prefab each. They use the same texture and when I spawn a bunch in a scene from code or by dragging them in the scene the texture count remains one. So that’s fine.
Now to the part where it get’s a bit dangerous.
I export these systems prefabs using:
BuildPipeline.BuildAssetBundles(bundlePath, buildMap, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.ForceRebuildAssetBundle, EditorUserBuildSettings.activeBuildTarget);
Then load the asset bundles using:
AssetBundle bundle = AssetBundle.LoadFromFile(“…/StreamingAssets/TestSystem.unity3d”);
aaaa = bundle.LoadAsset(“TestSystem”);
bundle = AssetBundle.LoadFromFile(“…/StreamingAssets/TestSystem2.unity3d”);
bbbb = bundle.LoadAsset(“TestSystem2”);
Now the profiler shows me there’s 3 copies of the texture in memory. One for the systems added in the scene directly, one for the first loaded asset bundle system and one for the second.
Is this the intended case or could you set a flag or something to make unity smarter when using resources?
To make things worse you can use
bundle.Unload(false);
to free up the space used by the bundle and still use the resources, that’s also fine. But if you’re not careful you will get a new copy of the texture if you load the same asset bundle and object once more.
I feel like this is something that should be handled by Unity by default. I mean, if the texture is not marked as read/writable it feels kinda strange that there would be a bunch of copies. In the case with the Unload(false) I can understand that it may be a way of keeping the textures safe in case you use bundle.Unload(true). But even then I feel like it’s not optimal.
I Haven’t tried this with other resources, but I guess it would show the same thing.
Would be nice if someone could shed some light on this.