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

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