アセット依存関係の管理
Unity3.5から4.0へのアップグレードガイド

アセットバンドルへのスクリプトを含める

アセットバンドルはTextAssetをスクリプトにTextAssetとしてスクリプトを含めることができますが,その場合実際に実行できるコードになりません。もしアプリケーションで実行できるコードをアセットバンドルに含めたい場合は,アセンブリにプリコンパイルする必要があり,またMono Reflectionクラスとしてロードする必要があります(注意:ReflectionはiOSでは利用できません)。アセンブリを普通のC# IDE(例えば MonoDevelop,Visual Studio)またはMono/.NET コンパイラを使用して任意のテキストエディタで作成することが出来ます。

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
    // Start a download of the given URL
    WWW www = WWW.LoadFromCacheOrDownload (url, 1);

    // Wait for download to complete
    yield return www;

    // Load and retrieve the AssetBundle
    AssetBundle bundle = www.assetBundle;

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

    // 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);
}

アセット依存関係の管理
Unity3.5から4.0へのアップグレードガイド