Search Unity

AssetBundles and scripts with constants

Discussion in 'Asset Bundles' started by mrmcduck, Nov 5, 2017.

  1. mrmcduck

    mrmcduck

    Joined:
    Nov 26, 2015
    Posts:
    26
    Hi everybody,

    I am currently working on an autopatcher for my game, and so far it is working fine. But there are few things I'm uncertain about:

    - I have a few classes (monoBehaviour) which contain constants (e.g. public const uint SOME_VALUE = 5; ), this constant values are also used in other scripts. Will I run into troubles with that pattern? If I update an asset with such a class containing constants, will all the other assets use the changed constants? Should I get rid of the const keyword, and access the variables directly by an instance of the object?

    - Using static in combination with asset bundles seems like a very bad idea? Before I started to work with assetBundles I had a few static manager classes (no monoBehaviour), so far as I understand this is not compatible? I can only patch things that exist as prefab, or are directly within a scene, yes? Also I'm pretty unsure about static methods within MonoBehaviour classes. This seems like a really missing point in the documentations.

    - Edit: I have a few non-monobehaviour classes which are used for small internal objects, for example fixed point math calculation objects, where should I put those? Is it fine to have them at the bottom of some other MonoBehaviour script? Should they be inside of a MonoBehaviour?

    Best regards
     
    Last edited: Nov 5, 2017
  2. mrmcduck

    mrmcduck

    Joined:
    Nov 26, 2015
    Posts:
    26
    Nobody? :(
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    These are mostly things you need to check for yourself. I don't know how the asset bundles interacts with data from scripts. Does it even work to have a script in one asset bundle that depends on a script from a different asset bundle? Or a GameObject in a bundle that has a script from a different bundle attached to it?

    As far as I understand, const values are written in directly when you compile code. So if you have:

    Code (csharp):
    1. const in defaultVal = 5;
    2.  
    3. public void DoSomething(string s, int i = defaultVal) {
    4.  
    5. }
    That compiles down to the equivalent of:

    Code (csharp):
    1. public void DoSomething(string s, int i = 5) {
    2.  
    3. }
    Which means that if you're working with dll's, consts can be tricky as if a const is defined in dll A, and used in dll B, the value won't change in dll B even if you recompile dll A. I have no idea if scripts in asset bundles work like that, or if the scripts are loaded and compiled when you load the bundle.
     
  4. mrmcduck

    mrmcduck

    Joined:
    Nov 26, 2015
    Posts:
    26
    I strongly disagree, why am I responsible to find out how internal engine mechanics work? Especially since I am not provided with it's source.

    No, scripts are not included in AssetBundles, this is documented. So far I understood only prefabs (which contain your scripts) and scenes can be included in AssetBundles. This behavior makes sense and is a good pattern, however it leads to the other questions I mentioned.

    About constants: What you wrote seems to be correct, therefore general rule of thumb: If you plan to use AssetBundles never use anything with const unless you are 100% sure you never have to change it. And as long as I don't get an official answer about the other questions, the same rule goes for anything with static.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You're responsible because it's not documented, and nobody seems to know the answer. So if you want to know, the only way is to check.

    You can disagree with that all you want, I guess?
     
  6. mrmcduck

    mrmcduck

    Joined:
    Nov 26, 2015
    Posts:
    26
    IMHO that's a funny way to see it, but fine if that works for you. However in my opinion I can expect answers from the developers when I'm paying for a commercial product if the documentation is lacking crucial information.

    Also I don't think that you see the scope of the open questions, this can't be worked out within a few minutes by breakpointing through two different functions. And even if it would be, I'm used to ship products based on solid programming (+using things as they are intended to) and not patterns that were worked out by trial and error.
     
  7. Reichert

    Reichert

    Unity Technologies

    Joined:
    Jun 20, 2014
    Posts:
    63
    In general your concerns are orthogonal to asset bundles, unless you are also trying to load dll's from bundles, which we don't recommend or officially support. Script/code is not delivered via bundles, only Type Tree information which allows older bundles to be loaded on newer versions of Unity (in most cases) where the code layout may have changed.

    Your general scripting related questions are probably better answered in the Scripting forum: https://forum.unity.com/forums/scripting.12/
     
  8. mrmcduck

    mrmcduck

    Joined:
    Nov 26, 2015
    Posts:
    26
    Thank you for your reply.

    Could you clarify on how to work with ScriptableObjects (e.g. inventory items) in AssetBundles? Or is that something that is not supported? My specific question would be on how to patch a ScriptableObject class via AssetBundles, where does it need to be?