分包加载

AssetbundleLoad.js脚本

-------------------------------------------------------------

var house : GameObject;
var carpet : Object;
var mirror : GameObject;
var textures : GameObject;
var mainobject : GameObject;
var names : String;

function Start()
{
   var www=new WWW("http://www.xxx.com/test/AssetBundle.unity3d"); //从服务器加载
   //var www=new WWW("file:///C:/1/AssetBundle.unity3d"); //从本地加载,绝对路径
   yield www;
   //一起选择Room,Carpet和Mirror的Prefab后导出,那么Room会成为mainAsset ,其他的用AssetBundle.Load("name")来加载
   //mainobject=Instantiate(www.assetBundle.mainAsset);
   house=Instantiate(www.assetBundle.Load("Room"));
   carpet=Instantiate(www.assetBundle.Load("asd")); //奇怪的是,就是它的名字不能写成carpet或者Carpet
   mirror=Instantiate(www.assetBundle.Load("Mirror"));
   textures=Instantiate(www.assetBundle.Load("Textures"));
   names=www.assetBundle.mainAsset.name;
   //www.assetBundle.Unload(false);
   
   renderer.material.mainTexture=textures.GetComponent("abc").A;
}

function Update () 
{
   
}

---------------------------------------------------------------------

abc.js

----------------------------------------------------------------------

var A: Texture2D;
var B: Texture2D;
var C: Texture2D;

function Update () 
{
   
}

---------------------------------------------------------------------

AssetbundleBuild.cs

---------------------------------------------------------------------

/// <summary> 
/// Exports a player compatible AssetBundle containing the selected objects, including dependencies. 
/// </summary> 
/// <remarks>Editor scripts are removed from the exported Assets.</remarks> 
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

class AssetbundleEditor
{
[MenuItem("Assets/Build AssetBundle")] 
public static void ExportPlayerAssetBundle() 

   string tempRelativePath = "Assets/Temp/"; 
   string tempAbsolutePath = Application.dataPath + "/../" + tempRelativePath;

   // Bring up save dialog. 
   string path = EditorUtility.SaveFilePanel("Save AssetBundle", "", "AssetBundle", "unity3d"); 
   if (path.Length > 0) 
   { 
    // Get all selected objects.

    Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); 
    //Object[] processedSelection = new Object[selection.Length]; 
    Object[] processedSelection=selection;
    for (int i = 0; i < selection.Length; i++) 
    { 
     // Clone the original object.

     Object currentObject = selection[i]; 
     bool isPrefab = currentObject != null && currentObject.GetType() == typeof(GameObject); 
     if (isPrefab) 
     { 
      if (!Directory.Exists(tempAbsolutePath)) 
       Directory.CreateDirectory(tempAbsolutePath);

      // Remove unneeded scripts from the prefab. 
                    /*
      Object clonedPrefab = EditorUtility.CreateEmptyPrefab(string.Format("{0}{1}.prefab", tempRelativePath, currentObject.name)); 
      if (clonedPrefab != null) 
      { 
       clonedPrefab = EditorUtility.ReplacePrefab((GameObject)currentObject, clonedPrefab); 
       //EditorBase component = ((GameObject)clonedPrefab).GetComponent(typeof(EditorBase)) as EditorBase; 
       UnityEditor component = ((GameObject)clonedPrefab).GetComponent(typeof(UnityEditor)) as UnityEditor; 
       if (component != null) 
        GameObject.DestroyImmediate(component, true); 
       EditorUtility.SetDirty(clonedPrefab); 
       processedSelection[i] = clonedPrefab; 
      } 
      */
     } 
    }

    // Save changes to AssetDatabase and import processed prefabs.

    EditorApplication.SaveAssets(); 
    AssetDatabase.Refresh();

    // Export the processed AssetBundle.

    BuildPipeline.BuildAssetBundle(Selection.activeObject, processedSelection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets); 
    Selection.objects = selection;

    // Remove all cloned objects from the project.

    for (int i = 0; i < processedSelection.Length; i++) 
    { 
     if (processedSelection[i] != null) 
      AssetDatabase.DeleteAsset(string.Format("{0}{1}.prefab", tempRelativePath, processedSelection[i].name)); 
    } 
    if (Directory.Exists(tempAbsolutePath)) 
     Directory.Delete(tempAbsolutePath, false); 
   } 
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值