将 Asset Database 置于暂时禁止自动导入的状态,允许您将多个资产导入分组在一起,以形成一次大型导入。
此方法允许您暂停 Asset Database 对新的或者已修改资产的自动导入。如果您想通过脚本对资产执行多个修改操作,且不想让 Asset Database 在单独的导入过程中导入每个修改,那么此方法就很有用。
您还可以暂停导入,执行多个修改,再恢复导入,这意味着 Unity 只会为在导入暂停期间您所执行的全部修改执行一次输入流程,如下例所示。
using UnityEngine; using UnityEditor;
public class StartStopAssetEditingExample : MonoBehaviour { [MenuItem("APIExamples/StartStopAssetEditing")] static void CallAssetDatabaseAPIsBetweenStartStopAssetEditing() { try { //Place the Asset Database in a state where //importing is suspended for most APIs AssetDatabase.StartAssetEditing();
AssetDatabase.CopyAsset("Assets/CopyAsset.txt", "Assets/Text/CopyAsset.txt"); AssetDatabase.MoveAsset("Assets/MoveAsset.txt", "Assets/Text/MoveAsset.txt"); } finally { //By adding a call to StopAssetEditing inside //a "finally" block, we ensure the AssetDatabase //state will be reset when leaving this function AssetDatabase.StopAssetEditing(); } } }
注意:AssetDatabase.StartAssetEditing 将 Asset Database 置于在调用AssetDatabase.StopAssetEditing之前禁止导入的状态。这意味着如果在这两个函数调用之间出现异常,那么 AssetDatabase 将无响应。因此,您应该始终在try..catch 块
或try..finally
块(视需要而定)中放置对AssetDatabase.StartAssetEditing和AssetDatabase.StopAssetEditing的调用。
还要注意:在AssetDatabase.StartAssetEditing和AssetDatabase.StopAssetEditing之间的暂停状态下,某些 AssetDatabase API 无法按预期工作。这是因为在暂停状态下创建的资产在调用 StopAssetEditing 之前并非在资产数据库中完全创建。经验法则是,您应该将更多涉及 AssetDatabase API 的使用推迟到AssetDatabase.StopAssetEditing调用之后,并且将AssetDatabase.StartAssetEditing/AssetDatabase.StopAssetEditing范围保留用于不需要所涉及资产得到完全导入的操作。
还可以通过在 using
语句中使用 AssetDatabase.AssetEditingScope 类暂停和恢复资源数据库导入,这是一种类似的替代方式。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.