Unity AssetBundlesパッケージ

2604 ワード

まず、接尾辞を変更する必要があるプレハブ物体をパッケージ化する必要がある.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 ();      
        }    

        ///      
        ///  ,        
        ///       
           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(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);

        }

    }
}