简体   繁体   English

Unity 检查器无法编辑公共数组的值

[英]Unity inspector can't edit public array's values

I want a script that has a public array so that I can modify the values in the inspector.我想要一个具有公共数组的脚本,以便我可以修改检查器中的值。 Here's an example script:这是一个示例脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Foo : MonoBehaviour
{
    public int[] bars;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

When I add the script to a game object, I can see the empty list in the inspector (as expected).当我将脚本添加到游戏 object 时,我可以在检查器中看到空列表(如预期的那样)。

在此处输入图像描述

But when I add elements to the array, the array area is just blank so I can't set the value of each element (or see what the element values are).但是当我向数组添加元素时,数组区域只是空白,所以我无法设置每个元素的值(或查看元素值是什么)。

在此处输入图像描述

Is this just a bug, or am I doing something wrong?这只是一个错误,还是我做错了什么?

I'm running Unity 2021.2.5f1 on macOS Monterey 12.0.1.我在 macOS Monterey 12.0.1 上运行 Unity 2021.2.5f1。

I had this same problem after upgrading to a new version of Unity, and I discovered that it works if I move the editor window over to a different screen.升级到新版本的 Unity 后,我遇到了同样的问题,我发现如果我将编辑器 window 移到另一个屏幕,它就可以工作。 Maybe because my main screen is widescreen?也许是因为我的主屏幕是宽屏? I dunno, I can't explain it.我不知道,我无法解释。 If I move the editor back to my main screen, they disappear again.如果我将编辑器移回主屏幕,它们会再次消失。 So for now, I just adjust the values on my smaller side-screen, then move back when I'm done.所以现在,我只是调整较小的侧屏幕上的值,然后在完成后返回。 If you don't have more than one screen, perhaps try to change the resolution or something.如果您没有一个以上的屏幕,也许可以尝试更改分辨率或其他内容。

May be not the best solution but, definin your own serializable int struct may works.可能不是最好的解决方案,但定义您自己的可序列化 int 结构可能有效。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public struct MyInt
{
    public int Int;
}

public class Foo : MonoBehaviour
{
     public MyInt[] bars;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

I'm having the same issue with 2021.2.7f1 and 2021.2.6f for Unity Apple Silicon. Unity Apple Silicon 的 2021.2.7f1 和 2021.2.6f 也有同样的问题。 For example if I create an array for say animation sprites and want to drag the sprites into the inspector.例如,如果我为 animation 精灵创建一个数组,并希望将精灵拖到检查器中。 They don't display in the dropdown.它们不会显示在下拉列表中。 It's just blank like the above screenshot.就像上面的屏幕截图一样,它只是空白。

Oddly enough I opened another older project that I converted to the newer unity version that was a duplicate of the same project I was rebuilding for practice and the arrays were displaying as they should.奇怪的是,我打开了另一个旧项目,将其转换为更新的统一版本,该版本与我为练习而重建的同一项目重复,并且 arrays 正在按应有的方式显示。

Edit: Wow, so the list displays on my M1 Macbook but not on my external display.编辑:哇,所以列表显示在我的 M1 Macbook 上,但不在我的外部显示器上。

I'll let you know if I find a solution.如果我找到解决方案,我会告诉你的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM