首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误CS0103请帮助它说名称'currentResolutionIndex‘在当前的上下文统一中不存在

错误CS0103请帮助它说名称'currentResolutionIndex‘在当前的上下文统一中不存在
EN

Stack Overflow用户
提问于 2021-12-21 00:43:50
回答 1查看 28关注 0票数 0
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class SettingsMenu : MonoBehaviour
{

    public AudioMixer audioMixer;

    public Dropdown resolutionDropdown;

    Resolution[] resolutions;

    void Start()
    {
        resolutions = Screen.resolutions;

        resolutionDropdown.ClearOptions();

        List<string> options = new List<string>();

        int currentResolution;
        
        for (int i = 0; i < resolutions.Length; i++)
        {
            string option = resolutions [i].width + " x " + resolutions[i].height;
            options.Add(option);

            if (resolutions[i].width == Screen.currentResolution.width &&
                resolutions[i].height == Screen.currentResolution.height)
            {
                currentResolutionIndex = i;
            }
        }
        resolutionDropdown.AddOptions(options);
        resolutionDropdown.value = currentResolutionIndex;
        resolutionDropdown.RefreshShownValue();
    }

    public void SetResolution (int resolutionIndex)
    {
        Resolution resolution = resolutions[resolutionIndex];
        Screen.SetResolution(resolution. width, resolution.height, Screen.fullScreen);
    }

    public void SetVolume (float volume)
    {
        audioMixer.SetFloat("Volume", volume);
    }

    public void SetQuality (int qualityIndex)
    {
        QualitySettings.SetQualityLevel(qualityIndex);
    }

    public void SetFullScreen (bool isFullScreen)
    {
        Screen.fullScreen = isFullScreen;
    }
}

请帮助,因为团结引擎的错误,我不明白,也许这是从2017年的教程,我不知道,但如果你能帮助我将是很好的。如果您需要更多的上下文,请写信给我,这与当前的分辨率索引有关

EN

回答 1

Stack Overflow用户

发布于 2021-12-21 00:51:15

编译器错误CS0103是由使用未声明的变量或方法引起的。如果currentResolutionIndex变量从未在文件中定义,则会发生此问题。

代码语言:javascript
复制
void Start()
{
    /* Added the following line of code. To manage this condition when the condition is not always true, the variable must be initialized. */
    int currentResolutionIndex = -1;
    
    for (int i = 0; i < resolutions.Length; i++)
    {
        /* This condition may not always be true. */
        if (resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
        {
            currentResolutionIndex = i;
        }
    }
   
    /* If the condition never works correctly, you should control and manage this condition. */
    if(currentResolutionIndex != -1)
    {
        resolutionDropdown.value = currentResolutionIndex;
    }
    else
    {
        /* Exception Handling */
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70429519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档