代码之家  ›  专栏  ›  技术社区  ›  zyonneo

如何持续检查游戏对象是否会在unity的层次结构中实例化?

  •  0
  • zyonneo  · 技术社区  · 7 年前

    我想继续检查一个游戏对象是否会被实例化。我所拥有的只是游戏对象的名称,比如“Model”。如果模型在层次结构中处于活动状态,我想做些什么。问题是最初游戏对象不可用。后来只会被实例化。如何检查名为的游戏对象是否存在于层次结构中,而层次结构将是一个克隆。

    if(gameobject.name=="Model(Clone)") { //Do something }

    这将返回一个 空值

    4 回复  |  直到 7 年前
        1
  •  1
  •   Noblight    7 年前

  • 初始化变量模型
    Gameobject Model;


  • if (Model != null && Model.name == "Model(Clone)"){//return false
        //Do Something
    }
    

  • 用名称实例化游戏对象。
    Gameobject Model = Instantiate (somePrefab);

  • if 需要放在里面 void update() ,或者像这样的循环 invoke()

  • 当游戏对象模型不为空并且有名为Model(Clone)的游戏对象时,它将检查if并返回true
    if (Model != null && Model.name == "Model(Clone)"){//return true
        //Do Something
    }
    
  •     2
  •  1
  •   Daveloper    7 年前

    if(gameobject!=null && gameobject.name=="Model(Clone)")
    {
        //Do something
    }
    

    如果你不能访问这个游戏对象,而你只是想检测它是否存在,那么可以尝试以下方法

    var go = GameObject.Find("Model(Clone)");
    if(go!=null)
    {
        //Do something
    }
    

    var gos = GameObject.FindObjectsOfType<MyScript>();
    if(gos!=null && gos.Length > 0)
    {
        // foreach(var go in gos) {}
        //Do something
    }
    
        3
  •  1
  •   Alox    7 年前

    如果是我,我会这样做。

    List<GameObject> models = new List<GameObject>;
    public GameObject baseModel; //Your model
    
    private void CreateModel()
    {
       GameObject obj = GameObject.Instantiate(baseModel) as GameObject;
       models.Add(obj);
    }
    

    void Update()
    {
       if(models.Count > 0)
       {
           //Do Something
       }
    }
    

    记住,如果你要摧毁这个物体,别忘了这样做。

    private void DestroyModel(GameObject obj)
    {
       models.Remove(obj);
       Destroy(obj);
    }
    

    这是一个很好的实践,以分配您的游戏对象的地方。英雄联盟

        4
  •  1
  •   Devwulf    7 年前


    Instantiate 一个物体,你可以 Invoke

    您首先需要编写 Instantiates Invokes 紧接着的UnityEvent(顺便说一句,整个事件都在一个.cs文件中):

    public class GameObjectSpawner : MonoBehaviour 
    {
        public GameObject GameObjectPrefab;
        public GameObjectEvent OnCreateObject;
    
        private void Update() 
        {
            // Everytime you click your left mouse button
            // Only runs once per mouse click
            if (Input.GetMouseButtonDown(0)) 
            {
                // Instantiate the gameobject...
                GameObject go = Instantiate(GameObjectPrefab);
    
                // Then immediately Invoke our event to run all the listeners
                // and passing our gameobject to them.
                OnCreateObject.Invoke(go);
            }
        }
    }
    
    // This is just so the code that 'listens' to the event can get access to the 
    // gameobject that just got instantiated.
    public class GameObjectEvent : UnityEvent<GameObject> { }
    

    因此,如果你的项目中有一个代码需要在游戏对象运行时运行 Instantiated ,该代码仅在实例化游戏对象时运行,以前从未运行过(除非手动运行该代码)。将该代码注册为 OnCreateObject

    public GameObjectSpawner Spawner;
    
    private void Start()
    {
        // You can place this anywhere you want to, but we're
        // placing this here in Start just to make sure that
        // we're already listening waaay before the gameobject
        // is instantiated.
        Spawner.AddListener(CheckHierarchy)
    }
    
    // It doesn't matter if this is public or private,
    // as long as it's void and takes in a GameObject as a parameter.
    private void CheckHierarchy(GameObject go)
    {
        // Now we can do all kinds of things to the newly instantiated
        // gameobject, like check if it's active in the hierarchy!
        if (go.activeInHierarchy == true)
        {
            Debug.Log("I LIVE!!!");
        }
    }
    

    UnityEvents的功能不止这些,但这只是帮助您开始使用它的要点。祝你好运!