注销主体备案与网站备案表,淘宝客网站一般用什么做的,简历模板免费下载的网页,在本地搭建wordpress推荐阅读 CSDN主页GitHub开源地址Unity3D插件分享简书地址 大家好#xff0c;我是佛系工程师☆恬静的小魔龙☆#xff0c;不定时更新Unity开发技巧#xff0c;觉得有用记得一键三连哦。
一、前言
【GameFramework框架】系列教程目录#xff1a; https://blog.csdn.net/q7… 推荐阅读 CSDN主页GitHub开源地址Unity3D插件分享简书地址 大家好我是佛系工程师☆恬静的小魔龙☆不定时更新Unity开发技巧觉得有用记得一键三连哦。
一、前言
【GameFramework框架】系列教程目录 https://blog.csdn.net/q764424567/article/details/135831551
最近好忙鸽了好久。不能颓废了继续发力。
今天分享的是GF框架的File System文件系统。
二、正文
2-1、介绍
首先引用比较官方的说法 GF的FileSystem虚拟文件系统使用类似磁盘存储的感念对零散的文件进行集中管理优化资源加载时产生的内存分配也可以对资源进行局部片段加载极大的提升了资源加载的性能。 通俗点讲就是我这个模块就是加载磁盘文件的优化加载时产生的内存分配。
那么它是怎么优化的呢 比如说一个文件夹里面有几千个文件传输的话会非常的慢因为普通的文件系统是一个一个读取写入每次读取写入都是一次磁盘IO所以花费大量时间而这个文件系统将会将整个文件夹当成一个压缩包然后再进行读取写入那么就减少了大量的IO提高了性能。 2-2、使用说明
查看文件是否存在
using GameFramework.FileSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;public class TestFileSystem : MonoBehaviour
{void Start(){FileSystemComponent fileSystemComponent GameEntry.GetComponentFileSystemComponent();string fullPath System.IO.Path.Combine(Application.persistentDataPath, FileSystem.dat); 检查是否存在文件系统参数要传递的是文件系统的完整路径bool hasFileSystem fileSystemComponent.HasFileSystem(fullPath);Debug.Log(hasFileSystem);}
}读写文件:
using GameFramework.FileSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityGameFramework.Runtime;public class TestFileSystem : MonoBehaviour
{void Start(){FileSystemComponent fileSystemComponent GameEntry.GetComponentFileSystemComponent();// 要创建的文件系统的完整路径string fullPath Path.Combine(Application.persistentDataPath, FileSystem.dat);// 要创建的文件系统最大能容纳文件数量int maxFileCount 16;// 要创建的文件系统最大能容纳的块数据数量int maxBlockCount 256;// 创建文件系统使用读写模式进行访问IFileSystem fileSystem fileSystemComponent.CreateFileSystem(fullPath, FileSystemAccess.ReadWrite, maxFileCount, maxBlockCount);// 将字节数组写入指定文件byte[] buffer new byte[1024]; // 读取文件片段使用的 buffer用此方式能够复用 buffer 来消除 GCAllocbool result fileSystem.WriteFile(FileName.dat, buffer);// 将流写入指定文件FileStream fs new FileStream(FileName.dat, FileMode.Create, FileAccess.Write);bool result2 fileSystem.WriteFile(FileName.dat, fs);// 将物理文件写入指定文件bool result3 fileSystem.WriteFile(FileName.dat, E:\PhysicalFileName.dat);}
}2-3、实现及代码分析
File System虽说是一个独立的模块但是一般不直接使用通常要配合其他模块比较典型的例子就是资源打包和读取额时候。
因为在做资源更新的时候会将资源进行整理分包加载资源管理器ResourceComponent继承了File System文件系统只需要在构建ResoucrceCollection.xml时在对应的资源Resources标签下指定FileSystem属性即可 Build资源的时候会自动将此Resource放入指定的文件系统运行加载资源时也会自动去对应的FileSystem文件中进行加载。
下面就以打包和加载来分析FileSystem文件系统的代码实现。
2-3-1、打包代码分析
1、点击Start Build Resource按钮后会调用BuildResource函数
/// 资源生成器。
internal sealed class ResourceBuilder : EditorWindowprivate void BuildResources(){if (m_Controller.BuildResources()){Debug.Log(Build resources success.);SaveConfiguration();}else{Debug.LogWarning(Build resources failure.);}}
}2、创建文件系统存储在文件系统中
public sealed partial class ResourceBuilderController
{public bool BuildResources(){......AssetBundleManifest assetBundleManifest BuildPipeline.BuildAssetBundles(workingPath, assetBundleBuildDatas, buildAssetBundleOptions, GetBuildTarget(platform));......if (OutputPackageSelected){CreateFileSystems(m_ResourceDatas.Values, outputPackagePath, m_OutputPackageFileSystems);}......
}3、创建文件系统后遍历所有的打包资源获取标记了FileSystem标签的项加入文件系统
private void CreateFileSystems(IEnumerableResourceData resourceDatas, string outputPath, Dictionarystring, IFileSystem outputFileSystem)
{outputFileSystem.Clear();string[] fileSystemNames GetFileSystemNames(resourceDatas);if (fileSystemNames.Length 0 m_FileSystemManager null){m_FileSystemManager GameFrameworkEntry.GetModuleIFileSystemManager();m_FileSystemManager.SetFileSystemHelper(new FileSystemHelper());}foreach (string fileSystemName in fileSystemNames){int fileCount GetResourceIndexesFromFileSystem(resourceDatas, fileSystemName).Length;string fullPath Utility.Path.GetRegularPath(Path.Combine(outputPath, Utility.Text.Format({0}.{1}, fileSystemName, DefaultExtension)));IFileSystem fileSystem m_FileSystemManager.CreateFileSystem(fullPath, FileSystemAccess.Write, fileCount, fileCount);outputFileSystem.Add(fileSystemName, fileSystem);}
}4、写入文件系统把每个经过加密处理的资源写入到指定的文件系统中
public sealed partial class ResourceBuilderController
{private bool ProcessOutput(Platform platform, string outputPackagePath, string outputFullPath, string outputPackedPath, bool additionalCompressionSelected, string name, string variant, string fileSystem, ResourceData resourceData, byte[] bytes, int length, int hashCode, int compressedLength, int compressedHashCode){string fullNameWithExtension Utility.Text.Format({0}.{1}, GetResourceFullName(name, variant), GetExtension(resourceData));if (OutputPackageSelected){if (!string.IsNullOrEmpty(fileSystem)){if (!m_OutputPackageFileSystems[fileSystem].WriteFile(fullNameWithExtension, bytes)){return false;}}}}
}2-3-2、加载代码分析
1、使用ResourceManager加载资源
internal class ResourceManager : GameFrameworkModule, IResourceManager
{/// 异步加载资源。public void LoadAsset(string assetName, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData){m_ResourceLoader.LoadAsset(assetName, assetType, priority, loadAssetCallbacks, userData);}
}2、使用Resource任务池加载使用多个LoadResourceAgent代理异步加载资源每个资源加载都是一个LoadAssetTask每个任务开始时都会分配一个LoadResourceAgent代理
internal sealed class TaskPoolT where T : TaskBase
{public void Update(float elapseSeconds, float realElapseSeconds){ProcessRunningTasks(elapseSeconds, realElapseSeconds);ProcessWaitingTasks(elapseSeconds, realElapseSeconds);}//处理任务池等待列表private void ProcessWaitingTasks(float elapseSeconds, float realElapseSeconds){LinkedListNodeT current m_WaitingTasks.First;while (current ! null FreeAgentCount 0){ITaskAgentT agent m_FreeAgents.Pop();LinkedListNodeITaskAgentT agentNode m_WorkingAgents.AddLast(agent);T task current.Value;LinkedListNodeT next current.Next;//LoadResourceAgent代理开始读取资源StartTaskStatus status agent.Start(task);current next;...}}
}3、资源文件的读取使用了资源加载代理辅助器分别为LoadFromFile、LoadFromMemory两种不同的加载方式
/// 默认加载资源代理辅助器。
public class DefaultLoadResourceAgentHelper : LoadResourceAgentHelperBase, IDisposable
{/// 通过加载资源代理辅助器开始异步读取资源文件。public override void ReadFile(string fullPath){m_FileAssetBundleCreateRequest AssetBundle.LoadFromFileAsync(fullPath);}/// 通过加载资源代理辅助器开始异步读取资源文件。public override void ReadFile(IFileSystem fileSystem, string name){FileInfo fileInfo fileSystem.GetFileInfo(name);m_FileAssetBundleCreateRequest AssetBundle.LoadFromFileAsync(fileSystem.FullPath, 0u, (ulong)fileInfo.Offset);}/// 通过加载资源代理辅助器开始异步读取资源二进制流。public override void ReadBytes(string fullPath){m_UnityWebRequest UnityWebRequest.Get(Utility.Path.GetRemotePath(fullPath));m_UnityWebRequest.SendWebRequest();}/// 通过加载资源代理辅助器开始异步读取资源二进制流。public override void ReadBytes(IFileSystem fileSystem, string name){byte[] bytes fileSystem.ReadFile(name);m_LoadResourceAgentHelperReadBytesCompleteEventHandler(this, LoadResourceAgentHelperReadBytesCompleteEventArgs.Create(bytes));}
}4、最后通过LoadMain从Bundle中读取资源最后运行监听事件LoadComplete加载全部完成
/// 默认加载资源代理辅助器。
public class DefaultLoadResourceAgentHelper : LoadResourceAgentHelperBase, IDisposable
{/// 通过加载资源代理辅助器开始异步读取资源文件。public override void ReadFile(string fullPath){m_FileAssetBundleCreateRequest AssetBundle.LoadFromFileAsync(fullPath);}/// 通过加载资源代理辅助器开始异步读取资源文件。public override void ReadFile(IFileSystem fileSystem, string name){FileInfo fileInfo fileSystem.GetFileInfo(name);m_FileAssetBundleCreateRequest AssetBundle.LoadFromFileAsync(fileSystem.FullPath, 0u, (ulong)fileInfo.Offset);}/// 通过加载资源代理辅助器开始异步读取资源二进制流。public override void ReadBytes(string fullPath){m_UnityWebRequest UnityWebRequest.Get(Utility.Path.GetRemotePath(fullPath));m_UnityWebRequest.SendWebRequest();}/// 通过加载资源代理辅助器开始异步读取资源二进制流。public override void ReadBytes(IFileSystem fileSystem, string name){byte[] bytes fileSystem.ReadFile(name);m_LoadResourceAgentHelperReadBytesCompleteEventHandler(this, LoadResourceAgentHelperReadBytesCompleteEventArgs.Create(bytes));}
}三、后记
如果觉得本篇文章有用别忘了点个关注关注不迷路持续分享更多Unity干货文章。 你的点赞就是对博主的支持有问题记得留言
博主主页有联系方式。
博主还有跟多宝藏文章等待你的发掘哦
专栏方向简介Unity3D开发小游戏小游戏开发教程分享一些使用Unity3D引擎开发的小游戏分享一些制作小游戏的教程。Unity3D从入门到进阶入门从自学Unity中获取灵感总结从零开始学习Unity的路线有C#和Unity的知识。Unity3D之UGUIUGUIUnity的UI系统UGUI全解析从UGUI的基础控件开始讲起然后将UGUI的原理UGUI的使用全面教学。Unity3D之读取数据文件读取使用Unity3D读取txt文档、json文档、xml文档、csv文档、Excel文档。Unity3D之数据集合数据集合数组集合数组、List、字典、堆栈、链表等数据集合知识分享。Unity3D之VR/AR虚拟仿真开发虚拟仿真总结博主工作常见的虚拟仿真需求进行案例讲解。Unity3D之插件插件主要分享在Unity开发中用到的一些插件使用方法插件介绍等Unity3D之日常开发日常记录主要是博主日常开发中用到的用到的方法技巧开发思路代码分享等Unity3D之日常BUG日常记录记录在使用Unity3D编辑器开发项目过程中遇到的BUG和坑让后来人可以有些参考。