ResourceManager(五)—— ResourceCommon

目录为:Assets/Scripts/ResourceManager/
ResourceCommon.cs

ResourceCommon.cs

//#define Resource_UseLog

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

//这个类和BuildCommon很像
public class ResourceCommon
{
    public static string textFilePath = Application.streamingAssetsPath;

    public static string assetbundleFilePath = Application.dataPath + "/assetbundles/";

    public static string assetbundleFileSuffix = ".bytes";

    public static string DEBUGTYPENAME = "Resource";

    //public static string mServerAssetPath = @"http://192.168.123.1/assetbundles/";
    //Server AssetPath
    public static string mServerAssetPath = "file:///c:/";

    //封装了一些Debug.Log
    public static void Log(string fileName)
    {
        //#if Resource_UseLog
        Debug.Log (fileName, DEBUGTYPENAME, true);
        //#endif
    }

    //根据资源路径获取资源名称
    public static string getResourceName(string resPathName)
    {
        int index = resPathName.LastIndexOf ("/");
        if (index == -1)
        {
            return resPathName;
        }
        else
        {
            return resPathName.Substring (index + 1, resPathName.Length - index - 1);
        }
    }

    //这个挺奇怪的,去掉suffix然后返回
    public static string getFileName(string fileName)
    {
        int index = fileName.IndexOf (".");
        if (-1 == index)
        {
            throw new Exception ("can not find !!!");
        }

        return fileName.Substring (0, index);
    }

    //返回单纯的fileName
    //例如Scenes/Scene1.unity.bytes
    //返回"Scene1.unity"
    public static string getFileName(string filePath, bool suffix)
    {
        if (!suffix)
        {
            string path = filePath.Replace ("\\", "/");
            int index = path.LastIndexOf ("/");
            if (-1 == index)
            {
                throw new Exception ("can not find !!!");
            }

            int index2 = path.LastIndexOf (".");
            if (-1 == index2)
            {
                throw new Exception ("can not find !!!");
            }

            return path.Substring (index + 1, index2 - index - 1);
        }
        else
        {
            string path = filePath.Replace ("\\", "/");
            int index = path.LastIndexOf ("/");
            if (-1 == index)
            {
                throw new Exception ("can not find !!!");
            }

            return path.Substring (index + 1, path.Length - index - 1);
        }
    }

    //返回文件夹名,但是传进来的必须是长目录的文件夹路径
    public static string getFolder(string path)
    {
        path = path.Replace ("\\", "/");
        int index = path.LastIndexOf ("/");
        if (-1 == index)
        {
            throw new Exception ("can not find !!!");
        }

        return path.Substring (index + 1, path.Length - index - 1);
    }

    //返回后缀
    public static string getFileSuffix(string filePath)
    {
        int index = filePath.LastIndexOf (".");
        if (-1 == index)
        {
            throw new Exception ("can not find Suffix !!! the filePath is : " + filePath);
        }

        return filePath.Substring (index + 1, filePath.Length - index - 1);
    }

    //获取所给目录下的所有文件,recursion表示是否递归
    public static void getFiles(string path, bool recursion, Dictionary<string, List<string>> allFiles, bool useSuffix, string suffix)
    {
        if (recursion)
        {
            string[] dirs = Directory.GetDirectories (path);
            foreach (string dir in dirs)
            {
                if (getFolder(dir) == ".svn")
                {
                    continue;
                }

                getFiles (dir, recursion, allFiles, useSuffix, suffix);
            }
        }

        string[] files = Directory.GetFiles (path);
        foreach (string file in files)
        {
            string fileSuffix = getFileSuffix (file);
            if (fileSuffix == "meta" || fileSuffix == "dll")
            {
                continue;
            }

            if (useSuffix && fileSuffix != suffix)
            {
                continue;
            }

            string relativePath = file.Replace ("\\", "/");
            relativePath = relativePath.Replace (Application.dataPath, "");
            string fileName = getFileName (file, true);
            if (allFiles.ContainsKey(fileName))
            {
                allFiles [fileName].Add (relativePath);
            }
            else
            {
                List<string> list = new List<string> ();
                list.Add (relativePath);
                allFiles.Add (fileName, list);
            }
        }
    }

    public static void CheckFolder(string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory (path);
        }
    }

    //除去了文件名
    public static string getPath(string filePath)
    {
        string path = filePath.Replace ("\\", "/");
        int index = path.LastIndexOf ("/");
        if (-1 == index)
        {
            throw new Exception ("can not find !!!");
        }

        return path.Substring (0, index);
    }

    public static string getLocalPath(string path)
    {
        string localPath = string.Format ("{0}/{1}", Application.persistentDataPath, path);
        if (!File.Exists(localPath))
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                localPath = string.Format ("{0}/{1}", Application.streamingAssetsPath, path);
            }
            else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                localPath = string.Format ("file://{0}/{1}", Application.streamingAssetsPath, path);
            }

            return localPath;
        }

        return "file:///" + localPath;
    }

    //Server Path
    public static string getServerPath(string path)
    {
        return string.Format ("{0}/{1}", mServerAssetPath, path);
    }

    //以bytes形式返回AssetBundle资源
    public static byte[] getAssetBundleFileBytes(string path, ref int size)
    {
        string localPath;

        //Android和IOS环境使用的沙箱目录
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            localPath = string.Format ("{0}/{1}", Application.persistentDataPath, path + ResourceCommon.assetbundleFileSuffix);
        }
        //Windows下使用assetbundle资源目录
        else
        {
            localPath = ResourceCommon.assetbundleFilePath + path + ResourceCommon.assetbundleFileSuffix;
        }

        //首先检测沙箱目录中是否有更新资源
        if (File.Exists(localPath))
        {
            try
            {
                FileStream bundleFile = File.Open(localPath, FileMode.Open, FileAccess.Read);
                byte[] bytes = new byte[bundleFile.Length];
                bundleFile.Read(bytes, 0, (int)bundleFile.Length);
                size = (int)bundleFile.Length;
                bundleFile.Close();

                return bytes;
            }
            catch (Exception e)
            {
                Debug.LogError (e.Message, "CreateAssetBundle", true);
                return null;
            }
        }
        //原始包中
        else
        {
            TextAsset bundleFile = Resources.Load (path) as TextAsset;

            if (null == bundleFile)
            {
                Debug.LogError ("load : " + path + " bundleFile error!!!", "CreateAssetBundle", true);
            }

            size = bundleFile.bytes.Length;

            return bundleFile.bytes;
        }
    }

    //-------------------------------------------------------------------------------------

    public delegate void Handle_CreateFromMemory(AssetBundleCreateRequest request, int size);
    public static IEnumerator _CreateFromMemory(string path, Handle_CreateFromMemory handle)
    {
        int size = 0;
        AssetBundleCreateRequest bundleRequest = AssetBundle.CreateFromMemory (ResourceCommon.getAssetBundleFileBytes (path, ref size));
        yield return bundleRequest;
        handle (bundleRequest, size);
    }

    public delegate void HandleFinishLoadAsyncFromAssetBundle(AssetBundleRequest request);
    public static IEnumerator LoadAsyncFromAssetBundle(AssetBundle assetbundle, string name, System.Type type, HandleFinishLoadAsyncFromAssetBundle handle)
    {
        AssetBundleRequest request = assetbundle.LoadAsync (name, type);
        yield return request;
        handle (request);
    }

    //-------------------------------------------------------------------------------------------------

    //仅用于win_editor
    //用于Debug
    public static AssetBundle loadAssetBundleImmediateForDebug(string path)
    {
        string localPath = string.Format ("file://{0}/{1}", assetbundleFilePath, path + ResourceCommon.assetbundleFileSuffix);
        WWW www = new WWW (localPath);

        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError ("the assetbundle path is error: " + path + "\nand the localPath is : " + localPath);
        }

        while (true)
        {
            byte[] b = www.bytes;
            if (www.isDone)
            {
                break;
            }
        }

        return www.assetBundle;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值