以编程方式更改天空盒材质(C#)

3

我想更改默认的天空盒材质,所以编写了以下代码... 然后我只看到了蓝色... 我错在哪里了?

顺便说一下,这种材质位于Assets文件夹中。

Material levelMat = Resources.Load(Application.dataPath + "/testeVR.mat", typeof(Material)) as Material;
RenderSettings.skybox = levelMat;

它有镜面/纹理吗? - Jeroen van Langen
2个回答

2

这个素材在 Assets 文件夹里

如果您是使用 Resources.Load 加载素材,就不能将素材放置在 Asset 文件夹中。

之后我只看到了蓝色

levelMat 变量是 null。当应用于 Skybox 的材质为 null 时,您会看到蓝色。可以通过在其后添加 Debug.Log(levelMat); 来证明。

Material levelMat = Resources.Load(Application.dataPath + "/testeVR.mat", typeof(Material)) as Material;

这也行不通。应该不要在路径参数中使用 Application.dataPath 函数,因为 Resources.Load 函数不支持这种方式。

从您的代码中需要理解以下几点:

1.Resources.Load 需要一个特殊的文件夹名叫做 Resources。请在 Assets 目录下创建一个名为 Resources 的文件夹,并将 testeVR.mat 放入其中。

2.在路径参数中不需要包含文件扩展名。所以,testeVR.mat 应该是没有 ".mat" 的 testeVR

只需在 Assets 文件夹中创建一个名为 Resources 的文件夹,然后将 testeVR 材质放入其中。路径应该如下所示:Assets/Resources/testeVR.mat。下面的代码可以用来加载它:

Material levelMat = Resources.Load("testeVR", typeof(Material)) as Material;

现在,假设你有另一个名为“Mats”的文件夹,它位于Resources文件夹中。你可以使用以下类似的语句:

Material levelMat = Resources.Load("Mats/testeVR", typeof(Material)) as Material;

1

只需创建材质的公共变量并放置您希望在运行时加载的材质,或者如果您想通过某些函数加载它,请使用此方法。

RenderSetting.sky = skyboxMat; 

skyboxMat存储了在Unity中拖动的材质,然后它就成为了你的天空盒的一部分。
public Material skyboxMat;
voidstart(){ RenderSetting.skybox = skyboxMat;}

这在编辑器和iOS上可以工作。但在Android上,它失败了?在Android中仍然是蓝色的。 - Rowan Gontier

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