Ejemplo n.º 1
0
 /// <summary>
 /// 加载assetBundle到缓存
 /// </summary>
 /// <param name="path"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 public GameObject Load(string path, string name)
 {
     using (AssetBundleLoad loader = new AssetBundleLoad(path))
     {
         return(loader.LoadAsset <GameObject>(name));
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 加载并克隆
 /// </summary>
 /// <param name="path"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 public GameObject LoadClone(string path, string name)
 {
     using (AssetBundleLoad loader = new AssetBundleLoad(path))
     {
         GameObject go = loader.LoadAsset <GameObject>(name);
         return(Object.Instantiate(go));
     }
 }
Ejemplo n.º 3
0
    public void StartUnloadSceneAsync(AssetBundleLoad load)
    {
        if (!_uninstallable_scene.ContainsKey(_scene_name))
        {
            return;
        }

        if (!_uninstallable_scene[_scene_name])
        {
            return;
        }

        TextAsset xmlInfo = load.LoadAsset <TextAsset>(_xml_asset_bundle_name);
        Dictionary <XmlNode, XmlNodeList> sceneObjects = GetSceneObjects(xmlInfo);

        load.UninstallAssetBundle(_asset_bundle_name, true);
        if (sceneObjects != null)
        {
            var itr = sceneObjects.Keys.GetEnumerator();
            while (itr.MoveNext())  // 遍历Object的节点
            {
                XmlElement objectNode = itr.Current as XmlElement;
                string     objectPath = objectNode.GetAttribute("Path");
                string     objectName = objectNode.GetAttribute("Name");

                string assetBundleName = load.GetAssetBundleNameByAssetPath(objectPath);
                if (assetBundleName != string.Empty)
                {
                    load.UninstallAssetBundle(assetBundleName, true);
                }
            }
            itr.Dispose();
        }

        _uninstallable_scene.Remove(_scene_name);
    }
Ejemplo n.º 4
0
    public IEnumerator StartLoadSceneAsync(AssetBundleLoad load)
    {
        if (!_uninstallable_scene.ContainsKey(_scene_name))
        {
            _uninstallable_scene.Add(_scene_name, false);
        }

        _current    = 0;
        _totalCount = 2;  //最少有2个,一个ab,一个场景加载

        CalculationDependencyCount(_asset_bundle_name, load);

        TextAsset xmlInfo = load.LoadAsset <TextAsset>(_xml_asset_bundle_name);

        Dictionary <XmlNode, XmlNodeList> sceneObjects = GetSceneObjects(xmlInfo);

        AssetBundleLoaderRequest abRequest = AssetBundleLoaderRequest.New(_asset_bundle_name);

        load.LoadSingleAssetBundleAsync(abRequest);
        while (!abRequest.IsDone)
        {
            yield return(null);
        }

        UpdateProgress();

        // 加载场景依赖项
        yield return(LoadDependency(_asset_bundle_name, load));

        yield return(SceneManager.LoadSceneAsync(_scene_name, _load_mode));

        UpdateProgress();

        // 复位Object对象
        if (sceneObjects != null)
        {
            var itr = sceneObjects.Keys.GetEnumerator();
            while (itr.MoveNext())  // 遍历Object的节点
            {
                XmlElement objectNode = itr.Current as XmlElement;
                string     objectPath = objectNode.GetAttribute("Path");
                string     objectName = objectNode.GetAttribute("Name");

                AssetLoaderRequest request = AssetLoaderRequest.New(objectPath);
                load.LoadAssetAsync <Object>(request);

                while (!request.IsDone)
                {
                    yield return(null);
                }
                UpdateProgress();

                GameObject gameObject = (GameObject)GameObject.Instantiate(request.asset);
                gameObject.name = objectName;

                foreach (XmlElement el in objectNode.ChildNodes)
                {
                    float x = float.Parse(el.GetAttribute("X"));
                    float y = float.Parse(el.GetAttribute("Y"));
                    float z = float.Parse(el.GetAttribute("Z"));

                    if (el.Name.Equals("Position"))
                    {
                        gameObject.transform.position = new Vector3(x, y, z);
                    }
                    else if (el.Name.Equals("Rotate"))
                    {
                        gameObject.transform.eulerAngles = new Vector3(x, y, z);
                    }
                    else if (el.Name.Equals("Scale"))
                    {
                        gameObject.transform.localScale = new Vector3(x, y, z);
                    }
                }
            }

            itr.Dispose();
        }

        UpdateProgress();
        IsDone = true;
        if (loadCompleted != null)
        {
            loadCompleted(this);
        }

        _uninstallable_scene[_scene_name] = true;
    }