程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用生成的下拉选项列表从 Unity 的下拉 UI 元素中获取选定的分辨率大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用生成的下拉选项列表从 Unity 的下拉 UI 元素中获取选定的分辨率?

开发过程中遇到如何使用生成的下拉选项列表从 Unity 的下拉 UI 元素中获取选定的分辨率的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用生成的下拉选项列表从 Unity 的下拉 UI 元素中获取选定的分辨率的解决方法建议,希望对你解决如何使用生成的下拉选项列表从 Unity 的下拉 UI 元素中获取选定的分辨率有所启发或帮助;

我正在为 Unity 的 TextMeshPro 屏幕分辨率下拉菜单编写功能,该菜单使用 for 处的 voID Start() 循环来获取 Unity 的可用分辨率并使用分辨率选项填充自身。然我可以填充下拉列表,但每个菜单选项都是不可选择的,并且会假设我只是选择了已启用的默认选项(例如,在我的情况下是 1920x1080,这是大多数用户的默认设置。)

我想弄清楚如何使生成的项目可选,尤其是如果这不是正确的方法。然生成的项目可以在 Unity 的编辑器中选择,但它们在构建时并不对齐。 (例如,选择 1920x1080 将在实际构建中返回 800x600。)

    using System.Collections;
    using System.Collections.Generic;
    using System.linq;
    using UnityENGIne;
    using TMPro;
    
    public class windowsetTings : MonoBehavIoUr

{
    #region Attributes

    #endregion

    #region Player Pref Key Constants

    private const String RESolUTION_PREF_KEY = "resolution";

    #endregion

    #region Resolution

    [@R_450_9464@lizefIEld]
    private TextMeshProUGUI resolutiontext;

    private Resolution[] resolutions;

    privatE int currentResolutionIndex = 0;

    public TMP_Dropdown resolutionDropdown;

    private bool clickable = true;

    #endregion

    private voID Start()
    {
        clickable = true;

        resolutions = Screen.resolutions;
        resolutionDropdown.ClearOptions();

        int endResolutionIndex = resolutions.Length - 1;

        //Makes a List of resolution options.
        List<String> options = new List<String>();
        
        for (int i = 0; i < resolutions.Length; i++)
        {
            String option = resolutions[i].wIDth + " x " + resolutions[i].height;

            options.Add(option);
            DeBUG.Log(option + "added.");
        }

        //Ensures those resolutions options are not made with duplicates.
        IEnumerable<String> disTinctoptions = options.disTinct();

        //Adds options to the List.
        resolutionDropdown.Addoptions(disTinctoptions.ToList());

        currentResolutionIndex = endResolutionIndex;

        resolutionDropdown.value = currentResolutionIndex;
        resolutionDropdown.refreshShownValue();

        currentResolutionIndex = PlayerPrefs.GeTint(RESolUTION_PREF_KEY,0);

        SetResolutiontext(resolutions[currentResolutionIndex]);
    }

    #region Apply Resolution

    private voID SetAndApplyResolution(int newResolutionIndeX)
    {
        currentResolutionIndex = newResolutionIndex;
        ApplyCurrentResolution();
        StartCoroutIne(WaittoClick(1));
    }
    //To prevent click spAMMing.
    IEnumerator WaittoClick(int seconds)
    {
        yIEld return new WaitForSeconds(seconds);
        clickable = true;
    }

    private voID ApplyCurrentResolution()
    {
        ApplyResolution(resolutions[currentResolutionIndex]);
        DeBUG.Log("Applying " + currentResolutionIndeX);
    }

    private voID ApplyResolution(Resolution resolution)
    {
        SetResolutiontext(resolution);
        Screen.SetResolution(resolution.wIDth,resolution.height,Screen.fullScreen);
        PlayerPrefs.SeTint(RESolUTION_PREF_KEY,currentResolutionIndeX);
    }

    #endregion

    #region Resolution Cycling

    private voID SetResolutiontext(Resolution resolution) => resolutiontext.text = resolution.wIDth + " x " + resolution.height;

    #endregion
    //This needs to be able to find what part of the List is SELEcted.
    public voID ApplyChanges(int X)
    {
        //ExperimenTing with bools to prevent click spAMMing.
        if (clickablE)
        {
            clickable = false;
            resolutionDropdown.value = x;
            resolutionDropdown.refreshShownValue();
            SetAndApplyResolution(currentResolutionIndeX);
        }
    }

    public voID ToggleFullScreen(bool option)
    {
        Screen.fullScreen = option;
        DeBUG.Log("FullScreen is " + option);
    }
}

ApplyChanges() 是我目前使用的公共函数,用作带有动态 int 的 DropDown 功能集的 OnValueChanged(Int32)。这种方法适用于我正在使用的另一个处理语言选项的下拉菜单,但不适用于此菜单。我认为这与生成的列表有关,因为语言菜单已经为不需要生成的语言提供了预设选项。

解决方法

您确定此代码在其他地方有效吗?您的问题是您没有在函数调用 currentResolutionIndex 之前设置变量 SetAndApplyResolution,或者您传入了错误的变量。

public void ApplyChanges(int X)
{
    //ExperimenTing with bools to prevent click spAMMing.
    if (clickablE)
    {
        clickable = false;
        resolutionDropdown.value = x;
        resolutionDropdown.refreshShownValue();
        
        // this line is your issue so either...
        // SetAndApplyResolution(currentResolutionIndeX);
        
        // solution 1 to fix above line
        currentResolutionIndex = x;
        SetAndApplyResolution(currentResolutionIndeX);
        
        // OR
        
        // solution 2 to fix above line (what I believe you intended)
        SetAndApplyResolution(X);
    }
}

除了上述修复之外,从 onValueChange 的侦听器设置此值时可能存在另一个问题。确保从以ApplyChanges 为首的列表中的Dynamic int 列表中选择回调函数,而不是Static Parameters

,

事实证明,我没有意识到 Unity 还列出了上述分辨率选项的刷新率,最初似乎显示的是重复项。

为了解决这个问题,我添加了显示的刷新率作为生成选项的一部分,这样当 for 中的 void Start() 循环生成所述选项时,它对最终用户更有意义,以及删除了 LINQ .DisTinct() 方法,因为它不再需要。

for (int i = 0; i < resolutions.Length; i++)
    {
        String option = resolutions[i].width + " x " + resolutions[i].height + " " + resolutions[i].refreshRate + "hz";

        options.Add(option);
    }

大佬总结

以上是大佬教程为你收集整理的如何使用生成的下拉选项列表从 Unity 的下拉 UI 元素中获取选定的分辨率全部内容,希望文章能够帮你解决如何使用生成的下拉选项列表从 Unity 的下拉 UI 元素中获取选定的分辨率所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。