场景无法从资产包中正确加载

时间:2015-11-04 12:36:01

标签: unity3d assetbundle

三天前,我坚持使用资产捆绑与统一4.x 到目前为止我所尝试的内容如下:

使用此代码段

制作场景资产包,该代码段为我成功制作了.unity3d场景资产包

public class SceneAssetBundleMaker : EditorWindow {

    bool groupEnabled;

    public static string sceneName;
    static string sceneNameWithPath;
    static string sceneNameWithPathAssetBundel;
    [MenuItem("AB Scene/BuildSceneAsset")]


    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(SceneAssetBundleMaker));

    }

    void OnInspectorUpdate()
    {
        Repaint();
    }

    void OnGUI()
    {
        EditorGUILayout.BeginVertical("Box");
        GUILayout.Label("Enter Scene Name");
        sceneName = EditorGUILayout.TextField(sceneName);

        if (GUILayout.Button("Make Asset Bundle"))
        {
            if (sceneName != null)
                myBuild();
            else
                Debug.LogError("sceneName must be entered");
        }
        EditorGUILayout.EndVertical();

    }
     static  void myBuild()
    {
        //        string[] levels = { "Assets/Scene/SimpleBakingPlanAndBoxes.unity" };
        //string[] levels = { "Assets/Scene/"+ sceneName+ ".unity" };
        sceneNameWithPath = "Assets/_project/_scenes/" + sceneName + ".unity";
        Debug.Log("sceneNameWithPath" + sceneNameWithPath);
        string[] levels = { sceneNameWithPath };
        Debug.Log("levels : " + levels.Length);
        Debug.Log("levels 0 : " + levels[0]);

        sceneNameWithPathAssetBundel = "Assets/AssetBundles/" +sceneName + ".unity3d";
        string v = BuildPipeline.BuildStreamedSceneAssetBundle(levels, sceneNameWithPathAssetBundel, BuildTarget.StandaloneWindows);
        Debug.Log(v);
        //"Streamed-Level1.unity3d"
        //BuildPipeline.BuildStreamedSceneAssetBundle(levels, "Streamed-Level1.unity3d", BuildTarget.StandaloneWindows64);

    }   
}

此代码用于下载场景资产包

public IEnumerator LoadSceneBundle(string assetBundleSceneName, string orignialName) {
        url = "file://" + Application.dataPath + "/AssetBundles/" + assetBundleSceneName + ".unity3d";
        Debug.Log("scene load url : " + url);
        using (WWW www = WWW.LoadFromCacheOrDownload(url,1)){
            yield return www;
            if (www.error != null) {
                throw new Exception("WWW donwload had an error : " + url + " " + www.error);
                //Debug.Log("");
            }
            AssetBundle ab = www.assetBundle;
            Debug.Log("Main Asset :: " + www.assetBundle.mainAsset);
            ab.LoadAll();
            Application.LoadLevel(orignialName);
            ab.Unload(false);
        }
    }

两个场景我正在通过此代码切换资产包。代码工作正常,但并非总是如此。有时它会加载场景,有时候不是在我独立构建时遇到的相同情况。

部署后,我创建了一个AssetsBundel文件夹,并将我的assetbudel场景文件放入其中,但一切都是徒劳的。

0 个答案:

没有答案