简体   繁体   English

无法重新创建子游戏对象统一 c#

[英]Can't recreate child gameObjects unity c#

There is a ScrollView with child buttons and Text objects which are dynamically created by the script.有一个带有子按钮和由脚本动态创建的 Text 对象的 ScrollView。 For the first time, the buttons and text are created normally, but when you try to destroy the ScrollView, and add buttons and text to it, they are not created again.第一次正常创建了按钮和文本,但是当您尝试销毁 ScrollView 并向其添加按钮和文本时,它们不会再次创建。

if (GameObject.Find("ScrollViewUsers") != null)
    {
        Destroy(GameObject.Find("ScrollViewUsers"));
    }

    GameObject scrollView = Instantiate(GameObject.Find("ScrollViewUsersSample"), GameObject.Find("PanelFindFriends").transform, true);
    scrollView.name = "ScrollViewUsers";
    scrollView.GetComponent<RectTransform>().localPosition = new Vector3(0, -20, 0);
    scrollView.GetComponent<RectTransform>().sizeDelta = new Vector2(GameObject.Find("PanelFindFriends").GetComponent<RectTransform>().sizeDelta.x, GameObject.Find("PanelFindFriends").GetComponent<RectTransform>().sizeDelta.y - 40);
    scrollView.transform.Find("Viewport/ContentUsersSample").name = "ContentUsers";

    Debug.Log("userList.Count=" + userList.Count);
    float _y = 0;
    foreach (KeyValuePair<int, string> user in userList)
    {
        Debug.Log("user=" + user.Value);
        Instantiate(GameObject.Find("TextUser"), GameObject.Find("ContentUsers").transform, true);
        GameObject.Find("TextUser(Clone)").name = "user_" + user.Value;
        GameObject.Find("user_" + user.Value).GetComponent<Text>().text = user.Value;
        GameObject.Find("user_" + user.Value).GetComponent<RectTransform>().localPosition = new Vector3(-25, y, 0);

        Instantiate(GameObject.Find("ButtonAddUser"), GameObject.Find("ContentUsers").transform, true);
        GameObject.Find("ButtonAddUser(Clone)").name = "addUser_" + user.Value;
        GameObject.Find("addUser_" + user.Value).GetComponent<RectTransform>().localPosition = new Vector3(62, y, 0);
        y -= 40;

        _y += (GameObject.Find("user_" + user.Value).GetComponent<RectTransform>().sizeDelta.y + 20);
}

Here is an example of your required这是您需要的示例

List<string> userList = new List<string> { "User1", "User2", "User3" };

[SerializeField]
private Text TextUserPrefabRef;

[SerializeField]
private Button ButtonAddUserPrefabRef;

[SerializeField]
private GameObject ScrollViewUsersSamplePrefabRef;

[SerializeField]
private Transform PanelFindFriendsRef;

private Transform ContentUsers;

public void CreateNew () 
{
    if (GameObject.Find("ScrollViewUsers") != null)
    {
        Debug.Log("Destroying");
        Destroy(GameObject.Find("ScrollViewUsers"));
    }

    GameObject scrollView = Instantiate(ScrollViewUsersSamplePrefabRef, PanelFindFriendsRef);
    scrollView.name = "ScrollViewUsers";

    ContentUsers = scrollView.transform.Find("Viewport/ContentUsersSample");
    ContentUsers.name = "ContentUsers";

    foreach (string user in userList)
    {
        Debug.Log("user=" + user);
        GameObject newTextUser = Instantiate (TextUserPrefabRef.gameObject, ContentUsers) as GameObject;
        newTextUser.GetComponent<Text>().text = user;

        GameObject newButton = Instantiate (ButtonAddUserPrefabRef.gameObject, ContentUsers);
        newButton.name = "addUser_" + user;
    }   
}

Then make prefabs of your scrollView, AddUserButton and Text Objects And assign them in inspector.然后制作滚动视图、AddUserButton 和文本对象的预制件并在检查器中分配它们。

In the foreach loop, Create a new variable instead of the GameObject.Find().在 foreach 循环中,创建一个新变量而不是 GameObject.Find()。

eg:例如:

var go = Instantiate(GameObject.Find("TextUser"), GameObject.Find("ContentUsers").transform, true);)
go.name = "...";
go.GetComponent<Text>().text = "...";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM