UI Toolkit自定义检视器在Playmode中消失Unity

huangapple go评论59阅读模式
英文:

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)。

问题是,当我进入播放模式时,检查器会消失,如图所示:

UI Toolkit自定义检视器在Playmode中消失Unity

英文:

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:

UI Toolkit自定义检视器在Playmode中消失Unity

答案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&lt;VisualTreeAsset&gt;(&quot;Assets/PathToYourFiles/YourFile.uxml&quot;)

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.

huangapple
  • 本文由 发表于 2023年2月18日 01:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定