【问题标题】:Creating and Downloading Asset bundle from local server in Unity 5在 Unity 5 中从本地服务器创建和下载资产包
【发布时间】:2019-10-17 20:46:41
【问题描述】:

不要向我推荐 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 创建和下载资产包的正确指南

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    【解决方案2】:

    我们正在创建的 Assetbundle 是特定于平台的: 要创建 AssetBundle,只需编写一个 C# 脚本并将其放在 Editor 文件夹中: 资产 > 编辑器 我在这里提供前。 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 文件放置在本地或远程服务器上: 请记住,在 android 平台上,我们必须使用下面提供的 WWW 类。 [imp] 现在我们还可以在assetbundle 中包含脚本: 只需尝试下面的脚本,您必须将此脚本附加到您要导入 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);

    【讨论】:

      【解决方案3】:

      大约 4 年后,我不得不再次处理资产包,目前的答案已经过时,并且在此之后发生了很多变化,因此我正在回答它。 为了制作和管理资产包及其依赖项,现在可以使用一个很棒的工具来制作资产包,它是asset bundle browser,至少你需要 2017.1 。它也可以在GitHub 上找到。非常简单易行。

      您可以使用自己喜欢的压缩方法快速制作资产包。官方网站here上提供了大量文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        相关资源
        最近更新 更多