if (ID == -1 || other.gameObject.layer != 9) { return; } if (other.transform.parent.name == "卡钳" && ID == 0) { transform.parent.Find("卡钳").gameObject.SetActive(true); other.transform.parent.GetComponent<FlashingController>().CloseHighlight(); DragObj = other.transform.gameObject; DragObj.SetActive(false); ID = 1; StepFubnction(ID); } else if (other.transform.name == "旧平衡块1" && ID == 1) { other.transform.GetComponent<FlashingController>().CloseHighlight(); other.transform.GetChild(0).gameObject.SetActive(true); other.transform.GetChild(0).GetComponent<FlashingController>().ShowHighlight(); DragObj.SetActive(false); isDrag = false; ID = 2; StepFubnction(ID); } else if (other.transform.parent.name == "卡钳透明" && ID == 2) { transform.parent.Find("卡钳").gameObject.SetActive(false); DragObj.SetActive(true); ID = 3; StepFubnction(ID); }优化代码
时间: 2024-01-13 09:03:31 浏览: 119
以下是可能的代码优化建议:
1. 将判断条件简化,避免多余的判断:
```
if (ID == -1 || other.gameObject.layer != 9) {
return;
}
if (ID == 0 && other.transform.parent.name == "卡钳") {
//...
}
else if (ID == 1 && other.transform.name == "旧平衡块1") {
//...
}
else if (ID == 2 && other.transform.parent.name == "卡钳透明") {
//...
}
```
2. 避免重复查找组件和对象,可以在 Start 方法中缓存相关组件和对象:
```
private GameObject cardClamp;
private GameObject dragObj;
private GameObject oldBalanceBlock1;
void Start() {
cardClamp = transform.parent.Find("卡钳").gameObject;
dragObj = ...; // 根据实际情况查找
oldBalanceBlock1 = ...; // 根据实际情况查找
}
void OnTriggerEnter(Collider other) {
if (ID == -1 || other.gameObject.layer != 9) {
return;
}
if (ID == 0 && other.transform.parent == cardClamp.transform) {
cardClamp.SetActive(true);
other.transform.parent.GetComponent<FlashingController>().CloseHighlight();
DragObj = other.transform.gameObject;
DragObj.SetActive(false);
ID = 1;
StepFubnction(ID);
}
else if (ID == 1 && other.transform == oldBalanceBlock1.transform) {
other.transform.GetComponent<FlashingController>().CloseHighlight();
other.transform.GetChild(0).gameObject.SetActive(true);
other.transform.GetChild(0).GetComponent<FlashingController>().ShowHighlight();
DragObj.SetActive(false);
isDrag = false;
ID = 2;
StepFubnction(ID);
}
else if (ID == 2 && other.transform.parent.name == "卡钳透明") {
cardClamp.SetActive(false);
DragObj.SetActive(true);
ID = 3;
StepFubnction(ID);
}
}
```
这样可以提高代码的可读性和执行效率。
阅读全文