Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Execute method from command line

Discussion in 'Editor & General Support' started by Santy, Oct 1, 2015.

  1. Santy

    Santy

    Joined:
    Apr 16, 2013
    Posts:
    26
    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
     
    will_unity731 and Syncrotron like this.
  2. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    The correct option is -projectPath, not -projectProject :)

    /Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath ProjectPath -executeMethod FileName.StaticMethodName
     
    will_unity731 and liortal like this.
  3. Santy

    Santy

    Joined:
    Apr 16, 2013
    Posts:
    26
    :) 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!!
     
  4. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Hola @Santy ;)

    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.
     
  5. Santy

    Santy

    Joined:
    Apr 16, 2013
    Posts:
    26
    Jejeje, Hola! :)

    Still not working... :mad:

    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.
     
  6. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    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)
     
    will_unity731 and Keyserjaya99 like this.
  7. Santy

    Santy

    Joined:
    Apr 16, 2013
    Posts:
    26

    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...
     
  8. Santy

    Santy

    Joined:
    Apr 16, 2013
    Posts:
    26
    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.