首页 文章

Unity Asset Bundles

提问于
浏览
0

我正在使用Unity游戏引擎为游戏创建一个编辑器,我希望允许设计师制作新的法术 . 我想要实现的是设计师将下载Unity粒子系统(或GameObject),将其保存在计算机上并为游戏提供完整的路径 . 然后游戏应该加载对象并将其用作新法术的粒子 .

我需要能够在运行时从文件夹加载Unity GameObject(粒子系统) . 我尝试使用Asset Bundles,但是当我只想从计算机(本地文件系统)而不是从服务器下载文件时,我不知道如何使用资产包 . 资产包在Debug中完美运行,但在构建和运行时却没有(它试图连接到某个服务器) . 我使用Asset Bundles的默认脚本:

public class LoadAssets : MonoBehaviour
    public const string AssetBundlesOutputPath = "/AssetBundles/";
    public string assetBundleName;
    public string assetName;

    public GameObject loadedObject;
    public bool finished;
    public MessageWindow messWindow;

    // Use this for initialization
    //originally was start here might create problems
    public IEnumerator Begin ()
    {
        messWindow.changeMessageAndShow("Asset " + assetName + " is loading.","Loading");
        yield return StartCoroutine(Initialize() );

        // Load asset.
        yield return StartCoroutine(InstantiateGameObjectAsync (assetBundleName, assetName) );
        finished=true;
        messWindow.Close();
    }

    public GameObject LoadObject()
    {
        StartCoroutine(Begin());
        //TODO wait for end of couroutine
        return loadedObject;
    }

    // Initialize the downloading url and AssetBundleManifest object.
    protected IEnumerator Initialize()
    {
        // Don't destroy this gameObject as we depend on it to run the loading script.
        DontDestroyOnLoad(gameObject);

        // With this code, when in-editor or using a development builds: Always use the AssetBundle Server
        // (This is very dependent on the production workflow of the project. 
        //  Another approach would be to make this configurable in the standalone player.)
#if DEVELOPMENT_BUILD || UNITY_EDITOR
        AssetBundleManager.SetDevelopmentAssetBundleServer ();
#else
        // Use the following code if AssetBundles are embedded in the project for example via StreamingAssets folder etc:

        // Or customize the URL based on your deployment or configuration
        //AssetBundleManager.SetSourceAssetBundleURL("http://www.MyWebsite/MyAssetBundles");
#endif
        AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath + "/");

        // Initialize AssetBundleManifest which loads the AssetBundleManifest object.
        var request = AssetBundleManager.Initialize();
        if (request != null)
            yield return StartCoroutine(request);
    }

    protected IEnumerator InstantiateGameObjectAsync (string assetBundleName, string assetName)
    {
        // This is simply to get the elapsed time for this phase of AssetLoading.
        float startTime = Time.realtimeSinceStartup;

        // Load asset from assetBundle.
        AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(assetBundleName, assetName, typeof(GameObject) );
        if (request == null)
            yield break;
        yield return StartCoroutine(request);

        // Get the asset.
        GameObject prefab = request.GetAsset<GameObject> ();

        if (prefab != null)
        {
            loadedObject = GameObject.Instantiate(prefab);            
            DontDestroyOnLoad(loadedObject);
        }

        // Calculate and display the elapsed time.
        float elapsedTime = Time.realtimeSinceStartup - startTime;
        Debug.Log(assetName + (prefab == null ? " was not" : " was")+ " loaded successfully in " + elapsedTime + " seconds" );
    }
}

非常感谢 .

1 回答

  • 1

    要从本地系统加载资产包,您需要Local AssetBundle Server . 启用Local Asset Server时,必须构建AssetBundle并将其放置在项目根目录中显式名为 AssetBundles 的文件夹中,该文件夹与 Assets 文件夹位于同一级别 .

    构建的AssetBundles将可供编辑器和本地运行的所有构建,可以访问本地网络上的编辑器 .

    使用 Assets>AssetBundles>Local AssetBundle Server 启用Local AssetBundle Server .

    Reference到资产包和资产包管理器 .

相关问题