【发布时间】:2014-11-28 21:34:44
【问题描述】:
我正在尝试替换整个 Mesh 或 GameObject(不是实例化或禁用和启用其他),因为我的 Mesh 与其他的不同,但 GameObject、位置和脚本是相同的,所以,这就是为什么我需要更改 只有网格(我想这是正确的做法)。
所以,这是 C# 中的代码
public GameObject MyMainGameObject;
public GameObject[] OtherMeshMaterials;
int maxMaterials;
int arrayPos = 0;
void Start ()
{
maxMaterials = OtherMeshMaterials.Length-1;
Debug.Log ( "Total = "+ maxMaterials );
}
void updateMaterials()
{
//Cycle forward
if (Input.GetKeyDown (KeyCode.U)) {
if (arrayPos == maxMaterials)
arrayPos = 0;
else
arrayPos++;
MyMainGameObject.GetComponent<MeshRenderer>().material = OtherMeshMaterials[arrayPos].GetComponent<MeshRenderer>().material;
MyMainGameObject.GetComponent<MeshFilter>().mesh = OtherMeshMaterials[arrayPos].GetComponent<MeshFilter>().mesh;
Debug.Log ( "NAME = "+ MyMainGameObject.transform.name );
}
//Cycle backwards
if (Input.GetKeyDown (KeyCode.Y)) {
if (arrayPos == 0)
arrayPos = maxMaterials;
else
arrayPos--;
MyMainGameObject.GetComponent<MeshRenderer>().material = OtherMeshMaterials[arrayPos].GetComponent<MeshRenderer>().material;
MyMainGameObject.GetComponent<MeshFilter>().mesh = OtherMeshMaterials[arrayPos].GetComponent<MeshFilter>().mesh;
Debug.Log ( "NAME = "+ MyMainGameObject.transform.name );
}
}
void Update ()
{
updateMaterials ();
}
当我更改为另一个时,网格比另一个小(但还不是问题)并且不再更改,名称已更改,但网格的更改发生了一次,再也不会发生......
我使用 4 个或更多 OtherMeshMaterials 来测试更多选项,但只更改了一次。
感谢您的帮助!
已编辑
脚本转到 ARCamera。
这是 2 个游戏对象的图像:http://www.loverde.com.br/temp_files/Unity_MeshsToChange.png
1 - Hexa_GO > it_hex_3450 > pms_hexa5034(这是我链接到脚本的游戏对象)
2 - Eko_GO > it_eko_3450 > pms_eko_5034(这也是我链接到脚本的游戏对象)
我试图将 pms_hexa5034 更改为 pms_ekoxxx,反之亦然。只换一个,永不回头,永不前进。
已编辑
请查看已批准答案上的 cmets 以了解我的工作。
感谢@gunnar-b 一直以来的帮助
【问题讨论】: