1、新建C#的控制台应用:
生成的exe:
2、unity中调用:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class AssetsImporter : AssetPostprocessor
{
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
TableAssets.ExecuteTable2Bytes("hello world");
}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class TableAssets
{
public static void ExecuteTable2Bytes(string args)
{
DirectoryInfo dir = new DirectoryInfo("Assets");
System.Diagnostics.Process exe = new System.Diagnostics.Process();
exe.StartInfo.FileName = @"E:\OGL5\ProtoGen\Table2Bytes\bin\Debug\netcoreapp3.1\Table2Bytes.exe";
exe.StartInfo.Arguments = args;
exe.StartInfo.CreateNoWindow = true;
exe.StartInfo.UseShellExecute = false;
exe.StartInfo.RedirectStandardOutput = true;
exe.StartInfo.StandardOutputEncoding = System.Text.Encoding.Default;
exe.Start();
string output = exe.StandardOutput.ReadToEnd();
exe.WaitForExit();
Debug.Log(output);
}
}
unity的控制台输出:
这里在C#的控制台应用中,使用Console.WriteLine(“Hello World!”);
输出的log,会在unity的console中输出。这个是因为在 exe.StartInfo.RedirectStandardOutput = true;设置,所以能看到。