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

时间:2016-03-26 12:25:44

标签: c# unity3d unity3d-editor

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

目前该面板的脚本如下所示,滑块可达到的最大值为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 i然后将第二个滑块的限制设置为4时,目标就是这样,因为百分比限制为10(在我的情况下代表100%)

有人知道我实现这个目标的好方法吗?

1 个答案:

答案 0 :(得分:1)

相当容易。只需计算10 - ammoxBoxesPercent并将其用作IntSlider medicKitsPercent的最大值。
你要做的一件事是将medicKitsPercent的下限设置为0,这样如果你在ammoxBoxesPercent上点击10(因为你希望总数最多为10)


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