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%”.



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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章