代码之家  ›  专栏  ›  技术社区  ›  Wesley Schravendijk

Unity将节点复制到内容容器

  •  0
  • Wesley Schravendijk  · 技术社区  · 6 年前

    快速提问。 我已经四处找了好几天了,但不知怎么的,我没法让它工作。

    我想通过一个c#脚本将一个节点框复制到一个内容容器中[参见图片]。 在Unity和C#中完成类似任务的最佳方式是什么?

    据我所知,这是正确的;这可以通过实例化函数完成,对吗?

    // get gameobject
    public GameObject listpanel;
    
    // Start is called before the first frame update
    void Start()
    {
        // find tag
        listpanel = GameObject.Find("listpanel");
    
        // duplicate tag
        GameObject duplicate = Instantiate(listpanel);
    
        // set panel not active
        //listpanel.SetActive(!listpanel.activeSelf);
    }
    

    任何人都知道我做错了什么。

    非常感谢您的帮助。

    enter image description here

    更新:这在控制台上完全没有错误;这似乎有效,但什么也没出现。

    韦斯利

    0 回复  |  直到 6 年前
        1
  •  1
  •   will-yama    6 年前

    如果您可以分享控制台中的任何错误,这将有助于我们进行调试。

    据我所见,如果你是通过标签找到的 FindGameObjectsWithTag 方法,而不是 发现 。这将作为数组返回。 还有 例示 方法有许多参数,其中第四个参数将说明它将附加到哪个父对象。

    下面是一个实例化位于资产“资源”文件夹中的预置的示例。 预置被实例化为另一个现有对象的子对象。

      void Start()
      {
        //State what to instantiate, where, and the rotation
        GameObject nodeobject = (GameObject)Resources.Load("mynodeobject");
        Vector3 position = new Vector3(0,0,0);
        Quaternion rotation = Quaternion.Euler(0,270,0);
    
        //State the parent object
        GameObject parentobject = GameObject.Find("ParentObject");
    
        //Instantiate the prefab
        Instantiate(nodeobject, position, rotation, parentobject.transform);
      }