【问题标题】:How to Destroy and Instantiate a GameObject?如何销毁和实例化游戏对象?
【发布时间】:2019-04-15 07:27:20
【问题描述】:

我有一个游戏对象,我想根据单击的按钮来销毁和实例化它。我真的不明白它是如何工作的,但它类似于下面的代码吗?然后我将脚本附加到某个游戏对象上,然后通过函数名称附加到 onClick 设置上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreateDestroyAR : MonoBehaviour {

    //PAGE HAS BEEN CREATED AS A PREFAB
    public GameObject Page;
    public void CreatePage() {
        Instantiate(Page);
    }

    public void DestroyPage()
    {
        Destroy(Page);
    }
}

是的,它们是我可以使用 setActive 的“页面”,但是当单击不同的按钮时需要销毁其中一个“页面”,并在每次单击相应按钮时重新实例化。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class CreateDestroyAR : MonoBehaviour {
    
        //PAGE HAS BEEN CREATED AS A PREFAB
        public GameObject Page;
        private GameObject instantiatedPage;
        public void CreatePage() {
           instantiatedPage = Instantiate(Page);
        }
    
        public void DestroyPage()
        {
            Destroy(instantiatedPage);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      相关资源
      最近更新 更多