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

简介:

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

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


1,C#上传工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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脚本,

1
UploadAssetBundles.script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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#程序内传参调用实现上传功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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最后的

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

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



关于WinSCP命令行调用说明可以查看官网https://winscp.net/eng/docs/scripting

WinSCP可以在这下载:http://down.51cto.com/data/2304295

本文转自   zl1030   51CTO博客,原文链接:http://blog.51cto.com/zl1030/1920820

相关文章
|
18天前
|
运维 安全 Linux
试试Linux设备命令行运维工具——Wowkey
WowKey 是一款专为 Linux 设备设计的命令行运维工具,提供自动化、批量化、标准化的运维解决方案。它简单易用、高效集成且无依赖,仅需 WIS 指令剧本文件、APT 账号密码文件和 wowkey 命令即可操作。通过分离鉴权与执行过程,WowKey 让运维人员专注于决策,摆脱繁琐的交互与执行工作,大幅提升运维效率与质量。无论是健康检查、数据采集还是配置更新,WowKey 都能助您轻松应对大规模设备运维挑战。立即从官方资源了解更多信息:https://atsight.top/training。
|
18天前
|
数据采集 运维 安全
Linux设备命令行运维工具WowKey问答
WowKey 是一款用于 Linux 设备运维的工具,可通过命令行手动或自动执行指令剧本,实现批量、标准化操作,如健康检查、数据采集、配置更新等。它简单易用,只需编写 WIS 指令剧本和 APT 帐号密码表文件,学习成本极低。支持不同流派的 Linux 系统,如 RHEL、Debian、SUSE 等,只要使用通用 Shell 命令即可通吃Linux设备。
|
6月前
|
监控 数据可视化 Ubuntu
|
2月前
|
Ubuntu Shell Linux
Linux命令行解释器的模拟实现
Linux命令行解释器的模拟实现
|
2月前
|
缓存 图形学
Unity C#for和foreach效率比较
该代码对比了三种遍历 `List<int>` 的方式的性能:使用缓存 `Count` 的 `for` 循环、每次访问 `list.Count` 的 `for` 循环以及 `foreach` 循环。通过 `Stopwatch` 测量每次遍历 300 万个元素所花费的时间,并输出结果。测试可在 Unity 环境中运行,按下空格键触发。结果显示,缓存 `Count` 的 `for` 循环性能最优,`foreach` 次之,而每次都访问 `list.Count` 的 `for` 循环最慢。
|
5月前
|
设计模式 API C#
C# 一分钟浅谈:GraphQL 服务器端实现
本文通过C#语言从零开始构建一个简单的GraphQL服务器端实现,介绍了环境准备、项目创建、定义Schema、配置GraphQL等步骤。同时,探讨了常见问题如数据源问题、类型定义不一致、性能问题和权限控制,提供了相应的解决方法。希望帮助读者更好地理解和应用GraphQL。
74 3
|
5月前
|
Ubuntu Linux 网络安全
linux系统ubuntu中在命令行中打开图形界面的文件夹
在Ubuntu系统中,通过命令行打开图形界面的文件夹是一个高效且实用的操作。无论是使用Nautilus、Dolphin还是Thunar,都可以根据具体桌面环境选择合适的文件管理器。通过上述命令和方法,可以简化日常工作,提高效率。同时,解决权限问题和图形界面问题也能确保操作的顺利进行。掌握这些技巧,可以使Linux操作更加便捷和灵活。
158 3
|
5月前
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
207 4
|
5月前
|
Linux 数据安全/隐私保护
适用于 Linux 的最佳命令行下载加速器
适用于 Linux 的最佳命令行下载加速器
174 3
|
6月前
SecureCRT 通过xftp命令行上传文件到服务器上
SecureCRT 通过xftp命令行上传文件到服务器上
217 1