unity连接linux服务器,Unity3D下用C#通过WinSCP命令行方式给Linux服务器SCP传文件

遇到一个需求是在Unity3D做编辑器工具时需要把生成的AssetBundle包上传到资源服务器,资源服务器用的Linux。

实现分为三部分:1,C#上传工具类;2,WinSCP脚本;3,传参调用使用上传功能。

1,C#上传工具类

using UnityEngine;

using System.Collections;

using System.IO;

using System;

using System.Diagnostics;

public class UploadHelper

{

public static void callUploadProcess(string arguments)

{

string winScpPath = Directory.GetParent(Application.dataPath) + "/WinSCP/WinSCP.exe";

try

{

Process proc = null;

bool redirectOutput = false;

proc = new Process();

proc.StartInfo.FileName = winScpPath;

proc.StartInfo.Arguments = arguments;

if (redirectOutput)

{

proc.StartInfo.UseShellExecute = false;

proc.StartInfo.RedirectStandardOutput = true;

proc.StartInfo.CreateNoWindow = true;

}

else

{

proc.StartInfo.CreateNoWindow = false;

}

proc.Start();

if (redirectOutput)

{

//重定向,显示上传工具输出

StreamReader sr = proc.StandardOutput;

while (!sr.EndOfStream)

{

string s = sr.ReadLine();

UnityEngine.Debug.Log(s);

}

}

proc.WaitForExit();

if (proc.ExitCode == 0)

{

UnityEngine.Debug.LogFormat("[{0}] 上传完毕!", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

}

else

{

UnityEngine.Debug.LogFormat("[{0}] 上传失败! ExitCode:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), proc.ExitCode);

}

}

catch (Exception ex)

{

UnityEngine.Debug.LogError(String.Format("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()));

}

}

}

2,WinSCP脚本,

UploadAssetBundles.script

option echo off

option batch on

option confirm off

open scp://"%1%":"%2%"@"%3%"

option transfer binary

synchronize remote -mirror -delete %4% %5%

chmod 755 %5%/*

close

exit

3,C#程序内传参调用实现上传功能

private static void UploadToServer(BuildTarget target)

{

string remoteFolder = null;//远程服务器资源所在路径

switch (target)

{

case BuildTarget.StandaloneWindows64:

remoteFolder = "###/AssetBundle/PC";

break;

case BuildTarget.Android:

remoteFolder = "###/AssetBundle/Android";

break;

case BuildTarget.iOS:

remoteFolder = "###/AssetBundle/iOS";

break;

default:

return;

}

string uploaderPath = Application.dataPath + "/Uploader";

//脚本路径

string scriptPath = uploaderPath + "/UploadAssetBundles.script";

//Log路径

string logPath = Directory.GetParent(Application.dataPath) + "/upload.log";

string localFolder = Application.dataPath + "/ABs";//本地资源目录路径

string[] param ={

"test",//远程服务器登录用户名

"test1234",//远程服务器登录密码

"192.168.1.xxx",//远程服务器IP或域名

localFolder,

remoteFolder,

scriptPath,

logPath,

};

string arguments = String.Format("/console /log={6} /script={5} /parameter \"{0}\" \"{1}\" \"{2}\" \"{3}\" \"{4}\"", param);

UploadHelper.callUploadProcess(arguments);

}

arguments最后的

/parameter \"{0}\" \"{1}\" \"{2}\" \"{3}\" \"{4}\""

是传给WinSCP脚本的参数,对应脚本里的“%1%”到“%5%”.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值