VR专区

Unity AssetBundles打包

2017-06-22  本文已影响31人  青鱼谷雨

首先需要打包一个预制体物体,该预制体需要更改后缀为.unity3d;

using UnityEditor;
using System.Collections;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
        
    public class QABEditor  
    {        
        [MenuItem("QFramework/AB/Build")]      
        public static void BuildAssetBundle()       
        {          

            // AB包输出路径           
            string outPath = Application.streamingAssetsPath + "/QAB";      

            // 检查路径是否存在         
            CheckDirAndCreate (outPath);     

            BuildPipeline.BuildAssetBundles (outPath, 0, EditorUserBuildSettings.activeBuildTarget);    

            // 刚创建的文件夹和目录能马上再Project视窗中出现        

            AssetDatabase.Refresh ();      
        }    

        /// <summary>     
        /// 判断路径是否存在,不存在则创建       
        /// </summary>      
           public static void CheckDirAndCreate(string dirPath)     
         {          
             if (!Directory.Exists (dirPath)) {   
                
                Directory.CreateDirectory (dirPath);  

            }      
        }    

}

读取ab包;
using UnityEngine;
using System.Collections;
using System.IO;

public class  QFramework : MonoBehaviour
{


    public string busUnity;    //ab包的名字  xxx.unity3d
    public string busName;   //生成物体的名字    xxx

    public string tree1Unity;
    public string tree1Name;
  
    void Start()
    {

        StartCoroutine(GameInstanciate(busUnity, busName));

        StartCoroutine(GameInstanciate(tree1Unity, tree1Name));
 
    }

 

    IEnumerator GameInstanciate(string unity, string name)
    {

        WWW www = new WWW("file:///" + Application.streamingAssetsPath + Path.DirectorySeparatorChar + "QAB" + Path.DirectorySeparatorChar + unity);

        yield return www;


        if (string.IsNullOrEmpty(www.error))
        {

            var go = www.assetBundle.LoadAsset<GameObject>(name);
            Debug.Log(go.transform.position);
            Instantiate(go);
           //Instantiate(go).transform.SetParent(GameObject.Find("Tree").transform);
           // Transform obj= Instantiate(go.transform,go.transform.position,Quaternion.identity,GameObject.Find("Tree").transform)as Transform;
            //GameObject obj = Instantiate(go, GameObject.Find("Tree").transform);
           // obj.transform.position = go.transform.position;
           // obj.transform.rotation = go.transform.rotation;
         


        }
        else
        {

            Debug.LogError(www.error);

        }

    }
}



上一篇 下一篇

猜你喜欢

热点阅读