2

I've been doing some research on unity AssetBundle and Resources class but so far I've not been able to find a way to unload resources at build time. This is not intended for Memory management but instead for Build/apk size.

For example, I have a LocalizedAudio class: were I have a FR sound and a US sound set to an instance of this class. I need to find a way to tell Unity that I want only the FR sound to be exported.

The problem is that each resource pointed by a MonoBehaviour (either a public member or a private member with the [SerializeField] attribute) will be exported by default. Is possible to override this behavior?, the apk gets really big when you have 5 or more languages but you just want to make a build for a single language. and deactivating each one of the resources before build is rather tedious for a medium size game.

The goal is to load different language sets into the builds but I'm not sure I'm on the right track with AssetsBundle and Resources.

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
  • Side note: Consider not using "unload" in title. Maybe change tosomething along the lines of "Build ... with one one set of localized resources" instead. BTW, don't need to add thank you to posts - check [meta](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) for reasons - your post already shows good effort. – Alexei Levenkov Mar 28 '14 at 16:15
  • 1
    @AlexeiLevenkov, thanks for the tip, hope is more clear. I'will read the meta now. – sacameunojo Mar 28 '14 at 16:42

1 Answers1

0

Resources are always included, so long as they're in a "Resources" folder. The only way to strip them from the build is to move them out of that folder or rename the folder. You could set it up such that each language pack uses a similar naming convention for resource files, and only certain language packs are included at build time.

Asset bundles are not always designer-friendly, but are a good way to accomplish dynamic inclusion of assets.

You do have one other option: using pre-processor directives to define or undefine certain variables.

Assume for the sake of argument that your fields are defined like so:

[SerializeField] LanguagePack fr;
[SerializeField] LanguagePack en;

The type of LanguagePack isn't important, here. It is only a stand-in for whatever type you're currently serializing.

You could adjust the class so that declaration of those fields is conditional:

#if FRENCH || UNITY_EDITOR
[SerializeField] LanguagePack fr;
#endif

#if ENGLISH || UNITY_EDITOR
[SerializeField] LanguagePack en;
#endif

When using the Unity editor, all of the fields will be available. When building a project, you can edit "Scripting Define Symbols" in player settings to enable ENGLISH, FRENCH, or whatever other language packs.

This may require introduction of some complicated code, since your defined variables will depend on build settings. In many cases, it will lead to simpler code if you add a property that serves as an alias:

LanguagePack current {
    get {
        #if FRENCH
            return fr;
        #else
            return en;
        #endif
    }
}

The advantage of all that is that the game is still easy to edit in the inspector.

Community
  • 1
  • 1
rutter
  • 11,242
  • 1
  • 30
  • 46
  • Moving the `Resources` and then playing with the `AssetsBundle` seems more practical than rewriting the actual code. In the long term doing this could be used to upload LanguageSets for an app update without rebuilding. Is that right? – sacameunojo Mar 30 '14 at 12:08
  • Yep! AssetBundles are tricky to work with (especially since they can't contain code easily), but they're a great option for dynamic content. – rutter Apr 02 '14 at 21:27