Search Unity

Missing Scripts on loading Scene Asset Bundle

Discussion in 'Asset Bundles' started by Neeraj_Navlakha, Nov 30, 2018.

  1. Neeraj_Navlakha

    Neeraj_Navlakha

    Joined:
    Jul 1, 2016
    Posts:
    6
    Hello Everyone,
    I am developing a Unity loader which loads different scene on demand using asset Bundle. The Scene from the asset bundle loads correctly but the scripts attached to the object in the Scene doesn't load and show a warning message "The referenced script (Unknown) on this Behaviour is missing!". Can someone advise a solution to this problem?
     
  2. Roelof

    Roelof

    Joined:
    Jun 25, 2013
    Posts:
    8
  3. Neeraj_Navlakha

    Neeraj_Navlakha

    Joined:
    Jul 1, 2016
    Posts:
    6
    Thanks.
    Can you please explain where should I write the code to import another code at runtime, whether It should be inside an asset bundle or in the loader loading the scene asset bundle?
     
  4. Roelof

    Roelof

    Joined:
    Jun 25, 2013
    Posts:
    8
    it can't be in the assetbundle you want to import/load, because you'd need to import and load it in order to do that :p The assetbundle is only a package that contains some data: it won't become meaningfull until you actually import it and load it's contents.

    So you'll probably want to put both the import and load functionality in a scene, which will then know what to do with the content of the assetbundle (load code and then load objects/scene and whatever).
     
  5. Neeraj_Navlakha

    Neeraj_Navlakha

    Joined:
    Jul 1, 2016
    Posts:
    6
    I had already tried that. Here is the code snippet. Can you please check were I am going wrong.
    Code (CSharp):
    1.  IEnumerator AssetUrl0(string url) {
    2.        
    3.         if (!assetBundle0)
    4.         {
    5.             Assembly assembly = Assembly.LoadFile(assemblyURL);
    6.             GameObject gameObject = new GameObject();
    7.             System.Type type = assembly.GetType("Rotation");
    8.             gameObject.AddComponent(type);
    9.  
    10.             using (WWW www = new WWW(url))
    11.             {
    12.                 yield return www;
    13.                 if (!string.IsNullOrEmpty(www.error))
    14.                 {
    15.                     Debug.LogError(www.error);
    16.                     yield break;
    17.                 }
    18.                 assetBundle0 = www.assetBundle;
    19.             }
    20.          
    21.             string[] scenes = assetBundle0.GetAllScenePaths();
    22.  
    23.             foreach (string scenepath in scenes)
    24.             {
    25.                 scene0 = Path.GetFileNameWithoutExtension(scenepath);              
    26.                 SceneManager.LoadSceneAsync(scene0, LoadSceneMode.Additive);
    27.  
    28.                 //assetBundle.Unload(false);
    29.                 //SceneManager.UnloadSceneAsync(scene);
    30.             }
    31.         }
     
  6. Roelof

    Roelof

    Joined:
    Jun 25, 2013
    Posts:
    8
    Hmm, loading the scenes (line 21 onward) seems to be right, though you'll get an error if for whatever reason your www request goes wrong.

    I don't think you're actually getting the code from an assetbundle, but you already have a dll ready at some address? I know I've been struggling a bit with the Assembly loading and found the advice to always try and use the 'Load' function like so:

    var name = AssemblyName.GetAssemblyName(filepath);
    var assembly = Assembly.Load(name);

    Also note that when trying to load an assembly that is already loaded (or shares a name with an already loaded assembly) then you will just get the original returned.
     
  7. Neeraj_Navlakha

    Neeraj_Navlakha

    Joined:
    Jul 1, 2016
    Posts:
    6
    I am not getting any code from the asset bundle because all the scripts go missing so I am loading the dll from some address. It fetches the dll correctly and game object is attached with the code inside dll but that object is of the main scene whereas I wanted to add the script to asset bundle scene or object.
     
  8. Roelof

    Roelof

    Joined:
    Jun 25, 2013
    Posts:
    8
    Is the name of your imported dll 'Assembly-CSharp'? If it is, then when it tries to import, it will already see an assembly with the same name and not load this new one. You'll need to use assembly definitions to put your code in a different assembly, so you can load it.
     
  9. Neeraj_Navlakha

    Neeraj_Navlakha

    Joined:
    Jul 1, 2016
    Posts:
    6
    I have a Rotation.cs script attached to gameobject in asset bundle scene and converted the same script to Rotation.dll. So do you mean I should change the name of the dll?
     
  10. Roelof

    Roelof

    Joined:
    Jun 25, 2013
    Posts:
    8
  11. Neeraj_Navlakha

    Neeraj_Navlakha

    Joined:
    Jul 1, 2016
    Posts:
    6
    I think you are getting confused. I have made Rotaion.dll in the visual studio not just changing the name of dll :D
     
  12. Roelof

    Roelof

    Joined:
    Jun 25, 2013
    Posts:
    8
    Hmm, can you post the Rotation class? Is it part of a Unity project or did you make it in a seperate visual studio solution which you built?
     
  13. Unity_Joseph

    Unity_Joseph

    Unity Technologies

    Joined:
    Nov 3, 2016
    Posts:
    16
    There was a bug preventing loading managed objects out of asset bundles when the class assemblies are loaded through Assembly.Load (built DLLs outside of Unity). This was recently fixed in 2019.1.0a13. I requested a backport to 2018.3.
     
    ruudvangaal and xen23 like this.
  14. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,052
    Do you have any news about that backport? I'm on a stable release and I don't want to bump to 2019.1 beta for that fix. I have tested it in 2018.3.4f1 and it's still broken.
     
    Last edited: Feb 2, 2019
    ruudvangaal and xen23 like this.
  15. harrysjoerd

    harrysjoerd

    Joined:
    Jan 30, 2017
    Posts:
    5
    Could it be that with this bug it was still possible to run it in Editor mode but not in a Standalone Build? Because that is exactly the problem that I encountered with assetbundles including prefabs referencing a DLL (some script references were still found though). When could we expect a backport to 2018.3?

    So glad I found this post. This issue could make me research DLL stuff forever.
     
    xen23 likes this.
  16. harrysjoerd

    harrysjoerd

    Joined:
    Jan 30, 2017
    Posts:
    5
    I tested in 2018.3.5 and verified it works now. Thanks Unity!
     
    ruudvangaal likes this.
  17. markgerow-LGE

    markgerow-LGE

    Joined:
    Apr 1, 2019
    Posts:
    1
    Is this currently working in 2019.1.10 and beyond? I am loading a dll in the same manner but still running into OPs issue.
     
    SEALabVR likes this.
  18. pahe4retro

    pahe4retro

    Joined:
    Aug 13, 2019
    Posts:
    33
    Good question. Trying this in Unity 2019.3.0f1 and experiencing this problem.

    Edit: Okay, I solved my problem with creating an Assembly Definition file for my scripts (which were previously generated into the Assembly-CSharp.dll. Yes, I renamed the dll (fullname) before creating the assetbundle for it). With the generated dll from the assembly definition file, it works now. Don't know why it doesn't work with the Assembly-CSharp.dll though, but maybe this is of help for some others too.
     
    Last edited: Dec 10, 2019