Unity - 从数组中获取随机的天空盒

3
为了完成学校任务,我需要制作一个基本的视频游戏。我正在尝试制作一个太空游戏的基本版本。
我想要一个包含多个天空盒子的数组,并且在启动游戏时,我希望游戏从该数组中随机选择一个天空盒子。这样你每次都会有一种在不同出生点的感觉。
有人可以帮助我吗?我在谷歌上找了很久,但没有找到任何有用的信息。
这些天空盒子位于名为“Assets/SkyBox Volume 2/DeepSpaceBlue”的文件夹中,文件名为 DSB。还有一个DeepSpaceGreen,文件名为DSG,以此类推。
这是我的当前代码,其中包含错误。
编辑:
using UnityEngine;
using System.Collections;
public class RandomSkybox : MonoBehaviour
{
    public Material[] materials;

    // Use this for initialization
    void Start()
    {
        skyBoxMaterial = materials[Random.Range(0, materials.length)];
        RenderSettings.skybox = skyBoxMaterial;
    }

    // Update is called once per frame
    void Update()
    {

    }
}

错误:
Severity    Code    Description Project File    Line    Suppression State
Error   CS0103  The name 'skyBoxMaterial' does not exist in the current context SpaceRaiders.CSharp D:\Documenten\Unity\SpaceRaiders\Assets\Scripts\Space\RandomSkybox.cs   10  Active
Error   CS1061  'Material[]' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'Material[]' could be found (are you missing a using directive or an assembly reference?)   SpaceRaiders.CSharp D:\Documenten\Unity\SpaceRaiders\Assets\Scripts\Space\RandomSkybox.cs   10  Active
Error   CS0103  The name 'skyBoxMaterial' does not exist in the current context SpaceRaiders.CSharp D:\Documenten\Unity\SpaceRaiders\Assets\Scripts\Space\RandomSkybox.cs   11  Active

请查看此链接:http://answers.unity3d.com/questions/541258/load-material-texture-from-file.html - Fᴀʀʜᴀɴ Aɴᴀᴍ
@FᴀʀʜᴀɴAɴᴀᴍ .. OP的问题只是他忘记拖入了。 - Fattie
@JoeBlow 是的..如果他通过代码来填充它,那么这个链接会很有帮助.. - Fᴀʀʜᴀɴ Aɴᴀᴍ
1个回答

4
public Skybox[] skyBoxes;

您需要将项目拖到数组中。

简单易懂的操作

很简单,在检查器中,将长度设置为“5”,然后将五个项目拖入其中即可。没有更多的操作。


此外,在您发布的最新版本中,

skyBoxMaterial = materials[Random.Range(0, materials.length)];

你忘记声明变量了,就是这么简单。

Material skyBoxMaterial;
skyBoxMaterial = materials[Random.Range(0, materials.length)];

对于“length”,显然是“长度(Length)”。不要忘记对于List<>来说,它是.Count而不是.Length

它不起作用是因为代码有误,我正在编辑我的帖子。 - Meindert Stijfhals
我现在仍然遇到这个错误,感谢您的帮助!严重性代码描述项目文件行抑制状态 错误CS1061:'Material []'不包含对'length'的定义,并且没有接受类型为'Material []'的第一个参数的扩展方法'length'可以找到(是否缺少使用指令或程序集引用?)SpaceRaiders.CSharp D:\ Documenten \ Unity \ SpaceRaiders \ Assets \ Scripts \ Space \ RandomSkybox.cs 11 Active - Meindert Stijfhals

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接