虚拟文件系统使用类似磁盘的概念对零散文件进行集中管理,优化资源加载时产生的内存分配,甚至可以对资源进行局部片段加载,这些都将极大提升资源加载时的性能。
E神
概念
GF文件系统引入了一个虚拟文件系统的概念,实际上就是把各种物理文件整合到一个大的物理文件当中进行集中管理,这样带来的好处是集中了各文件的物理储存空间位置,增加了IO的性能。
使用实践
这次我把使用实践的位置提前了,因为虚拟文件系统是个略微抽象的概念,如果上来就直接把代码结构展开分析可能会让抽象的概念变的更加的抽象。
若能直接展示使用流程,和实际的运转效果,大家在看代码的过程中就能有个明确的方向,比如按着实际的效果去梳理代码产生这部分效果的代码流程。
构建一个虚拟文件系统
先写一个简单的用来构建指定文件系统的工具
关键函数
private void Buildfile()
{
//读取指定文件夹指定后缀所有文件信息
string path = Application.streamingAssetsPath + "\\..\\PlotEditor\\Plot";
DirectoryInfo root = new DirectoryInfo(path);
System.IO.FileInfo[] files = root.GetFiles("*.txt");
//加载文件系统
FileSystemManager = GameFramework.GameFrameworkEntry.GetModule<IFileSystemManager>();
FileSystemManager.SetFileSystemHelper(new FileSystemHelper());
//创建文件系统
IFileSystem fileSystem = FileSystemManager.CreateFileSystem(fullPath, FileSystemAccess.ReadWrite, 32, 64);
//将物理文件写入文件系统
for (int i = 0; i < files.Length; i++)
{
fileSystem.WriteFile(files[i].Name, Application.streamingAssetsPath + "\\..\\PlotEditor\\Plot\\" + files[i].Name);
}
}
PlotforGFVFSEditor
使用流程
using UnityEngine;
using UnityEditor;
using System.IO;
using GameFramework.FileSystem;
namespace MDDGameFramework.MDDNodeEditor
{
public class PlotforGFVFSEditor : EditorWindow
{
IFileSystemManager FileSystemManager;
string fullPath = Path.Combine(Application.streamingAssetsPath, "PlotFileSystem.dat");
public static PlotforGFVFSEditor nodeEditorWindow;
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
[MenuItem("Tools/导入工具 _&E")]
public static void OpenEditorWindow()
{
PlotforGFVFSEditor window = (PlotforGFVFSEditor)EditorWindow.GetWindow(typeof(PlotforGFVFSEditor), false, "编辑窗口");
window.Show();
PlotforGFVFSEditor.nodeEditorWindow = window;
}
public void OnGUI()
{
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUIStyle largeLale = new GUIStyle();
largeLale.normal.background = null;
largeLale.normal.textColor = new Color(1, 1, 1);
largeLale.fontSize = 15;
largeLale.fontStyle = FontStyle.BoldAndItalic;
GUILayout.Label("构建文件工具", largeLale);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.BeginVertical();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("构建文件", new[] { GUILayout.Height(20), GUILayout.Width(100) }))
{
Buildfile();
}
//GUILayout.Space(15);
//GUILayout.Button("重新读取配置", new[] { GUILayout.Height(20), GUILayout.Width(100) });
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
private void Buildfile()
{
//读取指定文件夹指定后缀所有文件信息
string path = Application.streamingAssetsPath + "\\..\\PlotEditor\\Plot";
DirectoryInfo root = new DirectoryInfo(path);
System.IO.FileInfo[] files = root.GetFiles("*.txt");
//加载文件系统
FileSystemManager = GameFramework.GameFrameworkEntry.GetModule<IFileSystemManager>();
FileSystemManager.SetFileSystemHelper(new FileSystemHelper());
//创建文件系统
IFileSystem fileSystem = FileSystemManager.CreateFileSystem(fullPath, FileSystemAccess.ReadWrite, 32, 64);
//将物理文件写入文件系统
for (int i = 0; i < files.Length; i++)
{
fileSystem.WriteFile(files[i].Name, Application.streamingAssetsPath + "\\..\\PlotEditor\\Plot\\" + files[i].Name);
}
}
private void OnDestroy()
{
FileSystem = null;
FileSystemManager = null;
System.GC.Collect();
}
void OnInspectorUpdate()
{
this.Repaint();
}
private sealed class FileSystemHelper : IFileSystemHelper
{
public FileSystemStream CreateFileSystemStream(string fullPath, FileSystemAccess access, bool createNew)
{
return new CommonFileSystemStream(fullPath, access, createNew);
}
}
}
}
Comments | NOTHING