C# 从资产中获取材料
在我的Unity项目中,我试图用一些自制材料突出一些对象。 不幸的是,我发现的代码不起作用 以下脚本添加到三个不同的游戏对象:C# 从资产中获取材料,c#,unity3d,C#,Unity3d,在我的Unity项目中,我试图用一些自制材料突出一些对象。 不幸的是,我发现的代码不起作用 以下脚本添加到三个不同的游戏对象: Renderer[] rend_ThisObject; Material white, red; void Start() { // I tried the following two code parts: white = Resources.Load<Material>("White"); white = Resources.L
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;
}
}
渲染器[]渲染此对象;
材质为白色、红色;
void Start()
{
//我尝试了以下两个代码部分:
白色=资源。加载(“白色”);
白色=资源加载(“材料/白色.垫”,类型(材料))作为材料;
红色=资源。加载(“红色”);
红色=资源负载(“材料/红色材料”,类型(材料))作为材料;
}
无效更新()
{
foreach(rend_ThisObject中的变量child)
{
child.material=blinkObject?白色:红色;
}
}
如果我运行项目,带有脚本的游戏对象将显示为粉红色,并且不会闪烁
如果我将材质更改为白色、红色代码>至<代码>公共材料白色、红色代码>工作正常。要从我的资产/资源/材料文件夹中获取材料,我需要做哪些更改?通常:
从Unity自身:
不要使用它。
简单地使用public
字段并通过Inspector进行设置有什么问题
如果您不希望它们是公开的,那么只需使用
取而代之的是通过检查员分配他们
然后假设您的路径不是assets/resources/material
,而是assets/resources/material
请参见
你试过了
white = Resources.Load<Material>("White");
white = Resources.Load("Material/White.mat", typeof(Material)) as Material;
它应该是简单的
white = Resources.Load<Material>("Material/White");
white=Resources.Load(“物料/白色”);
现在,另一个一般性说明:
在每帧的基础上进行此闪烁不是一个好主意
而是使用某种延迟,例如
// adjust via the Inspector as well
[Tooltip("Color changes per second")]
[SerializeField] private float blinkFrequency = 2f;
private float timer;
void Update()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
timer = 1 / blinkFrequency;
blinkObject = !blinkObject;
foreach (var child in rend_ThisObject)
{
child.material = blinkObject ? white : red;
}
}
}
//也可以通过检查器进行调整
[工具提示(“每秒颜色变化”)]
[SerializeField]专用浮点数闪烁频率=2f;
专用浮点定时器;
无效更新()
{
timer-=Time.deltaTime;
中频(定时器)
// adjust via the Inspector as well
[Tooltip("Color changes per second")]
[SerializeField] private float blinkFrequency = 2f;
private float timer;
void Update()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
timer = 1 / blinkFrequency;
blinkObject = !blinkObject;
foreach (var child in rend_ThisObject)
{
child.material = blinkObject ? white : red;
}
}
}