栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

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

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

使用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:Users123DesktopSourcePathAa.mp3";
        string pathB = @"C:Users123DesktopSourcePathBb.mp3";
        string pathC = @"C:Users123DesktopSourcePathCc.mp3";
        
        //将路径添加到路径的list,为对象赋值
        pathInformation.FilePathList.Add(pathA);
        pathInformation.FilePathList.Add(pathB);
        pathInformation.FilePathList.Add(pathC);
        
        //为对象赋值
        pathInformation.TargerPath = @"C:Users92126DesktopFilePathtest.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 filePathList = new List();//需要读取的文件路径列表

        private string targerPath;//目标路径

        public string TargerPath { get => targerPath; set => targerPath = value; }
        public List 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(args[0]);//将传过来的参数进行转换。
			 
            pathList = pathInformation.FilePathList;
            TempPath = @pathInformation.TargerPath;


            List bytesList = new List();//读取到的文件的字节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转换成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;
                }
            }
        }


        /// 
        /// 将多个byte[] 合并成一个
        /// 
        /// byte[]  列表
        /// 合并之后的 byts [] 
        private static byte[] MergeBytes(List 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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900178.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号