我无法使用Unity的API修改编辑器GUI来更改引用值。
当我进入编辑器以更改GUI中的值时,它会刷新并保留我在参数中提供的标签文本/对象,为什么它不对引用进行变异并显示呢?
1。
我正在引用附加到特定游戏对象ButtonManager script = target as ButtonManager;
我想更改该类script.thisImageCaption = EditorGUILayout.TextArea("Content for the main image slide", script.thisImageCaption);
的值,但这不起作用
虽然... 很有用,但是当我选中此框时,GUI会记住我的选择并修改引用的值,那么为什么不选择其他值呢?
script.hasAudioClip = EditorGUILayout.Toggle("Voice Over?", script.hasAudioClip);
2。 我还引用了GameObjects及其单个组件
//following code is a snippet of code, the full context isnt provided, only the context related to mutating referenced values
List<Tuple<int, Text, Image>> imageCloneHolder = new List<Tuple<int, Text, Image>>();
imageCloneHolder.Add(new Tuple<int, Text, Image>(
slide.GetInstanceID(),
slide.transform.GetChild(1).GetComponent<Text>(),//get reference to text
slide.transform.GetChild(0).GetComponent<Image>()//get reference to image
));
item.Item2.text = EditorGUILayout.TextArea("Content for the image cloneslide",
item.Item2.text);//dosen't modify the referenced text
item.Item3.sprite = EditorGUILayout.ObjectField("This second Image slides image",
item.Item3.sprite,
typeof(Sprite),
false) as Sprite;//doesn't modify the referenced image
我不太清楚这是怎么回事,我想当您在Unity&C#(带有类,组件等)中获取引用时,它们将是指向实际对象的指针,而不是副本,但它似乎是Unity GUI的API让我修改副本吗?对于在类中引用的字符串,这不是真的吗?还是某些Unity组件?
屏幕转储
答案 0 :(得分:1)
我猜的主要问题是您不会将内容标记为脏。这是保存任何更改所必需的。
通常,您应该不直接在target
中对Editor
进行更改。宁可使用SerializedProperty
。他们为您处理所有标记内容 dirty (从而保存更改)和Undo / Redo功能。
如果您只添加一个元素,我也看不到为什么在这里使用List
...
我看不到您的完整代码,尤其是该类和完整的Editor会有所帮助。但是对于您提供的内容,应该类似于
public class YourEditor : Editor
{
private SerializedProperty thisImageCaption;
private SerializedProperty hasAudioClip;
// wherever you get this from
private XY item;
private void OnEnable()
{
// Link SerializedProperties to the real ones
thisImageCaption = serializedObject.FindProperty("thisImageCaption");
hasAudioClip = serializedObject.FindProperty("hasAudioClip");
}
public override void OnInpectorGUI()
{
// Load the current real values
// into the SerializedProperties
serializedObject.Update();
// Now only change the SerializedProperties
thisImageCaption.stringValue = EditorGUILayout.TextArea("Content for the main image slide", thisImageCaption.stringValue);
// Using PropertyField automatically
// uses the property drawer according
// to the property's type
EditorGUILayout.PropertyField(hasAudioClip);
// now to the tricky part
// for both slider sub-components you will need
// to create a serializedObject from the given components e.g.
var textSerializedObject = new SerializedObject(item.Item2);
var imageSerializedObject = new SerializedObject(item.Item3);
// do the update for both
textSerializedObject.Update();
imageSerializedObject.Update();
// now change what you want to change via serializedProperties e.g.
EditorGUILayout.PropertyField(textSerializedObject.FindProperty("m_text"), new GUIContent("Content for the image cloneslide"));
EditorGUILayout.PropertyField(imageSerializedObject.FindProperty("m_Sprite"), new GUIContent("This second Image slides image"));
// Write changes back to the real components
textSerializedObject.ApplyModifiedProperties();
imageSerializedObject.ApplyModifiedProperties();
serializedObject.ApplyModifiedProperties();
}
}
注意:请在智能手机上输入内容,因此没有保修,但我希望这个想法会清楚。