【问题标题】:How can I call a method inside Update only once?如何在 Update 中只调用一次方法?
【发布时间】:2019-03-20 07:53:51
【问题描述】:
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;
    }
}

我只想在字符串 var gameObjectsInfo 不为空的情况下调用Search(); 进行一次搜索,并且如果用户更改了 var gameObjectsInfo 中的字符串,则每次都进行新的搜索。但如果字符串发生变化,每次搜索和新搜索。

主要目标是能够在更新内或使用按钮进行实时搜索。按钮部分工作正常,但我希望能够在更新中实时搜索。

按钮脚本:

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();
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我认为一种解决方案可能是这样的:存储gameObjectsInfo 的先前状态并与当前gameObjectsInfo 进行比较。如果它们不相等,则 gameObjectsInfo 已更改。

    ...
    
    public string previousGameObjectsInfo = "";   // to store the previous state
    public string gameObjectsInfo = "";
    
    ...
    
    private void Update()
    {
        if(gameObjectsInfo != "" && gameObjectsInfo != previousGameObjectsInfo)
        {
            Search();   // or anything else
        }
    
        previousGameObjectsInfo = gameObjectsInfo;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2019-09-24
      • 2017-09-13
      • 2019-04-01
      • 2011-09-06
      • 2011-03-07
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      相关资源
      最近更新 更多