MeshFilter.sharedMesh 停止游戏后更改仍然存在
MeshFilter.sharedMesh changes remains after stopping game
我正在使用以下代码翻转立方体背面的 UV:
Vector2[] uvs = GetComponent().sharedMesh.uv;
uvs[7] = new Vector2(0, 0);
uvs[6] = new Vector2(1, 0);
uvs[11] = new Vector2(0, 1);
uvs[10] = new Vector2(1, 1);
GetComponent().sharedMesh.uv = uvs;
它按预期翻转了 uv,但是如果我注释掉所有内容并在 unity 编辑器中点击播放,我看到 uv 仍然翻转(好像代码对 uv 产生了永久性更改)。
如果我关闭并重新打开 Unity 编辑器,然后点击播放,uv 将再次处于初始(未翻转)状态。
在这种情况下这不是真正的问题,但它在调试过程中给我带来了很多麻烦,有人可以解释一下这种行为吗?
您正在直接修改 3d model/mesh,它会存储更改,而不是在您退出播放模式时重置它们。
来源:
https://docs.unity3d.com/Manual/ModifyingSourceAssetsThroughScripting.html
Direct Modification
IMPORTANT NOTE
The method presented below will modify actual source asset files used within Unity. These modifications are not undoable. Use them with caution.
Now let’s say that we don’t want the material to reset when we exit play mode. For this, you can use renderer.sharedMaterial. The sharedMaterial property will return the actual asset used by this renderer (and maybe others).
The code below will permanently change the material to use the Specular shader. It will not reset the material to the state it was in before Play Mode.
额外知识:
据我所知,如果您修改 ScriptableObjects,也会发生同样的事情。
有两种方法可以检索网格:
1.GetComponent<MeshFilter>().sharedMesh
MeshFilter.sharedMesh
指向模型的原始网格,这意味着当您修改它时,该模型的所有实例化预制件也将被修改。建议您仅使用MeshFilter.sharedMesh
读取网格但不要使用MeshFilter.sharedMesh
返回的数据来修改它。
这就是您看到当前效果的原因,只有当您想要影响使用此网格的所有其他 prefabs/GameObjects 时才应使用。
2.GetComponent<MeshFilter>().mesh
MeshFilter.mesh
指向原始网格的当前副本。当您调用 MeshFilter.mesh
时,会生成原始网格的副本,而 returned.If 副本已存在,则返回。这使得修改网格成为可能,而无需修改原始网格或影响使用此网格的其他 prefabs/GameObjects。
MeshFilter.mesh
是您应该用来修改网格的内容。
我正在使用以下代码翻转立方体背面的 UV:
Vector2[] uvs = GetComponent().sharedMesh.uv;
uvs[7] = new Vector2(0, 0);
uvs[6] = new Vector2(1, 0);
uvs[11] = new Vector2(0, 1);
uvs[10] = new Vector2(1, 1);
GetComponent().sharedMesh.uv = uvs;
它按预期翻转了 uv,但是如果我注释掉所有内容并在 unity 编辑器中点击播放,我看到 uv 仍然翻转(好像代码对 uv 产生了永久性更改)。
如果我关闭并重新打开 Unity 编辑器,然后点击播放,uv 将再次处于初始(未翻转)状态。
在这种情况下这不是真正的问题,但它在调试过程中给我带来了很多麻烦,有人可以解释一下这种行为吗?
您正在直接修改 3d model/mesh,它会存储更改,而不是在您退出播放模式时重置它们。
来源:
https://docs.unity3d.com/Manual/ModifyingSourceAssetsThroughScripting.html
Direct Modification
IMPORTANT NOTE
The method presented below will modify actual source asset files used within Unity. These modifications are not undoable. Use them with caution.
Now let’s say that we don’t want the material to reset when we exit play mode. For this, you can use renderer.sharedMaterial. The sharedMaterial property will return the actual asset used by this renderer (and maybe others).
The code below will permanently change the material to use the Specular shader. It will not reset the material to the state it was in before Play Mode.
额外知识:
据我所知,如果您修改 ScriptableObjects,也会发生同样的事情。
有两种方法可以检索网格:
1.GetComponent<MeshFilter>().sharedMesh
MeshFilter.sharedMesh
指向模型的原始网格,这意味着当您修改它时,该模型的所有实例化预制件也将被修改。建议您仅使用MeshFilter.sharedMesh
读取网格但不要使用MeshFilter.sharedMesh
返回的数据来修改它。
这就是您看到当前效果的原因,只有当您想要影响使用此网格的所有其他 prefabs/GameObjects 时才应使用。
2.GetComponent<MeshFilter>().mesh
MeshFilter.mesh
指向原始网格的当前副本。当您调用 MeshFilter.mesh
时,会生成原始网格的副本,而 returned.If 副本已存在,则返回。这使得修改网格成为可能,而无需修改原始网格或影响使用此网格的其他 prefabs/GameObjects。
MeshFilter.mesh
是您应该用来修改网格的内容。