在我的Unity项目中,我尝试用一些自制的材料突出显示某些对象。
不幸的是,我发现的代码不起作用。
下面的脚本被添加到三个不同的GameObjects:
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;
}
}
如果我运行该项目,则带有脚本的游戏对象将显示为粉红色,并且不会闪烁。
如果我将Material white, red;
更改为public Material white, red;
,则可以正常工作。为了从我的资源/资源/材料文件夹中获取材料,我必须做些什么更改?
参考方案
一般来说:
来自Unity自己的Best practise for Resources
:
不要使用它。
仅使用您的public
字段并通过检查器进行设置有什么问题?
如果您不希望它们成为public
,则只需使用[SerializeField]
[SerializeField] private Material white;
[SerializeField] private Material red;
而是通过检查器分配它们!
然后假设您的路径不是assets/resources/material
而是Assets/Resources/Material
参见Resources.Load
!
你试过了
white = Resources.Load<Material>("White");
white = Resources.Load("Material/White.mat", typeof(Material)) as Material;
两者都是错误的:
第一次缺少文件夹路径
该路径相对于项目的Resources
文件夹内任何名为Assets
的文件夹
第二个文件结尾太多。
注意:必须省略扩展名。
因此,使用路径加载文件
Assets/Resources/Material/White.mat
它应该只是
white = Resources.Load<Material>("Material/White");
现在再来一个一般说明:
每帧进行一次眨眼不是一个好主意!
而不是使用某种延迟,例如
// 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;
}
}
}
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
改造正在返回一个空的响应主体 - java我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…
每个文件合并后添加换行 - python我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…
Json到php,json_decode返回NULL - php我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…
您如何在列表内部调用一个字符串位置? - python我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…