如何在 运行 命令行 (Unity3d) 的脚本中获取游戏对象(在场景中)
How get GameObject (in scene) in script which run command line (Unity3d)
我在 Unity3d 中为 运行 脚本使用命令行:
"C:\Program Files (x86)\Unity\Editor\Unity.exe" -quit -batchmode -projectPath "path_to_project" -executeMethod Command.Do
在方法 "Do" 中:我尝试在场景中获取 GameObject:GameObject.Find("Automation"),但结果为 null(.
class Command
{
static public void Do()
{
Debug.Log("Test");
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
var scene = EditorBuildSettings.scenes[0];
string name = scene.path.Substring(scene.path.LastIndexOf('/') + 1);
name = name.Substring(0, name.Length - 6);
SceneManager.LoadScene(0);
var automationGo = GameObject.Find("Automation");
System.Console.WriteLine("automationGo " + automationGo);
}
}
我解决了:
相反 SceneManager.LoadScene(0) 需要使用:EditorSceneManager.OpenScene(scene.path);
万一有人从 Google 找到了这个帖子并想 运行 游戏来无头地测试东西(像我一样),这里是你需要的所有信息:
public class PlayTestsLoader : MonoBehaviour
{
public static bool PlayTestsRunning = false;
public static bool PlayTestsWantToStop = false;
// Run with (note paths are Linux syntax - replace as needed)
// ./Unity -projectPath ~/unityprojectname -executeMethod PlayTestsLoader.LoadTestScenes -logfile "~unityprojectname/logs.txt" -batchmode
public static void LoadTestScenes()
{
try
{
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android); // Test Android code paths as if you were doing it manually in Editor
EditorSceneManager.OpenScene($"Assets/Scenes/mainscene.unity"); // Load the scene in Editor - Not at play time!
EditorApplication.isPlaying = true; // When this static method returns, setting isPlaying to true will start the game
TestLog($"Entered playmode");
}
catch (Exception e)
{
TestLog($"Had exception {e}");
}
TestLog($"Complete PlayTestsLoader.LoadTestScenes");
}
static void TestLog(string log)
{
Debug.Log($"=== PlayTests === {log}");
}
public void Update()
{
if (PlayTestsRunning && PlayTestsWantToStop)
{
TestLog("ALL TESTS COMPLETED -- RETURNING 0");
EditorApplication.Exit(0); // This will stop the game from running when you're done, and make the headless (invisible) editor exit
}
}
public void OnEnable()
{
PlayTestsRunning = true;
TestLog("PlayTestsLoader Monobehaviour started");
StartCoroutine(PlayTestJourneyController());
}
IEnumerator PlayTestJourneyController()
{
yield return StartPvP();
yield return SayHelloAfterPvP();
PlayTestsWantToStop = true;
yield return null;
}
IEnumerator StartPvP()
{
TestLog($"Hello from StartPvP");
yield break;
}
IEnumerator SayHelloAfterPvP()
{
TestLog($"Hello from SayHelloAfterPvP");
yield break;
}
}
基本上,这可以让您设置所需的一切 - 您的更新循环、退出条件、任何协程等。
我在 Unity3d 中为 运行 脚本使用命令行: "C:\Program Files (x86)\Unity\Editor\Unity.exe" -quit -batchmode -projectPath "path_to_project" -executeMethod Command.Do
在方法 "Do" 中:我尝试在场景中获取 GameObject:GameObject.Find("Automation"),但结果为 null(.
class Command
{
static public void Do()
{
Debug.Log("Test");
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
var scene = EditorBuildSettings.scenes[0];
string name = scene.path.Substring(scene.path.LastIndexOf('/') + 1);
name = name.Substring(0, name.Length - 6);
SceneManager.LoadScene(0);
var automationGo = GameObject.Find("Automation");
System.Console.WriteLine("automationGo " + automationGo);
}
}
我解决了: 相反 SceneManager.LoadScene(0) 需要使用:EditorSceneManager.OpenScene(scene.path);
万一有人从 Google 找到了这个帖子并想 运行 游戏来无头地测试东西(像我一样),这里是你需要的所有信息:
public class PlayTestsLoader : MonoBehaviour
{
public static bool PlayTestsRunning = false;
public static bool PlayTestsWantToStop = false;
// Run with (note paths are Linux syntax - replace as needed)
// ./Unity -projectPath ~/unityprojectname -executeMethod PlayTestsLoader.LoadTestScenes -logfile "~unityprojectname/logs.txt" -batchmode
public static void LoadTestScenes()
{
try
{
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android); // Test Android code paths as if you were doing it manually in Editor
EditorSceneManager.OpenScene($"Assets/Scenes/mainscene.unity"); // Load the scene in Editor - Not at play time!
EditorApplication.isPlaying = true; // When this static method returns, setting isPlaying to true will start the game
TestLog($"Entered playmode");
}
catch (Exception e)
{
TestLog($"Had exception {e}");
}
TestLog($"Complete PlayTestsLoader.LoadTestScenes");
}
static void TestLog(string log)
{
Debug.Log($"=== PlayTests === {log}");
}
public void Update()
{
if (PlayTestsRunning && PlayTestsWantToStop)
{
TestLog("ALL TESTS COMPLETED -- RETURNING 0");
EditorApplication.Exit(0); // This will stop the game from running when you're done, and make the headless (invisible) editor exit
}
}
public void OnEnable()
{
PlayTestsRunning = true;
TestLog("PlayTestsLoader Monobehaviour started");
StartCoroutine(PlayTestJourneyController());
}
IEnumerator PlayTestJourneyController()
{
yield return StartPvP();
yield return SayHelloAfterPvP();
PlayTestsWantToStop = true;
yield return null;
}
IEnumerator StartPvP()
{
TestLog($"Hello from StartPvP");
yield break;
}
IEnumerator SayHelloAfterPvP()
{
TestLog($"Hello from SayHelloAfterPvP");
yield break;
}
}
基本上,这可以让您设置所需的一切 - 您的更新循环、退出条件、任何协程等。