C# 我怎样才能在Update中只调用一次方法?
我认为一种解决方案可能是这样的:存储C# 我怎样才能在Update中只调用一次方法?,c#,unity3d,C#,Unity3d,我认为一种解决方案可能是这样的:存储gameObjectsInfo的先前状态,并与当前的gameObjectsInfo进行比较。如果它们不相等,则更改了gameObjectsInfo using UnityEngine; using System.Collections; using UnityEditor; [CustomEditor(typeof(GameObjectInfo))] public class GameObjectInfoButton : Editor { publi
gameObjectsInfo
的先前状态,并与当前的gameObjectsInfo
进行比较。如果它们不相等,则更改了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();
}
}
}
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();
}
}
}
...
public string previousGameObjectsInfo = ""; // to store the previous state
public string gameObjectsInfo = "";
...
private void Update()
{
if(gameObjectsInfo != "" && gameObjectsInfo != previousGameObjectsInfo)
{
Search(); // or anything else
}
previousGameObjectsInfo = gameObjectsInfo;
}