Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义GUI文本字段';s和ObjectField';s don';不赋值_C#_Unity3d_Unity Editor - Fatal编程技术网

C# 自定义GUI文本字段';s和ObjectField';s don';不赋值

C# 自定义GUI文本字段';s和ObjectField';s don';不赋值,c#,unity3d,unity-editor,C#,Unity3d,Unity Editor,通过使用Unity的API修改编辑器GUI,我在修改引用值时遇到问题 当我进入编辑器更改GUI中的值时,它只是刷新并保留我在参数中提供的标签文本/对象,为什么它不改变引用并显示它呢 一,。 我正在引用一个类,该类作为ButtonManager附加到一个特定的游戏对象ButtonManager script=target 我想更改该类的值script.thisImageCaption=EditorGUILayout.TextArea(“主图像幻灯片的内容”,script.thisImageCapt

通过使用Unity的API修改编辑器GUI,我在修改引用值时遇到问题

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

一,。 我正在引用一个类,该类作为ButtonManager附加到一个特定的游戏对象
ButtonManager script=target

我想更改该类的值
script.thisImageCaption=EditorGUILayout.TextArea(“主图像幻灯片的内容”,script.thisImageCaption)但这不起作用

虽然如此bools工作,当我选中该框时,GUI会记住我的选择并修改引用值,那么为什么其他人不这样做呢?
script.hasAudioClip=EditorGUILayout.Toggle(“画外音”,script.hasAudioClip)

二,。 我还引用了游戏对象及其各个组件

//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

//下面的代码是一段代码,没有提供完整的上下文,只有与引用值的变化相关的上下文
List imageCloneHolder=新列表();
添加(新元组)(
slide.GetInstanceID(),
slide.transform.GetChild(1.GetComponent(),//获取对文本的引用
slide.transform.GetChild(0.GetComponent()//获取对图像的引用
));
item.Item2.text=EditorGUILayout.TextArea(“图像克隆滑动的内容”,
项目2.文本)//请勿修改引用的文本
item.Item3.sprite=EditorGUILayout.ObjectField(“第二个图像幻灯片图像”,
item.Item3.sprite,
类型(精灵),
假)作为精灵//不修改引用的图像
我不太明白出了什么问题,我想当你在Unity&C#(使用类、组件等)中获取引用时,它们将是指向真实对象的指针,而不是副本,但Unity的GUI API似乎让我修改副本?对于类中引用的字符串,这不是真的吗?或者对于某些Unity组件

屏幕转储

代码:


编辑:

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

通常,您不应该直接在
编辑器中更改
目标
。而是使用。它们可以为您处理所有标记内容(从而保存更改)和撤消/重做功能

我也不明白,如果你只添加了一个元素,为什么要在那里使用
列表

我看不到您的完整代码,尤其是类和完整编辑器会有所帮助。但是你提供的应该是

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


注意:在智能手机上输入,因此没有保修,但我希望想法变得清晰。

您不应该直接修改
目标的值,而是应该执行此操作,看起来有点复杂,但您的主要问题是,当前您没有将内容标记为脏,因此更改永远不会保存。使用
SerializedProperty
Unity自动处理所有标记脏和重做/撤消。我需要更多的代码,尤其是完整的编辑器,至少是包含所有字段的类来帮助您实现它。@derHugo我在主要帖子中共享了一个屏幕转储Unity如何知道我们在哪个serializedObject上查找属性?这就是我真正困惑的地方。当多个对象上有健全的属性名称时会发生什么?Unity如何能够识别任何差异?是否从[CustomEditor(typeof(ButtonManager))]属性知道哪个序列化对象?每次你关注,例如,一个新的游戏对象Unity都会为它的每个组件创建编辑器实例。这些编辑器实例将自动链接到它们作为检查器的组件的相应序列化对象。例如,您的所有游戏对象都具有属性相同但值不同的
Transform
组件。但检查器始终链接到相应的实例。特别是当选择多个对象时,它会变得有趣;)[CustomEditor]属性或多或少只是相关的,因此Unity知道对于类型
ButtonManager
存在一个自定义编辑器,它不应该使用默认编辑器。此外,还需要一些参数来进一步调整某些编辑器行为。