'How get GameObject (in scene) in script which run command line (Unity3d)
I use command line for run script in Unity3d: "C:\Program Files (x86)\Unity\Editor\Unity.exe" -quit -batchmode -projectPath "path_to_project" -executeMethod Command.Do
In method "Do": I try get GameObject in scene: GameObject.Find("Automation"), but result 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);
}
}
Solution 1:[1]
In case anyone finds this thread from Google and wants to run the game to test things headlessly (like me), here's all the info you'll need:
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;
}
}
Basically this gets you setup with everything you need - Your update loop, your exit conditions, any coroutines, etc etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dr-Bracket |
