Execute method from command line

Hello.

I have an script inside Assets/Editor and I want to execute it from an script. I am using this

/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectProject ProjectPath -executeMethod FileName.StaticMethodName

It is not working and I don’t know why.

Any help??

Thank you very much in advance,

Santiago

2 Likes

The correct option is -projectPath, not -projectProject :slight_smile:

/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath ProjectPath -executeMethod FileName.StaticMethodName

3 Likes

:slight_smile: Thank you very much!

I changed it but it is still not working. It does not execute the script.

The script’s name is MyScript.cs and it contains just this (at present):

static void MyMethod () {
System.IO.File.WriteAllText (“/tmp/FicheroDePrueba.txt”, “Este fichero se puede borrar”);
}

I try to execute the script like this:

/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath /tmp/MyUnityProject -executeMethod MyScript.MyMethod

I execute that line from a bash script, not directly from the command line. I tried to execute it as sudo, but nothing… What am I doing wrong??

Thank you!!

Hola @Santy :wink:

Add this line to your function Debug.Log(“Writing to file”); and then check the Editor.log. If your debug message appears, the problems should be in your WriteAllText call.

Jejeje, Hola! :slight_smile:

Still not working… :rage:

What I understand is that I can execute a CS file method from command line with Unity using this line:
/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectProject ProjectPath -executeMethod FileName.StaticMethodName

That line will launch Unity, that will start the project and execute the method of that file (FileName.cs).

Now the file only contains this, without using statements, only this:
static void MyMethod () {
Debug.Log (“Esto es una prueba”);
}

Thank you Jordi.

I created a class in Assets/Editor folder called Test with your method.

This is my call to Unity:

./Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath /Users/Jordi/Batchmode -executeMethod Test.MyMethod

If I open the Editor.log (/Users/Jordi/Library/Logs/Unity/Editor.log), I see this text:

Esto es una prueba
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
Test:MyMethod() (at Assets/Editor/Test.cs:7)
(Filename: Assets/Editor/Test.cs Line: 7)

2 Likes

Thank you very much Jordi. Could you write your entire class content?? (including using sentences or any other). I must be making a mistake somewhere…

I got it!
Everything was OK but I had some errors in my script.

I wrote using UnityEditor; and I have also declared the static method inside a class, something like this:

using UnityEngine;
using UnityEditor;

public class MyScriptName {

public static void MyMethod () {
Debug.Log (“Here you put your code”);
}
}

And you have to call it from command line like this:
./Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath ${PROJECT_PATH} -executeMethod MyScripName.MyMethod

And your script must be in Assets/Editor.

5 Likes