Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Command Line, Import Asset

Discussion in 'Scripting' started by sidestepper, Sep 4, 2009.

  1. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    96
    So I've been trying to use the command line arguments with Unity to get it to import a Heart.png image I have on my home directory. While it doesn't crash or anything, after opening the project after I have run the script, my heart asset is not in the project. And after looking through the script reference on some of this stuff, it just seems, empty, like, not nearly enough information. Anyways, was hoping some one could help me out. The script I created is in my editor folder and is as follows:

    Code (csharp):
    1.  
    2. using UnityEditor;
    3.    
    4. class ImportAssetScript
    5. {
    6.      static void PerformImport()
    7.      {
    8.         AssetDatabase.ImportAsset("~/Heart.png", ImportAssetOptions.Default);
    9.      }
    10. }
    11.  
    Very simple, I am probably missing something, but like I said, the documentation for this stuff is weak at best.

    My command line call(while originally I was trying to accept a string path to the asset I wanted to import, when it didn't work, I just hard coded it in the script for now) looks like this:

    /Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projPath ~/TestProject -executeMethod ImportAssetScript.PerformImport

    I also tried it without specifying the projPath, when I run it I do see the indicator by Unity in my dock, so I know it starts unity in the background, then closes it after a second or two when the indicator goes away. But when I then open Unity, there is no heart png in that project! I don't know what to do? Is there somewhere with better documentation on this?
     
  2. tjroger

    tjroger

    Joined:
    May 6, 2017
    Posts:
    2
  3. james_cg

    james_cg

    Joined:
    Nov 13, 2019
    Posts:
    15
    Same problem here. I used the same way you used.
    Not working even if the files are added under Assets folder
     
  4. swh_

    swh_

    Joined:
    Jun 11, 2018
    Posts:
    2
    I am also having this problem, even with files under the Assets folder, in 2019.4.0f1. The logs from AssetDatabase.ImportAsset echo out as if it happened, but it doesn't do anything. I also tried using -executeMethod without -batchMode, so it seems that the AssetDatabase functions just don't work if called via -executeMethod.
     
  5. IVxIV

    IVxIV

    Joined:
    Nov 13, 2013
    Posts:
    16
    FWIW this works fine for me, in 2019.4.5:

    Code (CSharp):
    1. public class AssetImporter
    2. {
    3.  
    4.     private const string ARG_ASSET_PATH = "-path";
    5.     private const string ASSETS_DIRECTORY = "Assets";
    6.  
    7.     /// <summary>
    8.     /// Used to import an asset via command line invocation of Unity, like so:
    9.     /// C:\Program Files\Unity\Hub\Editor\2019.4.5f1\Editor\Unity.exe -batchmode -quit -projPath "C:\UnityTestProject" -executeMethod AssetImporter.Import -path "Assets\TestFile.txt"
    10.     /// </summary>
    11.     public static void Import()
    12.     {
    13.         var args = System.Environment.GetCommandLineArgs();
    14.  
    15.         if (args != null)
    16.         {
    17.             for (int i = 0; i < args.Length; i++)
    18.             {
    19.                 switch (args[i])
    20.                 {
    21.                     case ARG_ASSET_PATH:
    22.                     {
    23.                         if (++i < args.Length)
    24.                         {
    25.                             var assetPath = args[i];
    26.  
    27.                             assetPath = assetPath.TrimStart('/').TrimStart('\\');
    28.                             if (!assetPath.StartsWith(ASSETS_DIRECTORY))
    29.                             {
    30.                                 Debug.LogError($"Unable to import '{assetPath}' as it is not under the {ASSETS_DIRECTORY} directory");
    31.                             }
    32.                             else
    33.                             {
    34.                                 Debug.Log($"Importing '{assetPath}'...");
    35.                                 AssetDatabase.ImportAsset(assetPath);
    36.                             }
    37.                         }
    38.                         break;
    39.                     }
    40.                 }
    41.             }
    42.         }
    43.     }
    44.  
    45.     #endregion
    46. }
    47.