我正在编写一个重启功能,我想删除属于某个面板的所有预制件的实例。此代码将新输入字段添加到父
private void ShowInputField(){
GameObject inputFieldGameObject = inputFieldObjectPool.GetObject();
//reset pool since it may have been disabled by the DeActivatePreviousInputFields function
InputField inputField = inputFieldGameObject.GetComponent<InputField>();
inputField.Select ();
inputField.ActivateInputField ();
inputField.interactable = true;
inputField.readOnly = false;
//make this the previous inputs
previousInputs = inputFieldGameObject;
//assign the prefab to its parent panel
inputFieldGameObject.transform.SetParent (inputFieldParent);
inputFieldGameObject.transform.localScale = Vector3.one;
Canvas.ForceUpdateCanvases ();
//make scroll bar always show the last added input field
GameObject.Find ("ScrollView").GetComponent<ScrollRect> ().horizontalNormalizedPosition = 1;
}
重启的代码,它会销毁预制件的所有实例并再次添加一个,如下所示。
void RestartGame(){
//gets all elements of the parent
InputField [] childElements = letterPanel.GetComponentsInChildren<InputField> ();
Debug.Log (childElements.Count());
foreach (var item in childElements) {
Destroy (item);
}
ShowInputField()
}
这应该会破坏预制件的所有实例,但它不会使它们只能通过下面的图像显示它来编辑
我该如何解决这个问题?提前谢谢你......
答案 0 :(得分:2)
您试图从列举的列表中销毁当前项目,但这是一个无法销毁的输入字段。而是通过引用它属于并销毁它的游戏对象,删除输入字段。
答案 1 :(得分:0)
我做了这个并且有效
void RestartGame(){
//gets all elements of the parent
InputField [] childElements = letterPanel.GetComponentsInChildren<InputField> ();
Debug.Log (childElements.Count());
foreach (vInputField item in childElements) {
if(item.name.Contains("InputField")){
Destroy (item.gameObject);
}
}
ShowInputField()
}