05
2017
10

ResourceManager(三)—— Request,ResourceAsyncOperation

目录为:Assets/Scripts/ResourceManager
Request.cs,ResourceAsyncOperation.cs

Request.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public enum RequestType
{
    LOAD,
    UNLOAD,
    LOADLEVEL,
    UNLOADLEVEL,
}

//用于资源相关的请求
class Request
{
    internal string mFileName;  //请求资源相对Assets/完整路径名称

    //ResourceType
    internal ResourceType mResourceType;

    //ResourcesManager声明的三个delegate
    internal ResourcesManager.HandleFinishLoad mHandle;
    internal ResourcesManager.HandleFinishLoadLevel mHandleLevel;
    internal ResourcesManager.HandleFinishUnLoadLevel mHandleUnloadLevel;

    //Request Type
    internal RequestType mRequestType;

    internal ResourceAsyncOperation mResourceAsyncOperation;

    //构造函数
    internal Request(string fileName, ResourceType resourceType, ResourcesManager.HandleFinishLoad handle, RequestType requestType, ResourceAsyncOperation operation)
    {
        mFileName = fileName;
        mResourceType = resourceType;
        mHandle = handle;
        mRequestType = requestType;
        mResourceAsyncOperation = operation;
    }

    internal Request(string fileName, ResourceType resourceType, ResourcesManager.HandleFinishLoadLevel handle, RequestType requestType, ResourceAsyncOperation operation)
    {
        mFileName = fileName;
        mResourceType = resourceType;
        mHandleLevel = handle;
        mRequestType = requestType;
        mResourceAsyncOperation = operation;
    }
}

ResourceAsyncOperation.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;

//异步加载资源,这个类主要用来算加载进度
public class ResourceAsyncOperation
{
    internal RequestType mRequestType;
    internal int mAllDependenciesAssetSize;
    internal int mLoadDependenciesAssetSize;
    internal bool mComplete;

    //异步操作
    public AsyncOperation asyncOperation;

    internal ResourceUnit mResource;

    //构造函数
    internal ResourceAsyncOperation(RequestType requestType)
    {
        mRequestType = requestType;
        mAllDependenciesAssetSize = 0;
        mLoadDependenciesAssetSize = 0;
        mComplete = false;
        asyncOperation = null;
        mResource = null;
    }

    public bool Complete
    {
        get
        {
            return mComplete;
        }
    }

    //算进度
    public int Progress
    {
        get
        {
            //如果已经完成
            if (mComplete)
            {
                return 100;
            }
            else if (0 == mLoadDependenciesAssetSize)
            {
                return 0;
            }
            else
            {
                //使用assetbundle
                if (ResourcesManager.Instance.UsedAssetBundle)
                {
                    //分成加载关卡或者加载单个资源两种情况
                    if (RequestType.LOADLEVEL == mRequestType)
                    {
                        //dependencies进度
                        int depsProgress = (int)(((float)mLoadDependenciesAssetSize / mAllDependenciesAssetSize) * 100);
                        int levelProgress = asyncOperation != null ? (int)((float)asyncOperation.progress * 100) : 0;

                        return (int)(depsProgress * 0.8) + (int)(levelProgress * 0.2);
                    }
                    else
                    {
                        return (int)(((float)mLoadDependenciesAssetSize / mAllDependenciesAssetSize) * 100);
                    }
                }
                //不使用assetbundle
                else
                {
                    if (RequestType.LOADLEVEL == mRequestType)
                    {
                        int levelProgress = asyncOperation != null ? (int)((float)asyncOperation.progress * 100.0f) : 0;

                        return levelProgress;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
        }
    }
}
上一篇:Android应用层通信机制 下一篇:React Native项目结构