0
votes

I've searched on this question and found a lot of info related, but some inconsistencies, and no clear answer.

In a Unity project, I'm creating asset bundles with gameobjects that have scripts (C#). I've learned how to do that, and it works.

My problem is, I ultimately need to preserve the values of the public variables in the scripts that were assigned before building the asset bundle. That is, my prefab has a script with public variables. I instance it many times with different public values for each instance. I need all that to survive the trip through the asset bundle.

My current process is thus:

  1. Remove scripts from their game objects.
  2. Build the scripts into assemblies
  3. Pack game objects and assemblies into asset bundles
  4. Download the bundles to new project
  5. Unpack the asset bundles and Instantiate the game objects
  6. Attach script assemblies to game objects with AddComponent()

The issue is step 1. I remove the scripts from the game objects before bundling because if I don't, it complains when unpacking that the referenced script is missing. And also, since step 6 uses AddComponent to attach it, I cant really do that if that script is already an attached component. So removing them before bundling solves those problems.

What am I missing? Where is my method at fault? I read that the public values are saved in the script reference on the game object when bundled, but of course I lose that when I remove the script components.

So, please, how do I maintain public values thru the asset bundling process? Should I NOT be removing script components before bundling? If so, how should I be managing the scripts differently? Would I need to bundle both the script assemblies AND source files? (I currently only bundle the assemblies), and wouldn't that create a redundancy when attaching the assembly?

1

1 Answers

0
votes

Unity does not store assemblies in bundles. It stores only serialization data for scripts (that's your public variables, and variables with [SerializeField] attribute). But it also stores the hash of the script assembly for every script type, so you have to build bundles in the same project with the main executable to avoid missing references.

So, you don't have to remove scripts from their GameObjects, but you have to build bundles in the same unity project and rebuild them, when code changes.