Search Unity

script in assetbundle not loaded

Discussion in 'Scripting' started by ghiboz, May 5, 2015.

  1. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hi all!
    I've this problem: I create an assetbundle that contains a single prefab with this code:

    Code (CSharp):
    1. GameObject go = Selection.activeGameObject;
    2. string name = go.name;
    3.  
    4. Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/" + name + ".prefab");
    5. PrefabUtility.ReplacePrefab(go, prefab);
    6. AssetDatabase.Refresh();
    but I've got some scripts attached to the gameobjects inside my prefab but the scripts are not loaded when I load my assetbundle... I assume that are not present in the .prefab...

    how can I solve this?

    thanks
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Dave-Hampson likes this.
  3. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    thanks, but the code above is for the 'old' unity 4 system, i'm using the new assetbundle management...and having only a prefab, I think that the problem is when I create the prefab
     
  4. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    Hi again, I've reached to load my script in a different way, but after, using the code in the manual, I give this error:
    the code is this:

    Code (CSharp):
    1. TextAsset txt = codeBundle.LoadAsset(item, typeof(TextAsset)) as TextAsset;
    2. var assembly = System.Reflection.Assembly.Load(txt.bytes);
    thanks again!
     
  5. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    The BadImageFormatException would indicate that the txt.bytes doesn't translate to a valid assembly.

    If you try outputting the txt.bytes to a file, is it identical to the original assembly?
     
  6. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    yes, I've added this line:
    Code (CSharp):
    1. File.WriteAllBytes(@"C:\Output.txt", txt.bytes);
    and the output.txt is the same like the original assembly... :(
     
  7. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
  8. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    nope, also reading directly the file, the result is the same:
    Code (CSharp):
    1. var bb = File.ReadAllBytes(@"C:\LayoutTick.cs");
    2. var ass = System.Reflection.Assembly.Load(bb);
    reading the guide the problem seems to be not in my text file, but for some dll that MonoDevelop uses...
     
  9. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    ok, solved, i loaded directly a c# file, now I created a dll with the .cs and is imported
     
    steego likes this.
  10. Falcon_DZ

    Falcon_DZ

    Joined:
    Nov 23, 2015
    Posts:
    2
    Hi, @ghiboz
    i have the same prob, can you plz brief your solution..
     
  11. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hi @Falcon_DZ ,
    i've created a dll that contains the scripts and after I import the dll..
    not yet tested if works right with the latest version
     
  12. Falcon_DZ

    Falcon_DZ

    Joined:
    Nov 23, 2015
    Posts:
    2