Пример #1
0
    // 打开一个二进制文件,fileName为绝对路径
    public static void openFile(string fileName, ref byte[] fileBuffer)
    {
        try
        {
#if !UNITY_ANDROID || UNITY_EDITOR
            FileStream fs       = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            int        fileSize = (int)fs.Length;
            fileBuffer = new byte[fileSize];
            fs.Read(fileBuffer, 0, fileSize);
            fs.Close();
            fs.Dispose();
#else
            // 安卓平台如果要读取StreamingAssets下的文件,只能使用AssetManager
            if (startWith(fileName, CommonDefine.F_STREAMING_ASSETS_PATH))
            {
                // 改为相对路径
                fileName   = fileName.Substring(CommonDefine.F_STREAMING_ASSETS_PATH.Length, fileName.Length - CommonDefine.F_STREAMING_ASSETS_PATH.Length);
                fileBuffer = AndroidAssetLoader.loadAsset(fileName);
            }
            // 安卓平台如果要读取persistentDataPath的文件,则可以使用File
            else if (startWith(fileName, CommonDefine.F_PERSISTENT_DATA_PATH))
            {
                fileBuffer = AndroidAssetLoader.loadFile(fileName);
            }
            else
            {
                UnityUtility.logError("openFile invalid path : " + fileName);
            }
#endif
        }
        catch (Exception)
        {
            UnityUtility.logInfo("open file failed! filename : " + fileName);
        }
    }
Пример #2
0
    // 同步加载资源包
    public void loadAssetBundle(bool persistentFirst)
    {
        if (mLoaded != LOAD_STATE.LS_UNLOAD)
        {
            return;
        }
        // 先确保所有依赖项已经加载
        foreach (var info in mParents)
        {
            info.Value.loadAssetBundle(persistentFirst);
        }
        // 然后加载AssetBundle
#if UNITY_ANDROID && !UNITY_EDITOR
        byte[] assetBundleBuffer = null;
        // 先去persistentDataPath中查找资源
        if (persistentFirst)
        {
            assetBundleBuffer = AndroidAssetLoader.loadFile(CommonDefine.F_PERSISTENT_DATA_PATH + mBundleName + CommonDefine.ASSET_BUNDLE_SUFFIX);
        }
        // 找不到再去StreamingAssets下查找
        if (assetBundleBuffer == null)
        {
            assetBundleBuffer = AndroidAssetLoader.loadAsset(mBundleName + CommonDefine.ASSET_BUNDLE_SUFFIX);
        }
        mAssetBundle = AssetBundle.LoadFromMemory(assetBundleBuffer);
#else
        // 先去persistentDataPath中查找资源
        if (persistentFirst && isFileExist(CommonDefine.F_PERSISTENT_DATA_PATH + mBundleName + CommonDefine.ASSET_BUNDLE_SUFFIX))
        {
            mAssetBundle = AssetBundle.LoadFromFile(CommonDefine.F_PERSISTENT_DATA_PATH + mBundleName + CommonDefine.ASSET_BUNDLE_SUFFIX);
        }
        // 找不到再去StreamingAssets下查找
        if (mAssetBundle == null && isFileExist(CommonDefine.F_STREAMING_ASSETS_PATH + mBundleName + CommonDefine.ASSET_BUNDLE_SUFFIX))
        {
            mAssetBundle = AssetBundle.LoadFromFile(CommonDefine.F_STREAMING_ASSETS_PATH + mBundleName + CommonDefine.ASSET_BUNDLE_SUFFIX);
        }
#endif
        if (mAssetBundle == null)
        {
            logError("can not load asset bundle : " + mBundleName);
            return;
        }
        // 加载其中的所有资源
        List <string> assetNameList = new List <string>(mAssetList.Keys);
        int           assetCount    = assetNameList.Count;
        for (int i = 0; i < assetCount; ++i)
        {
            UnityEngine.Object obj = mAssetBundle.LoadAsset(CommonDefine.P_RESOURCE_PATH + assetNameList[i]);
            mAssetList[assetNameList[i]].mAssetObject = obj;
        }
        mLoaded = LOAD_STATE.LS_LOADED;
        afterLoaded();
    }
Пример #3
0
    // 打开一个二进制文件,fileName为绝对路径,返回值为文件长度
    // 使用完毕后需要使用releaseFileBuffer回收文件内存
    public static int openFile(string fileName, out byte[] fileBuffer, bool errorIfNull)
    {
        fileBuffer = null;
        try
        {
#if !UNITY_ANDROID || UNITY_EDITOR
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            if (fs == null)
            {
                if (errorIfNull)
                {
                    UnityUtility.log("open file failed! filename : " + fileName);
                }
                return(0);
            }
            int fileSize = (int)fs.Length;
            fileBuffer = FrameBase.mBytesPoolThread.newBytes(getGreaterPow2(fileSize));
            fs.Read(fileBuffer, 0, fileSize);
            fs.Close();
            fs.Dispose();
            return(fileSize);
#else
            // 安卓平台如果要读取StreamingAssets下的文件,只能使用AssetManager
            if (startWith(fileName, FrameDefine.F_STREAMING_ASSETS_PATH))
            {
                // 改为相对路径
                fileName   = fileName.Substring(FrameDefine.F_STREAMING_ASSETS_PATH.Length, fileName.Length - FrameDefine.F_STREAMING_ASSETS_PATH.Length);
                fileBuffer = AndroidAssetLoader.loadAsset(fileName, errorIfNull);
            }
            // 安卓平台如果要读取persistentDataPath的文件,则可以使用File
            else if (startWith(fileName, FrameDefine.F_PERSISTENT_DATA_PATH))
            {
                fileBuffer = AndroidAssetLoader.loadFile(fileName, errorIfNull);
            }
            else
            {
                UnityUtility.logError("openFile invalid path : " + fileName);
            }
            return(fileBuffer.Length);
#endif
        }
        catch (Exception)
        {
            UnityUtility.log("open file failed! filename : " + fileName);
        }
        return(0);
    }