Giter VIP home page Giter VIP logo

unityfs's Introduction

unityfs

与旧版本(0.9.4)不兼容

提供一个轻量实用的资源和文件访问层, 简化资源热更新项目的开发流程. 开发过程中可以使用编辑器模式直接访问工程内的任意资源, 并在发布时无缝切换到通过 AssetBundle 访问资源. 配置文件与脚本代码独立压缩打包, 运行期直接读取压缩包访问文件, 且不受 Unity 文件命名限制.

目标特性

  • 资源管理
    • 异步加载资源/场景
    • 自动管理资源加载/卸载
    • 自动管理资源更新
    • 资源对象池
  • 下载
    • 按优先级下载资源
    • 断点续传
    • 多下载源重试
    • 支持边玩边下
    • 空闲时自动后台下载
  • 打包
    • 可视化打包管理
    • 按条件分包
    • 按资源数量分包
    • 根据资源访问分析自动配置首包
    • 资源包加密
    • 冗余资源报告

进度

功能基本完成.

Examples

初始化

    // 可用下载地址列表 (会依次重试, 次数超过地址数量时反复重试最后一个地址)
    // 适用于 CDN 部署还没有全部起作用时, 退化到直接文件服务器地址
    var urls = UnityFS.Utils.Helpers.URLs(
        // "http://localhost:8081/",
        "http://localhost:8080/"
    );

    // 下载存储目录
    var dataPath = string.IsNullOrEmpty(Application.temporaryCachePath) ? Application.persistentDataPath : Application.temporaryCachePath;
    var localPathRoot = Path.Combine(dataPath, "bundles");
    Debug.Log($"open localPathRoot: {localPathRoot}");

    UnityFS.ResourceManager.Initialize(developMode, localPathRoot, urls, this);
    UnityFS.ResourceManager.Open();

资源加载

UnityFS.ResourceManager 返回的资源会自动管理加载卸载与资源包依赖:

  • IFileSystem
  • UAsset

当资源实例不存在强引用时将进入GC流程, 对应资源包将被自动卸载.

加载文件 (脚本/配置等)

IFileSystem 中的文件可以同步加载 (实际对应zip包中的文件).

// 获取核心脚本代码包
var fs = UnityFS.ResourceManager.FindFileSystem("Assets/Examples/Scripts/code.js");
fs.completed += self =>
{
    // 可以在这里由脚本接管后续启动流程
    // 需要保持对 fs 的引用, 无引用时将在GC后自动释放对应包
    ScriptEngine.Initialize(fs); 
    ScriptEngine.RunScript("Assets/Examples/Scripts/code.js");

    // 判断文件是否存在
    var exists = fs.Exists("Assets/Examples/not_exist.file");
    Debug.Log($"file exists? {exists}");

    // 读取文件内容 (zip包中的文件可以同步读取)
    var data = fs.ReadAllBytes("Assets/Examples/Config/test.txt");
    Debug.Log(System.Text.Encoding.UTF8.GetString(data));
};
// 可以用 LoadAsset 载入文件内容 (通常位于zip包中)
var testFile = UnityFS.ResourceManager.LoadAsset("Assets/Examples/Config/test.txt");
testFile.completed += self =>
{
    var text = System.Text.Encoding.UTF8.GetString(testFile.ReadAllBytes());
    Debug.Log($"用 LoadAsset 形式加载一个文件: {text}");
};

加载资源 (异步)

// 方式1. 得到原始 UAsset 对象
UnityFS.ResourceManager.LoadAsset("Assets/Examples/Prefabs/Cube 1.prefab", self =>
{
    UnityFS.Utils.AssetHandle.CreateInstance(self, 5.0f);
});

// 方式2. 通过辅助方法直接创建 GameObject (返回的是一个代理 GameObject)
UnityFS.ResourceManager.Instantiate("Assets/Examples/Prefabs/Cube 1.prefab").DestroyAfter(10.0f);

加载场景 (异步)

// LoadScene/LoadSceneAdditive 分别对应 Single/Additive 场景加载模式
var scene = UnityFS.ResourceManager.LoadSceneAdditive("Assets/Examples/Scenes/test2.unity");
scene.completed += self =>
{
    Debug.Log("scene loaded");
};

StartCoroutine(UnityFS.Utils.Helpers.InvokeAfter(() =>
    {
        // 场景对象需要手工卸载 (异步完成)
        // 场景对应的资源包则在所有场景实例对象GC后自动卸载
        scene.UnloadScene(); 
        scene = null;
    }, 20f)
);

打包编辑器

editorwindow

资源冗余分析

buildreport

Unity 版本

Unity 2017.4+

License

MIT

unityfs's People

Contributors

dependabot[bot] avatar ialex32x avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

unityfs's Issues

有个小bug

AssetHandle脚本的CreateInstance方法中
public static AssetHandle CreateInstance(UAsset asset, float ttl) { var gameObject = Object.Instantiate(asset.GetObject()) as GameObject; return UnityFS.Utils.AssetHandle.Attach(gameObject, asset, 5.0f); }
这段代码中的5.0f之后删除应该是测试时留的没修改掉吧?应该使用上面定义的参数ttl吧。如果不修改为ttl。prefab每次创建5秒之后就会被强制删除。
还有一个问题,能否获取所有资源的路径(有些物品的路径可能不在同一个资源目录下面,动态获取资源的时候就无法知道路径了)在登陆的时候把所有路径存起来,需要获取资源的时候直接通过资源名就可以了,路径通过资源名在字典中取。

请问如何读取TexturePacker图集中指定图片?

加载资源时如何读取TexturePacker图集内的图片。
_ var sprite = UnityFS.ResourceManager.LoadAsset(atlasPath);
sprite.completed += self =>
{
if (func != null) func.Call(sprite.GetObject());
};
_
使用上面的代码读取只能读取到图集中的第一张图片,无法读取图集中指定的图片。

有没有办法登录游戏之后所有的加载均使用同步的方式?

有没有办法登录游戏之后所有的资源加载均使用同步的方式?现在使用异步加载资源,很多需要同步加载的资源会有问题,例如:释放一个技能,点击释放技能之后,零点几秒之后才会显示出来特效,但是技能的动作已经播放完毕。全部使用异步的方式加载,卡顿非常明显。而且现在如果需要在异步的加载方法里面再嵌套一个异步加载资源,也是不允许的,在异步中使用同步的时候,也会出现加载不到资源的情况。UnityFS.ResourceManager.Instantiate(“asset/ss”).target;如果能在登录游戏之后,资源采用异步的方式加载,应该会好很多,也会减少很多代码吧。

询问一个问题。

在Build Packages的时候,打包的资源是不是必须有一个要要设置为FileList类型?如果不设置fileListManifest就会报空,无法打包。
希望大佬可以出一个详细的打包教程。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.