C# 如何在自定义编辑器中定义变量,需要定义数组吗?
我想得到一些帮助,因为我不了解序列化属性路径之类的东西。我已经写了两个脚本一个自定义编辑器和一个常规脚本。如果有人知道我如何从编辑器脚本中引用第一个脚本中的slotContents变量,同时保持它的可折叠性。谢谢:DC# 如何在自定义编辑器中定义变量,需要定义数组吗?,c#,arrays,unity3d,unity3d-editor,C#,Arrays,Unity3d,Unity3d Editor,我想得到一些帮助,因为我不了解序列化属性路径之类的东西。我已经写了两个脚本一个自定义编辑器和一个常规脚本。如果有人知道我如何从编辑器脚本中引用第一个脚本中的slotContents变量,同时保持它的可折叠性。谢谢:D using UnityEngine; using System.Collections; public class Inventory : MonoBehaviour { public GUISkin mainDesignSkin; public GUIS
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
public GUISkin mainDesignSkin;
public GUISkin craftDesignSkin;
public bool enableCraftingMenu = true;
public int craftSlot1;
public int craftSlot2;
public int craftResult; //Make this private users do not need to see this it will clutter editor.
public int [] slotContents;
}
还有第二个剧本
using UnityEngine;
using System.Collections;
using UnityEditor;
//Decalres type for editor.
[CustomEditor(typeof(Inventory))]
public class InventoryUiCustomEditor : Editor {
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
Inventory ItemReference = (Inventory)target;
EditorGUILayout.LabelField("First we need to set our skin look in the skins folder or assign your own designs.");
EditorGUILayout.LabelField("Please assign even if crafting is disabled to avoid accidental null reference errors.");
EditorGUILayout.Separator();
ItemReference.mainDesignSkin = (GUISkin) EditorGUILayout.ObjectField("Apply the skin entitled 'Main' here.", ItemReference.mainDesignSkin, typeof (GUISkin), false);
ItemReference.craftDesignSkin = (GUISkin) EditorGUILayout.ObjectField("Apply the skin entitled 'Craft' here.", ItemReference.craftDesignSkin, typeof (GUISkin), false);
EditorGUILayout.Separator();
EditorGUILayout.LabelField("Should the crafting menu be enabled in this Scene?");
EditorGUILayout.Separator();
ItemReference.enableCraftingMenu = EditorGUILayout.Toggle("Enable Crafting", ItemReference.enableCraftingMenu);
if (ItemReference.enableCraftingMenu == true){
EditorGUILayout.Separator();
EditorGUILayout.LabelField("The below fields are for your testing only they will be automatically overridden.");
EditorGUILayout.LabelField("The following fields can be used as a tester however will only work on valid recipes check the");
EditorGUILayout.LabelField("documentation video for a tutorial on this.");
EditorGUILayout.Separator();
ItemReference.craftSlot1 = EditorGUILayout.IntField("Item 1", ItemReference.craftSlot1);
ItemReference.craftSlot2 = EditorGUILayout.IntField("Item 2", ItemReference.craftSlot2);
ItemReference.craftResult = EditorGUILayout.IntField("Result of mixing above two items.", ItemReference.craftResult);
}
}
}
检查此链接:
这是您应该阅读的链接,因为它包含您答案的详细信息:
并搜索Serialize.Field
如果不在现场,用户将其关闭:
下面是您的代码的外观:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Inventory : MonoBehaviour
{
public GUISkin mainDesignSkin;
public GUISkin craftDesignSkin;
public bool enableCraftingMenu = true;
public int craftSlot1;
public int craftSlot2;
public int craftResult; //Make this private users do not need to see this it will clutter editor.
[SerializeField]
public int [] slotContents;
}
您在
slotContents
数组之前是否尝试过使用[Serializable]
?感谢您分享这段伟大的代码。我正在寻找一种通过选择枚举值来定制inspector视图的方法,您的代码为我指明了正确的方向。凉的但是,我如何在编辑器中引用它所说的一切都是你告诉我的,这只是主要脚本,而不是编辑器方面的东西??