0

我在使用 Unity 的 API 修改编辑器 GUI 来改变引用值时遇到问题。

当我进入编辑器更改 GUI 中的值时,它只是刷新并保留我在参数中提供的标签文本/对象,为什么它不改变引用并显示它呢?

1. 我正在引用一个附加到特定游戏对象的类ButtonManager script = target as ButtonManager;

我想更改该类的值,script.thisImageCaption = EditorGUILayout.TextArea("Content for the main image slide", script.thisImageCaption);但这不起作用

奇怪的是... bools工作,当我选中该框时,GUI 会记住我的选择并修改引用的值,那么为什么其他人不呢? script.hasAudioClip = EditorGUILayout.Toggle("Voice Over?", script.hasAudioClip);

2. 我还引用了游戏对象及其各个组件

//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 组件?

屏幕转储

代码:https ://imgur.com/a/5fRR56c

编辑:https ://imgur.com/a/jeXMGSN

4

1 回答 1

1

我猜主要问题是你没有将东西标记为。这是保存任何更改所必需的。

通常,您不应直接targetEditor. 而是使用SerializedProperty. 他们为您处理所有标记内容从而保存更改)和撤消/重做功能。

List另外,如果您只添加一个元素,我也不明白您为什么要在那里使用...

我看不到您的完整代码,尤其是课程和完整的编辑器会有所帮助。但是对于您提供的内容,它应该类似于

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();
    }
}

注意:在智能手机上打字,所以没有保修,但我希望这个想法很清楚。

于 2019-07-17T07:34:47.590 回答