简体   繁体   English

如何在 Unity 中获取分配给游戏对象的所有材质

[英]How to get all the materials assigned to a GameObject in Unity

In Unity, we can get the material that the GameObject has with the following code.在 Unity 中,我们可以通过以下代码获取 GameObject 具有的材质。

Material myMaterial = GetComponent<Renderer>().material;

But with the above code, we can only get one material per GameObject.但是使用上面的代码,我们只能为每个 GameObject 获取一个材质。

However, in actually, Unity GameObjects can have more than one material.然而,实际上,Unity GameObjects 可以有不止一种材质。

As shown in the image below如下图所示在此处输入图像描述

There can be more than one material per GameObject if it is assigned on a face-by-face basis.如果逐面分配,每个 GameObject 可以有多个材质。

I tried to get multiple materials with the code below but it didn't work.我尝试使用下面的代码获取多种材料,但没有成功。

List<Material> myMaterials = GetComponent<Renderer>().material;

Is there a way to get multiple materials assigned to a GameObject?有没有办法将多个材质分配给一个游戏对象?

You can use the Renderer.Materials : https://docs.unity3d.com/ScriptReference/Renderer-materials.html您可以使用Renderer.Materialshttps://docs.unity3d.com/ScriptReference/Renderer-materials.html

List<Material> myMaterials = GetComponent<Renderer>().materials.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM