- Home /
How to include script in Assets Bundle ?
hii all..
I want to load scripts in assets bundle which is component of the gameobject..
But i failed to load those scripts. I have read the documnets given : http://docs.unity3d.com/Manual/scriptsinassetbundles.html
I have tried this like :
//www = WWW.LoadFromCacheOrDownload ("file://E:/HTML5 Testcase/New Unity Project/Resources.unity3d", 1);
www = WWW.LoadFromCacheOrDownload ("localpath/sample.unity3d", 1);
// Wait for download to complete
yield return www;
if(www.error != null)
{
Debug.Log(www.error);
yield break;
}
if(www.error != null)
{
throw new UnityException("WWW download had an error : " + download.error);
}
// Load and retrieve the AssetBundle
AssetBundle bundle1 = www.assetBundle;
bundle1.LoadAllAssets();
// Load the TextAsset object
TextAsset txt = bundle1.LoadAsset("Assembly-CSharp.dll", typeof(TextAsset)) as TextAsset;
Debug.Log ("Txt : " + txt);
if( txt!= null )
{
// Load the assembly and get a type (class) from it
var assembly = System.Reflection.Assembly.Load(txt.bytes);
var type = assembly.GetType("MyClassDerivedFromMonoBehaviour");
//Instantiate a GameObject and add a component with the loaded class
GameObject go = new GameObject();
go.AddComponent(type);
Debug.Log("Loaded.. ");
}
But txt is coming null.
What is the problem with this script. please help me guys.
Thanks.
You need to make sure you delete the namespace that is created for the .dll before you build it into the package so that its contents ends up in the global namespace I believe! Otherwise it loads it and then can't find it :o
Answer by ajaybhojani · Sep 08, 2015 at 08:13 AM
hello you can direct attach script to object then make asset bundle of it . when you are downloading that bundle in unity editor run but it will not display as component in unity editor.
That script is not working and its saying about compiler error.
Answer by sodrix · Sep 08, 2015 at 08:44 AM
Hum You are using 'myCode.cs' or 'myCode.txt'? Your code have into code for execute? you can test to change 'txt!=null' to 'txt.Length>0'
Your answer
Follow this Question
Related Questions
Build assetbundle successfully but cannot instantiate it 0 Answers
Assetsbundle in subfolder 0 Answers
Building Asset Bundles requires Unity Advanced for AndroidPlayer 1 Answer
why UnloadUnusedAssets doesn't unload a asset? 0 Answers
Unity 5 Asset Bundles - missing specific sprites from downloaded scene? 0 Answers