Script Import AssetBundle: ClassDerivedFromMonoBehaviour?

I think I’m almost done with this Script that Includes scripts in assetbundle.

I’m using this documentation here:
http://docs.unity3d.com/Manual/scriptsinassetbundles.html + all the answer/question I’ve read from here:

this is my script:

using UnityEngine;
using System.Collections;

public class BinaryImporter : MonoBehaviour {

    string url = "file:///C:/Users/rhylvin2016/Documents/Unity%20Folders/WWW/Assets/AssetBundles/bytetext";
    IEnumerator Start()
    {
        while (!Caching.ready)
            yield return null;

        Debug.Log("1");
        WWW www = WWW.LoadFromCacheOrDownload(url,1);
        if (www != null) { Debug.Log(www); }
        Debug.Log("2");
        yield return www;

        Debug.Log("3");
        AssetBundle bundle = www.assetBundle;
        if (bundle != null) { Debug.Log(bundle); }
        Debug.Log("4");
        TextAsset txt = bundle.LoadAsset("CameraMove", typeof(TextAsset)) as TextAsset;

        Debug.Log("5");
        var assembly = System.Reflection.Assembly.Load(txt.bytes);
        if (assembly != null)  { Debug.Log(assembly); }
        var type = assembly.GetType("Class1");

        Debug.Log("6"+assembly.GetType().Name);
        GameObject go = new GameObject();
        Debug.Log("7");
        go.AddComponent(type);
        Debug.Log("8");
    }
}

I don’t get an error rather a notification that’s saying “AddComponent asking for invalid type
UnityEngine.GameObject:AddComponent(Type)”

that line

 var type = assembly.GetType("Class1");

is probably the problem. “Class1” for me is the “ClassDerivedFromMonoBehaviour” in the documentation. “Class 1 is the class with MonoBehaviour in the dll that I created”…

to be more understandable I’m gonna tell you step by step what I did.

1.) Open Visual Studio and create new Class Library naming Assembly name CameraMove
2.) Change Target framework to 3.5, deleted references that isn’t use.
3.) Deleted namespace and other using, added using UnityEngine
4.) made this script as the DLL

    public class Class1 : MonoBehaviour
    {
    public void Update()
    {
        Debug.Log("hey");
        //originally wanted to create a script that could Find the MainCamera and make it move, but I didn't get to it cause I got stuck with this problem
    }
}

5.)Change Solution Configuration to Release and Built it.

6.)Change the built dll to .bytes

7)Created new Unity Project, made this script to create an AssetBuild Button

using UnityEditor;

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

8)Created AssetBundles Folder, manually put the MoveCamera.bytes in Unity. and made its assetbundle name into bytetext

9)Built it and copied the byetext bundle directory and paste it in string url

In the documentation it says

TextAsset txt = bundle.Load("myBinaryAsText", typeof(TextAsset)) as TextAsset;

so that’s why I wrote “MoveCamera”, seems to work, its not null. so probably on the right track…just don’t know about that

var type = assembly.GetType("MyClassDerivedFromMonoBehaviour");

Thanks in advance

@AngryAnt @rohankad @Bunny83

Setting up to separate prefabs and checking if tag = PlayerTorpedo has worked, though i'd still like to know if there is a better/different way.

2 Answers

2

So you get the expected name logged out in 6?

Just for kicks, could you change the GameObject construction line #30 to the following (removing the AddComponent step) and see if this affects the error you get logged?

new GameObject ("Test", type);

i did what you said. but still the same. not an error but a notification saying "AddComponent asking for invalid type UnityEngine.GameObject:.ctor(String, Type[])" also I tried this.new GameObject("CameraMove", assembly.GetType("Class1")); still the same, but when I change the assembly.GetType("any string") to assembly.GetType("CameraMove"), I get an error saying "TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'CameraMove'. System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType)"

you can try “System.Type type =assembly.GetType(“Class1”);”

Thanks @rhylvin2016 and @twinkle!