由于项目的需要,用FileZilla server做了个本地的FTP服务器,在Unity端对打包好的场景进行下载,并自动加载该场景,实现了程序和场景的分离,让APK的文件小了很多。原代码如下:(因为是在网上找的,并加以改动,所以有一部分代码是没用到的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
// 添加命令空间
using System.Net;
using System.Text;
public class GetSceneFromFTP : MonoBehaviour {
private const int ftpport = 21;
private string ftpUristring = null;
private NetworkCredential networkCredential;
public string ftppath;
private string currentDir = "/";
public string downname;
public GameObject LoginInformation;//显示是否登录成功
public GameObject SceneListMenu;
void Start()
{
}
#region 与服务器的交互
//创建FTP连接
public FtpWebRequest CreateFtpWebRequest(string uri, string requestMethod)
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
request.Credentials = networkCredential;
request.KeepAlive = true;
request.UseBinary = true;
request.Method = requestMethod;
return request;
}
// 获取服务器返回的响应体
public FtpWebResponse GetFtpResponse(FtpWebRequest request)
{
FtpWebResponse response = null;
try
{
response = (FtpWebResponse)request.GetResponse();
return response;
}
catch (WebException ex)
{
return null;
}
}
#endregion
// 登录服务器事件
public void btnlogin_Click()
{
ftpUristring = "ftp://" + ftppath;
networkCredential = new NetworkCredential("admin",