使用Unity向其他程序传递参数
暂时没有什么事情做来玩啦。
unity的代码
只需要将脚本挂载到一个GameObject上即可。
``` using System.Collections; using System.Collections.Generic; using System.Diagnostics; using UnityEngine; using Debug = UnityEngine.Debug;
public class PathObj : MonoBehaviour { private string exePath = @" c"// "; //需要启动的程序路径启动程序的路径 PathInformation pathInformation = new PathInformation();
private void Awake()
{
Debug.Log("开始喽");
}
private void OnEnable()
{
//路径path的list,为对象赋值
string pathA = @"C:\Users\123\Desktop\SourcePath\Aa.mp3";
string pathB = @"C:\Users\123\Desktop\SourcePath\Bb.mp3";
string pathC = @"C:\Users\123\Desktop\SourcePath\Cc.mp3";
//将路径添加到路径的list,为对象赋值
pathInformation.FilePathList.Add(pathA);
pathInformation.FilePathList.Add(pathB);
pathInformation.FilePathList.Add(pathC);
//为对象赋值
pathInformation.TargerPath = @"C:\Users\92126\Desktop\FilePath\test.mp3";
//pathInformation转换成json,然后在另外一个程序将json解析出来
string json = Newtonsoft.Json.JsonConvert.SerializeObject(pathInformation);
Process pro = Process.Start(@exePath, json);//打开程序B,并传递参数。
Debug.Log("结束");
}
} ```
使用到的Process.Start()的文档,》文档链接 挂载上面这个东西。 下面是需要传递的参数对象。
``` using System; using System.Collections.Generic;
//需要传给另外一个程序的的参数的对象,会将这个对象给另外一个程序
public class PathInformation//路径信息
{
private List<string> filePathList = new List<string>();//需要读取的文件路径列表
private string targerPath;//目标路径
public string TargerPath { get => targerPath; set => targerPath = value; }
public List<string> FilePathList { get => filePathList; set => filePathList = value; }
}
```
另外一个exe的代码
在另外一边,使用当exe启动的时候会获取到信息。
``` using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.Json;
namespace AudioSynthesis { class Program { static string TempPath;//需要从外面传过来 static List pathList = new List ();//路径路径信息
public static void Main(string[] args)//这里的string[] args是传过来的参数
{
Console.WriteLine("Hello World!");
PathInformation pathInformation = JsonSerializer.Deserialize<PathInformation>(args[0]);//将传过来的参数进行转换。
pathList = pathInformation.FilePathList;
TempPath = @pathInformation.TargerPath;
List<byte[]> bytesList = new List<byte[]>();//读取到的文件的字节list
FileStream fileStream = new FileStream(TempPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);//创建文件读取和写入流
//根据路径,获取到信息,并将信息加入到bytesList,准备转化为bytes
for (int i = 0; i < pathList.Count; i++)
{
bytesList.Add(FileContent(pathList[i]));
}
var bytes = MergeBytes(bytesList);//将list<string>转换成bytes
fileStream.Write(bytes, 0, bytes.Length);//向文件中写入字节数组
fileStream.Flush();//刷新缓冲区
fileStream.Close();//关闭流
Console.WriteLine("文件写入读取完成");
}
///
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
try
{
byte[] buffur = new byte[fs.Length];
fs.Read(buffur, 0, (int)fs.Length);
fs.Flush();//刷新缓冲区
fs.Close();
return buffur;
}
catch (Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 将多个byte[] 合并成一个
/// </summary>
/// <param name="bytesList">byte[] 列表</param>
/// <returns>合并之后的 byts [] </returns>
private static byte[] MergeBytes(List<byte[]> bytesList)
{
int bytesLength = 0;//lbytesList中每一行byte元素的总长度
for (int i = 0; i < bytesList.Count; i++)
{
bytesLength = bytesLength + bytesList[i].Length;
}
int k = 0;
byte[] mergeBytes = new byte[bytesLength];//用于存储合并之后的 byts[]
for (int i = 0; i < bytesList.Count; i++)
{
for (int j = 0; j < bytesList[i].Length; j++)
{
mergeBytes[k] = (bytesList[i])[j];
k = k + 1;
}
}
return mergeBytes;
}
}
} ```
不懂评论 私信enjoy
pic from a SHX.
本文介绍如何使用Unity向其他程序传递复杂参数,包括通过序列化对象为JSON字符串,并利用Process.Start()方法启动外部程序的同时传递参数。同时展示了如何在接收端解析这些参数并执行相应操作。
1660

被折叠的 条评论
为什么被折叠?



