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.
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
↧