Unity 在自定义编辑器面板中动态限制滑块

Unity Dynamically Limiting a Slider in a Custom Editor Panel

我已经在我的面板中尝试过计算总百分比的剩余量并将其设置为滑块的上限,但这似乎不准确,因为我的逻辑是错误的或者它破坏了面板。

目前,面板的脚本如下所示,滑块可以达到的最大值为 10,标签显示总数,所以我不会超过,这是非常不切实际的。

public static int ammoxBoxesPercent = EditorPrefs.GetInt("Ammo");
    public static int medicKitsPercent = EditorPrefs.GetInt("MedicKits");
    public static int amountOfItemsPerZone =  EditorPrefs.GetInt("Amount");


    public static int totalPercentageVal;
    public static int maxTotalValue = 10;

    [MenuItem("Enviroment Controls/ Object Spawners Control Panel")]
    private static void showEditor()
    {
        EditorWindow.GetWindow<ObjectSpawnersControlPanel>(false, "OBJ Spawners CP");
    }


    void OnGUI()
    {


            ammoxBoxesPercent = EditorGUILayout.IntSlider("Ammox Box Percent", ammoxBoxesPercent, 1, 10);
            EditorPrefs.SetInt("Ammo", ammoxBoxesPercent);

            medicKitsPercent = EditorGUILayout.IntSlider("Medic Kit Percent", medicKitsPercent, 1, 10);
            EditorPrefs.SetInt("MedicKits", medicKitsPercent);


            amountOfItemsPerZone = EditorGUILayout.IntSlider("Amount of Items Spawned at Each Zone", amountOfItemsPerZone, 1, 5);
            EditorPrefs.SetInt("Amount", amountOfItemsPerZone);

            totalPercentageVal = medicKitsPercent + ammoxBoxesPercent;
            EditorGUILayout.LabelField ("Total Percentage so far : " + totalPercentageVal.ToString() + "0");
            EditorGUILayout.LabelField ("Total must not go above 100");

    }

我的目标是让第一个滑块设置为 6,然后将第二个滑块的限制设置为 4,因为百分比限制为 10(在我的例子中代表 100%)

有人知道实现此目标的好方法吗?

相当容易。只需计算 10 - ammoxBoxesPercent 并将其用作 IntSlidermedicKitsPercent 的最大值。
您要做的一件事是将 medicKitsPercent 的下限设置为 0,以防您在 ammoxBoxesPercent 上达到 10(因为您希望总数最大为 10)


medicKitsPercent = EditorGUILayout.IntSlider("Medic Kit Percent", medicKitsPercent, 0, maxTotalValue - ammoxBoxesPercent);