zoukankan      html  css  js  c++  java
  • unity三种资源加载方式Resources,AssetBundle,WebRequset的同步异步加载

    using System;
    using System.Collections;
    using UnityEngine;
    using UnityEngine.Networking;
    
    public enum AssetLoadMode
    {
        Resource,
        AssetBundle,
        WebRequest
    }
    public class AssetDownLoadComponent : GameComponent
    {
        void Awake()
        {
            Initinalize();
        }
        public T ResourcesLoad<T>(string path) where T : UnityEngine.Object
        {
            return Resources.Load<T>(path);
        }
        public void ResourcesAsyncLoad<T>(string path, Action<ResourceRequest> action) where T : UnityEngine.Object
        {
            StartCoroutine(ResourcesAsync<T>(path,action));
        }
    
        public void UnityWebRequested(string url, Action<UnityWebRequest> callback)
        {
            StartCoroutine(UnityWebRequestLoad(url,callback));
        }
        public T AssetBundleLoad<T>(string path, RuntimePlatform  runtimePlatform) where T:UnityEngine.Object
        {
            AssetBundle asset= LoadAssetBundle(path, runtimePlatform);
            string name = path.Substring(path.LastIndexOf('/'));
            name = name.Substring(0,name.Length-3);
            return asset.LoadAsset<T>(name);
        }
        public void AssetBundleAsyncLoad(string path, Action<AssetBundleCreateRequest> callBack, RuntimePlatform runtimePlatform)
        {
            StartCoroutine(AssetBundleLoadAsync(path,callBack, runtimePlatform));
        }
    
    
    
        private IEnumerator ResourcesAsync<T>(string path, Action<ResourceRequest> action) where T : UnityEngine.Object
        {
            ResourceRequest resourcesRequest = Resources.LoadAsync<T>(path);
            yield return resourcesRequest;
            if (resourcesRequest.asset!=null)
            {
                action(resourcesRequest);
            }
            else
            {
                Debug.LogError(path+"无法加载");
            }
            
        }
        private IEnumerator UnityWebRequestLoad(string url, Action<UnityWebRequest> callback)
        {
            UnityWebRequest webRequest = UnityWebRequest.Get(url);
            yield return webRequest.SendWebRequest();
            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                Debug.Log(webRequest.error);
            }
            else
            {
                callback.Invoke(webRequest);
            }
        }
    
        private AssetBundle LoadAssetBundle(string path, RuntimePlatform runtimePlatform)
        {
            string root = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundle/Win/");
            switch (runtimePlatform)
            {
                case RuntimePlatform.Android:
                    root = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundle/Android/");
                    break;
                default:
                    break;
            }
            Uri uri = new Uri(root+path);
            return AssetBundle.LoadFromFile(uri.AbsolutePath);
        }
    
        private IEnumerator AssetBundleLoadAsync(string path,Action<AssetBundleCreateRequest> callBack,RuntimePlatform runtimePlatform)
        {
            string root = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundle/Win/");
            switch (runtimePlatform)
            {
                case RuntimePlatform.Android:
                    root = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundle/Android/");
                    break;
                default:
                    break;
            }
            Uri uri = new Uri(root + path);
            AssetBundleCreateRequest assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(uri.AbsolutePath);
            yield return assetBundleCreateRequest;
            callBack(assetBundleCreateRequest);
    
    
        }
    
    }
  • 相关阅读:
    701. 二叉搜索树中的插入操作
    【ceph | 运维】 部署mgr
    【cpeh | 运维】mon相关命令
    【Leetcode】144. 二叉树的前序遍历
    【Linux】Linux中查看某个软件的安装路径
    【Leetcode】100. 相同的树
    【Leetcode】145. 二叉树的后序遍历
    【Leetcode】94. 二叉树的中序遍历
    redis学习04Redis的主从架构
    RabbitMQ学习02安装与配置(Ubuntu系统)
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14269296.html
Copyright © 2011-2022 走看看