首页 文章

在Unity 5中从本地服务器创建和下载资产包

提问于
浏览
3

don't recommendUnity3d official links 我试过了它并不全面,也没有提供必要的细节 . 我是新手,将在Unity3d中制作 AssetBundles . 到目前为止,在统一官员docs的帮助下,下面给出的是什么 .

/// <summary>
/// AssetBundles are exported from the editor using script code. (This is similar to the 4.x approach.)
/// The following script exports AssetBundles.
/// </summary>
public class AssetBundleCreate {

    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles() {
        Debug.Log("asset build");
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles");
    }

    [MenuItem("Assets/Get AssetBundle Names")]
    static void GetNames() {
        var names = AssetDatabase.GetAllAssetBundleNames();
        foreach(var name in names){
            Debug.Log("AssetBundle name is : " + name);
        }
    }
}

但是代码没有生成任何资产包而是它已经制作了 . 文件和.abc扩展名文件 . 我缺少什么,也 Share proper guide about Creating and Downloading Asset bundle from local server/pc in Unity 5

2 回答

  • 2
  • 2

    我们正在创建的Assetbundle是 platform specific: 要创建AssetBundle,只需编写一个C#脚本并将其放在 Editor 文件夹中: Assets > Editor 这里我提供ex . android特定平台:

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class ExportAssetBundles : Editor {
        [MenuItem("Assets/Build AssetBundle")]
        static void ExportResource()
        {
            string path = "Assets/AssetBundle/myAssetBundle.unity3d";
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,
                                           BuildAssetBundleOptions.CollectDependencies
                                         | BuildAssetBundleOptions.CompleteAssets,BuildTarget.Android);
        }
    }
    

    之后你可以放置target.unity3d文件 locally or on remote server :记住在android平台上我们必须使用下面提供的WWW类 . [imp]现在我们还可以在assetsbundle中包含脚本:只需尝试下面的脚本,你必须将这个脚本附加到你要导入AssetBundle的场景中的空游戏对象:

    using UnityEngine;
    using System.Collections;
    
    public class AssetBundleAugmenter : MonoBehaviour {
    
    	public string AssetName;
    	public int Version;
    	
    	private GameObject mBundleInstance = null;
    
    	
    	void Start() {  
    		StartCoroutine(DownloadAndCache());
    
    		}
    
    	// Update is called once per frame
    	IEnumerator DownloadAndCache() {
    
    		while(!Caching.ready)
    			yield return null;
    		
    		// example URL of file on PC filesystem (Windows)
    		// string bundleURL = "file:///D:/Unity/AssetBundles/MyAssetBundle.unity3d";
    		
    		// example URL of file on Android device SD-card
    		string bundleURL = "file:///mnt/sdcard/AndroidCube.unity3d";
    		
    		using (WWW www = WWW .LoadFromCacheOrDownload(bundleURL, Version)) {
    			yield return www;
    			
    			if (www .error != null)
    				throw new UnityException("WWW Download had an error: " + www .error);
    
    			// Load and retrieve the AssetBundle
    			AssetBundle bundle = www .assetBundle;
    
    			// 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);
    
    			if (AssetName == "") {
    				mBundleInstance = Instantiate (bundle.mainAsset) as GameObject;
    			}
    			else {
    				mBundleInstance = Instantiate(bundle.LoadAsset (AssetName)) as GameObject;
    			}
    		
    	
    			// Unload the AssetBundles compressed contents to conserve memory
    			bundle.Unload(false);
    
    		}
    	}
    	
    }
    

    如果您对导出脚本不感兴趣,那么只需从给定脚本中跳过以下部分:

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

相关问题