我该如何"ping"自定义Unity3D检查器中的文本文件?

问题描述:

我试图在Unity3D的自定义编辑器中"ping"文本文件,例如使用 EditorGUIUtility.PingObject (在Hierachy中显示文件,并闪烁黄色选择字段).
文件位于Assets/StreamingAssets/Example.csv

I'm trying to "ping" a Textfile in a custom editor in Unity3D like e.g. using EditorGUIUtility.PingObject (Shows the file in the Hierachy and flashes a yellow selection field over it).
The file is under Assets/StreamingAssets/Example.csv

最简单的解决方案(我认为)是简单地在ObjectField中显示它->如果单击该字段,资产也会被"ping". 所以我正在尝试:

The simplest solution (I thought) would be to simply show it in an ObjectField -> if clicking on the field the asset also gets "pinged". so I'm trying:

// For debug, later the filename will be dynamic
var path = "Assets/StreamingAssets/" + "Example" + ".csv";
TextAsset file = (TextAsset)AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset));
EditorGUILayout.PrefixLabel("CSV File", EditorStyles.boldLabel);
EditorGUILayout.ObjectField(file, typeof(TextAsset), false);

但是尽管文件存在并且path正确,但file仍然是null

But though the file is there and the path correct, file is allways null

我的错误是对TextAsset的类型转换,并使用

My mistake was the typecasting to TextAsset and using LoadAssetAtPath which requires a Type parameter.

将其取消广播"为object(资产),并使用 LoadMainAssetAtPath 而不需要Type参数现在可以正常工作:

Leaving it "uncasted" as object (asset) and using LoadMainAssetAtPath instead which doesn't require the Type parameter works now:

// For debug, later the filename will be dynamic
var path = "Assets/StreamingAssets/" + "Example" + ".csv";
var file = AssetDatabase.LoadMainAssetAtPath(path);
EditorGUILayout.PrefixLabel("CSV File", EditorStyles.boldLabel);
EditorGUILayout.ObjectField(file, typeof(object), false);