unity3d - 制作一个编辑脚本对象的工具
问题描述
我一直在制作一个自定义编辑器窗口工具,基本上可以将游戏项目列表读取为 ScriptableObjects,但是我现在被困在阅读和编写新对象上。我想创建新项目并将它们添加到所有项目的列表(存储在可编写脚本的对象中),所以剑、枪、刀、苹果等的新条目......我应该编辑工具正在创建新的物理 ScriptableObject 资产文件,还是该列表应该只是普通数据对象类?
解决方案
您正在寻找的第一部分是可编写脚本的对象不会自行序列化更改,除非我们:
A)创建脚本对象的新实例并将其存储在那里或
B)更新脚本对象中的数据,然后将其标记为“脏”并保存。
我建议在您正在编写的编辑器脚本中保持副本处于活动状态,然后定期执行保存例程,或者当用户点击更新或保存按钮时。
因此,保存可编写脚本对象的典型模式可能如下所示:
// If for some reason there is no current object you are recording changes into
// then create a new instance to save your info into
if (saveObj == null) {
saveObj = ScriptableObject.CreateInstance<YourScriptableObjClass>();
AssetDatabase.CreateAsset(saveObj, $"Assets/YourSavePath/YourObject.asset");
}
// Next Load it up with all your data, for example through some config function
saveObj.Configure(someData, otherData, moreData);
// Now flag the object as "dirty" in the editor so it will be saved
EditorUtility.SetDirty(saveObj);
// And finally, prompt the editor database to save dirty assets, committing your changes to disk.
AssetDatabase.SaveAssets();
请注意,以上只是编辑器的功能。通过这种方式,您将无法在运行时覆盖可编写脚本的对象。
编辑:对于加载,您有几个选项。您可以为可编写脚本的对象创建一个自定义编辑器,它提供某种“打开窗口”按钮,或者您可以在编辑器窗口中创建一个字段,您可以在其中拖放要使用的可编写脚本的对象。为此,您所做的很大程度上取决于您的工具以及您希望它的工作流程如何发挥作用。
对于“可编写脚本的对象按钮”,我将为可编写脚本的对象创建一个自定义检查器,该检查器提供一个按钮,该按钮使用我单击的对象触发我的窗口打开:
[CustomEditor(typeof(YourScriptableObjClass))]
public class YourScriptableObjClassEditor : Editor
{
private YourScriptableObjClass targetInfo;
public void OnEnable() {
if (targetInfo == null) {
targetInfo = target as YourScriptableObjClass;
}
}
public override void OnInspectorGUI() {
// Make a button that calls a static method to open the editor window,
// passing in the scriptable object information from which the button was pressed
if (GUILayout.Button("Open Editor Window")) {
YourEditorWindow.OpenWindow(targetInfo);
}
// Remember to display the other GUI from the object if you want to see all its normal properties
base.OnInspectorGUI();
}
}
推荐阅读
- c# - 如何通过其子属性在 Cosmos EF 中查找记录?
- react-native - 我们可以使用 ReactNative 在 jitsi meet 中实现“屏幕截图”功能吗
- node.js - 在瀑布对话框中移动数据。机器人框架 SDK
- vue.js - Vue安装错误和安装包时
- java - android.graphics.drawable.LayerDrawable 不能转换为 android.graphics.drawable.ColorDrawable
- c - 如何在 C 中编写 int 值二进制和 txt 文件?
- r - 如何在 R 中更改“toString”的分隔符?
- c - 在 C 语言中,如果我将“连续 wchar_t 字符”放入 wchar_t 变量会发生什么?
- javascript - React - 更新组件中的自定义钩子引用
- python - python 3.1中的Else函数在mac os中的空白行不能正常工作