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);
    
    
        }
    
    }
  • 相关阅读:
    postgres配置主从流复制
    laravel5如何创建service provider和facade
    postgres中几个复杂的sql语句
    Laravel5设计json api时候的一些道道
    技术晨读_2015_4_13
    PHP 中的Closure
    l5如何通过路由走api版本回退查找设置
    postgres中的中文分词zhparser
    postgres中的视图和物化视图
    问题:Virtual Box如何复制镜像
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14269296.html
Copyright © 2011-2022 走看看