简体   繁体   中英

How can I call a method inside Update only once?

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

[ExecuteInEditMode]
public class GameObjectInfo : MonoBehaviour
{
    [System.Serializable]
    public class GameObjectstInfo
    {
        public GameObject parent;
        public int childrenCount;
        public List<Transform> children = new List<Transform>();
    }

    public string gameObjectsInfo = "";
    public GameObjectstInfo[] objectsinfo;

    private bool searching = false;

    // Start is called before the first frame update
    void Start()
    {
        Search();
    }

    private void Update()
    {

    }

    public void Search()
    {
        if (gameObjectsInfo != "")
        {
            var foundObjects = FindGameObjectsWithName(gameObjectsInfo);
            objectsinfo = new GameObjectstInfo[foundObjects.Length];

            for (int i = 0; i < foundObjects.Length; i++)
            {
                objectsinfo[i] = new GameObjectstInfo();
                objectsinfo[i].parent = foundObjects[i];

                foreach (Transform child in foundObjects[i].transform)
                {
                    objectsinfo[i].childrenCount += 1;
                    objectsinfo[i].children.Add(child);
                }
            }
        }
    }

    GameObject[] FindGameObjectsWithName(string nameIt)
    {
        int it = 0;
        GameObject[] objArr;
        bool b = false;
        while (!b)
        {
            if (GameObject.Find(nameIt))
            {
                GameObject.Find(nameIt).name = nameIt + it;
                it++;
            }
            else
            {
                b = true;
            }
        }

        objArr = new GameObject[it];
        while (it > 0)
        {
            it--;
            objArr[it] = GameObject.Find(nameIt + it);
            objArr[it].name = nameIt;
        }

        return objArr;
    }
}

I want to make that only if the string var gameObjectsInfo is not empty call Search(); once make a search once and if the user change the string inside the var gameObjectsInfo make a new search each time. But each time one search and a new search if the string has changed.

The main goal is to be able to search in real time inside the Update or using the button. The button part is working fine but I want to be able to search also in real time inside the Update.

The button script:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(GameObjectInfo))]
public class GameObjectInfoButton : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        GameObjectInfo myScript = (GameObjectInfo)target;
        if (GUILayout.Button("Search"))
        {
            myScript.Search();
        }
    }
}

I think one solution could be something like this: store the previous state of gameObjectsInfo and compare to the current gameObjectsInfo . If they are not equivalent then gameObjectsInfo has been changed.

...

public string previousGameObjectsInfo = "";   // to store the previous state
public string gameObjectsInfo = "";

...

private void Update()
{
    if(gameObjectsInfo != "" && gameObjectsInfo != previousGameObjectsInfo)
    {
        Search();   // or anything else
    }

    previousGameObjectsInfo = gameObjectsInfo;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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