- Home /
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:
http://answers.unity3d.com/questions/259569/how-to-compile-script-to-include-it-to-assetbundle.html
http://answers.unity3d.com/questions/9688/dynamically-loading-scripts.html
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.
Open Visual Studio and create new Class Library naming Assembly name CameraMove
Change Target framework to 3.5, deleted references that isn't use.
Deleted namespace and other using, added using UnityEngine
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
Answer by AngryAnt · Jul 15, 2016 at 12:05 PM
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("Camera$$anonymous$$ove", assembly.GetType("Class1"));`
still the same, but when I change the assembly.GetType("any string") to assembly.GetType("Camera$$anonymous$$ove"), I get an error saying
"TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'Camera$$anonymous$$ove'. System.$$anonymous$$onoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType)"
I don't know if this is relevant or not but maybe my type is null, but I don't think it is null though anyway I found this comment:
"Got it!
var myType = Type.GetType(dbInfo.ScriptName);
was always returning NULL, because the class I was looking for is wrapped in a namespace. It's the same namespace the rest of the code is in, but as soon as I included the namespace in the GetType lookup it started finding a valid Type, and everything else is gravy.
Thanks for all of the help!!"
on the forum:
but I already deleted the default namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public class Class1 : $$anonymous$$onoBehaviour
{
public void Update()
{
Debug.Log("hey");
}
}
Answer by twinkle · Jul 22, 2016 at 06:07 PM
you can try “System.Type type =assembly.GetType("Class1");”
Your answer
Follow this Question
Related Questions
BadImageFormatException: Include Script using AssetBundle 1 Answer
in Unity 5, how can I get/set an asset's AssetBundle assignment via scripting? 1 Answer
How to auto find a component inside an assembly (dll)? 0 Answers
Enumerators and pdb2mdb 1 Answer
Cross reference multiple DLLs through AssetBundles 0 Answers