遇到一个需求是在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