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

时间:2015-10-30 11:22:47

标签: c# unity3d

不建议 Unity3d官方链接我已尝试过它并不全面,也没有提供必要的详细信息。我是新手,将在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扩展名文件。 我缺少的内容以及分享有关在Unity 5中从本地服务器/ PC创建和下载资产包的正确指南

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

我们正在创建的Assetbundle是特定于平台: 要创建AssetBundle,只需编写一个C#脚本并将其放在编辑器文件夹中: 资产&gt;编辑器 在这里我提供前。 android特定平台:

&#13;
&#13;
CACHES = {
'default': {
    'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
    'LOCATION': 'django_dbcache',
},
'staticfiles': {
    'BACKEND': "django.core.cache.backends.locmem.LocMemCache",
    'LOCATION': 'static-files',
}
&#13;
&#13;
&#13;

之后,您可以在本地或远程服务器上放置target.unity3d文件 : 请记住,在android平台上我们必须使用下面提供的WWW类。 [imp]现在我们还可以在assetsbundle中包含脚本: 只需尝试下面的脚本,您必须将此脚本附加到要在其中导入AssetBundle的场景中清空游戏对象:

&#13;
&#13;
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);
    }
}
&#13;
&#13;
&#13;

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

&#13;
&#13;
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);

		}
	}
	
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

大约4年后,我不得不再次处理资产捆绑交易,目前的答案已经过时,此后发生了许多变化,因此我来回答。 为了制作和管理资产束及其依赖关系,现在提供了一个出色的工具来制作资产束asset bundle browser,至少您需要 2017.1 。它也可以在GitHub上使用。这非常简单容易。

您可以使用自己喜欢的压缩方法快速制作资产捆绑包。大量文档可在官方网站here上找到。

enter image description here