首页 > 其他分享 >Unity 在AssetPostprocessor内使用AssetDataBase是不安全的行为(尤其在Build前进行Library的删除)

Unity 在AssetPostprocessor内使用AssetDataBase是不安全的行为(尤其在Build前进行Library的删除)

时间:2023-07-05 18:45:58浏览次数:50  
标签:AssetDatabase imported assets Unity Library AssetDataBase asset before

https://docs.unity3d.com/Manual/AssetDatabaseCustomizingWorkflow.html

在Build前进行Library的删除,读取Asset是不安全的行为,需要使用C#的IOSystem进行操作

Asset Import Order

If you are scripting using the AssetDatabase class, it’s important to understand how the order of Unity’s import processes can affect your scripts, otherwise you may get unexpected results. The order is as follows:

  1. Import Script Assets (.cs, .dll, .asmdef files)
  2. Compilation
  3. Domain reload
  4. InitializeOnLoad callback
  5. Import all other assets

Scripts are always imported and compiled before all other regular assets, because the Editor needs to know whether there are custom asset post-processors or scripted importers in the project. This ensures that the Editor uses any new or changed importers or post-processors when importing the rest of the non-script assets.

The InitializeOnLoad callback is often used to run some code on project startup or when scripts change. As shown in the list above, this callback is run after Unity reloads the domain, but before it starts importing assets. This means if you’re using the [InitializeOnLoad] callback to access assets, your code is executed before the current asset import cycle completes. In particular:

  • For assets being imported for the first time, methods like AssetDatabase.LoadAssetAtPath, AssetDatabase.FindAssets, Shader
    .Find, Resources.Load will return null, since those assets have not yet been imported. 

  • For assets that have already been imported at least once, methods like AssetDatabase.LoadAssetAtPath, AssetDatabase.FindAssets, Shader.Find, Resources.Load will return the previous (outdated) version of the asset if it was modified before reloading the domain, since domain reload occurs before the regular asset import phase.

When you are writing scripted importers, asset pre-processors, and asset post-processors, you should not make your code assume that other specific assets are already imported according to any particular order. When importing, Unity groups assets into queues by type, and while the types are imported in a predefined order, assets within a queue of the same type are imported in an arbitrary order unless you use ScriptedImporter.GatherDependenciesFromSourceFile. Using GatherDependenciesFromSourceFile also creates a dependency between the assets, so if one asset is modified, the other that depends on it is reimported.

标签:AssetDatabase,imported,assets,Unity,Library,AssetDataBase,asset,before
From: https://www.cnblogs.com/sun-shadow/p/17529534.html

相关文章

  • [Unity3D]Unity+Android交互教程——让手机"动"起来
    更多教程请访问:http://dingxiaowei.cn/ 想要用Unity实现一个二维码扫描的功能,然后网上找插件,找到一个貌似叫EasyCodeScanner,但下载下来用用,真不好使,一导入运行就报错,调好错了再运行发现点按钮没反应,反复试了几遍发现还是没反应,没办法看源码,结果发现只实现了IOS部分,没有Android部......
  • A Go library implementing an FST (finite state transducer)——mark下
    https://github.com/couchbaselabs/vellumBuildinganFSTTobuildanFST,createanewbuilderusingthe New() method.Thismethodtakesan io.Writer asanargument.AstheFSTisbeingbuilt,datawillbestreamedtothewriterassoonaspossible.Withthi......
  • unity协程(IEnumerator)开始和结束
    仅用于记录遇到的问题和解决方案。快速阅览:一、结束协程无效:协程方法需要单独存一份privateIEnumeratormyTest,再开始和结束不直接传入方法名,而是使用这份保存的myTest进行开始和结束。二、再次开启协程时少跑了几行代码:再次开始同一个方法名的协程时,不是从第一句代码开始执行......
  • unity前后端通信 json解析 HTTP POST(BestHttp插件实现)
    1.配置请求体中的Json字符串1request_data=newRequestBodyClass();2request_data.a="Json第一个字段";3request_data.b="Json第二个字段";4stringbody=JsonUtility.ToJson(request_data);2.实例化HTTPRequest,请求头方法设置为POST,并且设置请求头中类型为json......
  • 为什么Unity使用AssetBundle热更的时候要剔除掉.mainfest文件
    1)为什么Unity使用AssetBundle热更的时候要剔除掉.mainfest文件​2)Addressable热更,断点续传问题3)在线性空间,使用后处理解决UI混合问题这是第341篇UWA技术知识分享的推送,精选了UWA社区的热门话题,涵盖了UWA问答、社区帖子等技术知识点,助力大家更全面地掌握和学习。UWA社区主页:comm......
  • Unity3D:工具栏
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生工具栏在UnityEditor顶部可以看到工具栏。工具栏不是窗口,是Unity界面中唯一无法重新排列的部分。有关场景视图中的其他工具,请参阅叠加。工具栏显示在Editor顶部控制:描述:从帐户下拉菜单访问您的......
  • Unity Visual Studio 常用的自带配置以及快捷键
    ​完整文档->UnityVisualStudio常用的自带配置以及快捷键一、内联提示 不等号字体 快速创建新文件快捷键:Ctrl+shift+A 选择下一项与多行光标Alt+左键功能增强需安装插件Select Next Occurrence 拆分窗口窗口右上角 折叠大纲​......
  • Unity3D高级编程主程手记 学习笔记四:用户界面
     用户界面(UI)是游戏项目中重要的组成部分。面对一个从零开始的项目,首先要选择选用哪个UI系统作为主框架。主流公司里最常用的UI系统有:NGUI,UGUI,除此之外还有部分公司使用FairyGUI,DoozyUI。UGUI的运行原理UGUI是在3D网格下构建起来的UI系统,它的每一个可显示元素都是通过3D......
  • Unity___AB包
    AB包一些压缩文件,模型,预制体,图片....Resources和AB包区别Resources打包定死,只读,无法修改存储位置自定义,压缩方式,后期更新减少包体大小热更新1.资源2.脚本热更新过程获取资源地址通过资源对比文件,检测哪些需要热更新,下载AB包生成的文件AB包文件资源文件manif......
  • Unity 打包发布到 Web 相关配置
    IIS发布时,web.config配置文件如下所示:<?xmlversion="1.0"encoding="UTF-8"?><configuration><system.webServer><staticContent><mimeMapfileExtension=".data"mimeType="multipar......