英文:
UI Toolkit Custom Inspector Disappeared in Playmode Unity
问题
我已创建了一个使用UI Toolkit的自定义检查器。初始化UI文档的方式如下:
public VisualTreeAsset uXML;
这个uXML
是通过检查器分配的。然后在CreateInspectorGUI
中:
root = new VisualElement();
if (uXML == null)
{
Debug.Log("UI文档为null: " + documentName);
return root;
}
uXML.CloneTree(root);
root
是UI文档的主根(我正在使用UI Toolkit)。
问题是,当我进入播放模式时,检查器会消失,如图所示:
英文:
I have created a custom inspector using the UI Toolkit. The way I initialize the UI Document is as follows:
public VisualTreeAsset uXML;
This uXML
is assigned through the inspector. Then in CreateInspectorGUI
:
root = new VisualElement();
if (uXML == null)
{
Debug.Log("UI Document is " + documentName + " null");
return root;
}
uXML.CloneTree(root);
The root
is the main root of the UI document (I am using UI Toolkit).
The problem is that when I enter play mode, the Inspectors disappears as shown:
答案1
得分: 1
我假设你在编辑器脚本中使用了“默认引用”。不幸的是,只有在“编辑”模式下,才会对默认引用进行反序列化。一旦你切换到“播放”模式,该引用将变为null。
解决这个问题的一般方法是在创建检视器窗口时搜索你的资产:
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/PathToYourFiles/YourFile.uxml")
不幸的是,如果你将文件移动到其他位置,你需要更新字符串文件位置。因为它是一个字符串路径,所以在移动或重命名文件时没有引用可供跟踪。这不是最理想的方法,但很简单,并且在播放模式下能够提供检视器。
我对这个问题进行了快速搜索。根据这个文档,Unity还建议使用Addressables或Resources文件夹。
因为你不希望这些资产在最终构建中跟随,我更喜欢将它们放在“Editor”文件夹中,并确保在移动或重命名文件时要小心。在一个项目中,我创建了一个自动化流程来为我“找到”文件。这涉及使用ScriptableObject和程序集重新加载,但我仍在考虑是否值得这个努力。
英文:
I’m going to assume that you used a “Default Reference” for your editor script. Unfortunately, the default reference is only deserialised when you're in "Edit" mode. As soon as you go to "Play" mode, the reference is null.
The general way to get around the issue is to search for your asset when you create the inspector window:<br>AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/PathToYourFiles/YourFile.uxml")
Unfortunately, if you move your files somewhere else, you need to go in an update the string file location. Because it's a string path, there's no reference to follow the file if you move or rename the file. It's not clean, but it's easy, and gives you your inspector when in Play mode.
I did a quick search on the issue. Looking at this document, it seems Unity also suggest using Addressables or the Resources folder.
Because you don't really want the assets to follow you into a final build, I prefer placing them into an Editor
folder, and just making sure I'm careful when moving or renaming the files. In one project, I made an automated process that "found" the file for me. It involved using a ScriptableObject and Assembly Reloads but I'm still in two minds whether the effort was worth it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论