使用Unity向其他程序传递参数

本文介绍如何使用Unity向其他程序传递复杂参数,包括通过序列化对象为JSON字符串,并利用Process.Start()方法启动外部程序的同时传递参数。同时展示了如何在接收端解析这些参数并执行相应操作。

使用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("文件写入读取完成");
    }

///

/// 文件读取 /// /// /// private static byte[] FileContent(string filePath) { if (!File.Exists(filePath)) { Console.WriteLine("文件不存在"); return null; }

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中高级学习者。为了更加深入的刨析各个语法的本质,我们采用反编译解读IL中间语言的方式,来解构语法重点与难点。 中级篇内容主要讲解: .Net 框架、里氏替换原则(LSP)、类的属性极其本质特性、IS ,AS 关键字、字符串的“驻留性” 原理、深入解析Equals() 原理、枚举类型、自定义集合、深入解析动态集合特性与内部原理、泛型集合、泛型约束、初级委托与事件讲解等。 进阶篇是在中级篇的基础之上,进一步研究与讲解关于IO操作、序列化、正则表达式、系统委托(Action、Function、Predicate等)、反射原理与特性、Linq查询表达式、多线程、线程池、任务、Socket套接字编程(Tcp与UDP协议),以及最后使用Unity开发具备实战价值的通讯聊天程序等。C#“进阶篇”教学详细说明如下:1: IO操作与序列化      学习文件、目录、二进制文件、文本文件的读取与写入底层原理。学习文件序列化与反序列化技能。2: 正则表达式      学习正则表达式的强大作用与常用原字符的含义与应用场景。3: 深入委托与事件      学习Action、Func、Predicate 系统内置委托类型,已经适用场合。学习匿名方法、Lambda表达式。深入解析委托与事件的区别。4: 反射与特性      学习反射的概念与动态调用的重要应用价值,以及Type、Assembley核心类等,最后讲解“特性”技术。5: Linq 查询表达式     学习Linq 查询表达式对于“对象集合”(支持IEnumberable 或IEnumberable) 以及SQL数据库、XML文档方面的强大查询功能。    6: 多线程     学习多线程以及线程传参、线程取得返回数值技术,前台与后台线程、线程的同步、线程池、任务等技术。   7: Socket套接字通讯     学习Socket套接字通讯中,Tcp与UPD通讯协议的不同应用场景,以及各自的演示示例,最后用Unity开发一款实用性的聊天通讯工具。温习提示:           本C# for Unity 使用Virtual Studio2012,以及Unity5.2 进行开发与讲解。(学员使用更高版本,对学习没有任何影响)。      A:《C# For Unity系列之入门篇》https://edu.youkuaiyun.com/course/detail/4560B:《C# For Unity系列之基础篇》https://edu.youkuaiyun.com/course/detail/4595C: 《C# For Unity系列之中级篇》https://edu.youkuaiyun.com/course/detail/24422
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值