在我的 Unity 项目中,我尝试使用一些自制材料突出显示某个对象。不幸的是,我找到的代码不起作用。
以下脚本被添加到三个不同的游戏对象中:
Renderer[] rend_ThisObject;
Material white, red;
void Start()
{
// I tried the following two code parts:
white = Resources.Load<Material>("White");
white = Resources.Load("Material/White.mat", typeof(Material)) as Material;
red = Resources.Load<Material>("Red");
red = Resources.Load("Material/Red.mat", typeof(Material)) as Material;
}
void Update()
{
foreach (var child in rend_ThisObject)
{
child.material = blinkObject ? white : red;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行该项目,带有脚本的游戏对象会显示为粉红色并且不会闪烁。
如果我更改Material white, red;为public Material white, red;它工作正常。我需要更改什么才能从我的资产/资源/材料文件夹中获取材料?
在我的 Unity 项目中,我有一个Empty GameObject ,它有 3 个子GameObjects。该空的游戏物体被称为Cable Strip如果摄像机移动到一个特殊的位置,我想这三个子对象的材料开始发生变化,从白色到黄色和背部。不幸的是,我所有的解决方案都不起作用。
这是我到目前为止:
using UnityEngine
public class BlinkingObject : MonoBehaviou
{
public GameObject CableStrip;
public Material white, yellow;
public GameObject camManager; //Empty GameObject with the Main Camera
private Vector3 camPosition;
bool blinkObject;
void Update()
{
if(camManager.transform.position == camPosition)
{
InvokeRepeating("Blink", 0, 1);
}
}
public void Blink()
{
Renderer[] colorCable = CableStrip.GetComponentsInChildren<Renderer>(true);
foreach (var cableChild in colorCable)
{
if(blinkObject)
{
cableChild.material = yellow;
blinkObject = false;
} …Run Code Online (Sandbox Code Playgroud)