c# - 如何使用一个 Input.GetKeyDown(KeyCode.S) 在多种材质之间切换?
问题描述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkyBox : MonoBehaviour
{
public Material[] skyboxes;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.S))
{
for (int i = 0; i < skyboxes.Length; i++)
{
RenderSettings.skybox = !skyboxes[i];
}
}
}
}
首先不确定使用循环是否是个好主意。
第二个在右侧出现错误:
!skyboxes[i]
无法将类型“bool”隐式转换为“UnityEngine.Material”
解决方案
这是一个有效的解决方案:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkyBox : MonoBehaviour
{
public Material[] skyboxes;
private int index = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
index++;
if (index == skyboxes.Length)
index = 0;
RenderSettings.skybox = skyboxes[index];
}
}
}
推荐阅读
- python - 如何从列表中检测句子中的单词?
- opencv - 我在 ubuntu 18.04 中使用 openCV 时遇到问题
- excel - 比较 2 个工作簿中的行并复制新工作簿中的差异
- django - 在我的 Django 和 DRF 项目中,我的正确工作链接出现服务器错误,添加 404 页面后找不到 404 页面丢失 url
- c - 如何正确设置 xwindows 属性
- sql - 如何在 SQL 中为每 3 位添加逗号?
- math - 根据给定的过去发生频率查找每日发生次数
- python - 调试屏幕返回 HTML 屏幕而不是我的文件
- python - python在csv的列和行中组合2个re.findall字符串
- spring-boot - 如何在spring boot中的多对一映射中获取带有内部post对象的用户对象