目录
一.目的
1.想知道:如何:实现本地(字符串/图片等)上传+有进度条+上传都在主线程上传
二.参考
1.阿里云SMS&OSS、腾讯云COS接入Unity(Unity2018.1.0)
2.阿里云服务器+Unity:OSS:四:成功:实现字符串(非本地+临时的)上传
三.注意
1.Unity切换.net库版本:切换到是4.x,否则上传文件失败!
1.将文件放在项目内+Application.streamingAssetsPath?实现了上传,不能使用绝对路径,因为看视频上面老师实现了绝对路径成功,也认为自己能实现;下次注意了。
1.版本
1.委托回调事件一定要在主线程中调用,免得在分线程中调用会报错
四.操作:1:成功
?1. 代码:MyALiYunConfig
1.代码 MyPutObject
1.代码:MyALiYunTest
1.Unity设置
1.1 代码设置
1.1 Slider设置
1.运行效果:成功:
1.1 上传临时的字符串:成功
1.1 上传本地图片:不使用线程:成功
1.1 上传本地图片:使用线程上传:成功
1.1上传本地图片:使用进度条:完成
一.目的
1.想知道:如何:实现本地(字符串/图片等)上传+有进度条+上传都在主线程上传
二.参考
1.阿里云SMS&OSS、腾讯云COS接入Unity(Unity2018.1.0)
http://www.sikiedu.com/course/303
- good:我参考这个视频学习的。
2.阿里云服务器+Unity:OSS:四:成功:实现字符串(非本地+临时的)上传
https://mp.csdn.net/mp_blog/creation/editor/120100814
- good:我之前写的;成功:实现字符串(非本地+临时的)上传
- good:这篇文章准备实现本地字符串+本地图片等资源上传
三.注意
1.Unity切换.net库版本:切换到是4.x,否则上传文件失败!
1.将文件放在项目内+Application.streamingAssetsPath?实现了上传,不能使用绝对路径,因为看视频上面老师实现了绝对路径成功,也认为自己能实现;下次注意了。
1.版本
- windows10 64 为
- Unity2017.4.35C1
- aliyun_oss_dotnet_sdk_2_8_0.zip
1.委托回调事件一定要在主线程中调用,免得在分线程中调用会报错
四.操作:1:成功
?1. 代码:MyALiYunConfig
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Ali cloud server inside some configuration; 功能:阿里云服务器里面的一些配置]
/// </summary>
public class MyALiYunConfig
{
/// <summary>字符串:阿里云的KeyID</summary>
public const string AccessKeyId = "去阿里云官网查";
/// <summary>字符串:阿里云的KeySecret</summary>
public const string AccessKeySecret = "去阿里云官网查";
/// <summary>字符串:阿里云的 EndPoint</summary>
public const string EndPoint = "去阿里云官网查";
/// <summary>字符串:阿里云的 Bucket名字</summary>
public const string Bucket = "去阿里云官网查";
}
?
?
1.代码 MyPutObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Aliyun.OSS;
using System.Text;
using System.IO;
using Aliyun.OSS.Common;
using System;
using System.Threading;
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Upload files to Ali Cloud server; 功能:上传文件到阿里云服务器]
/// </summary>
public class MyPutObject : MonoBehaviour
{
/// <summary>单例:MyPutObject</summary>
public static MyPutObject Instance;
/// <summary>OssClient:oss客户端</summary>
private OssClient client;
/// <summary>string:文件名</summary>
private string str_fileName;
/// <summary>string:文件路劲</summary>
private string str_fileLocalPath;
/// <summary>Thread:上传本地文件的线程</summary>
private Thread thread;
/// <summary>Action:上传成功的委托</summary>
private Action aPutSuccessCallBack;
/// <summary>float:上传的进度</summary>
private float fPutProcess;
/// <summary>Action:委托事件:上传进度条的回调事件</summary>
private Action<float> aPutWithProcessCallBack=null;
/// <summary>bool:是否上传成功</summary>
private bool bIsPutSuccess=false;
private void Awake()
{
Instance = this;//单例赋值
client = new OssClient(MyALiYunConfig.EndPoint, MyALiYunConfig.AccessKeyId, MyALiYunConfig.AccessKeySecret);//创建客户端对象
}
private void FixedUpdate()
{
//上传事件回调函数需要放在主线程里面,否则会报错
if (aPutWithProcessCallBack!=null)
{
aPutWithProcessCallBack(fPutProcess);
if (fPutProcess==1)//代表上传成功
{
aPutWithProcessCallBack = null;
fPutProcess = 0;
}
}
//如果上传成功
if (bIsPutSuccess)
{
aPutSuccessCallBack();//这样做的方法是将委托全部放在主线程中,避免在分线程调用报错
bIsPutSuccess = false;
}
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Upload string to Ali cloud server; 功能:上传 字符串 到阿里云服务器]
/// </summary>
/// <param name="_strFileName"></param>
/// <param name="_strText"></param>
public void PutObjWithStr(string _strFileName, string _strText)
{
try
{
byte[] b = Encoding.UTF8.GetBytes(_strText);
//使用using:让其结束后自动删除
using (Stream stream = new MemoryStream(b))
{
client.PutObject(MyALiYunConfig.Bucket, _strFileName, stream);//会一直执行,指导将所有数据上传成功
Debug.Log("字符串上传成功:" + _strText);
}
}
catch (OssException e)
{
Debug.LogError("字符串上传错误:" + e);
}
catch (System.Exception e)
{
Debug.LogError("字符串上传错误:" + e);
}
}
/// <summary>
/// [Author:xzy][Time:20210905]
/// [Function:Upload local files to Ali cloud server by thread,Avoid large file upload freezes; 功能:通过线程上传本地文件到阿里云服务器,避免大文件上传卡死问题
/// </summary>
public void PutObjectFromLocalThread(Action _action,string _strLocalPath,string _strFileNames)
{
this.str_fileLocalPath = _strLocalPath;
this.str_fileName = _strFileNames;
aPutSuccessCallBack= _action;//委托事件赋值
thread = new Thread(PutObjFromLocal);//新建线程
thread.Start();//开启线程
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Upload local files to Ali cloud server; 功能:上传本地文件到阿里云服务器
/// [注意:①因为线程无法添加参数,所以将新建一个函数PutObjFromLocal,将参数变为全局变量]
/// </summary>
/// <param name="_strLocalPath">文件本地绝对值路径</param>
/// <param name="_strFileName">文件名字</param>
public void PutObjFromLocal(string _strLocalPath, string _strFileName)
{
try
{
client.PutObject(MyALiYunConfig.Bucket, _strFileName, _strLocalPath);
Debug.Log("本地上传成功:" + _strFileName);
}
catch (OssException e)
{
Debug.LogError("本地上传失败:" + e.Message);
}
catch (System.Exception e)
{
Debug.LogError("本地上传失败:" + e.Message);
}
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Upload local files to Ali cloud server; 功能:上传本地文件到阿里云服务器]
/// </summary>
public void PutObjFromLocal()
{
try
{
client.PutObject(MyALiYunConfig.Bucket, str_fileName, str_fileLocalPath);
Debug.Log("本地上传成功:" + str_fileName);
//aPutSuccessCallBack();//上传成功调用一下委托,delegate(){ },其中()里为参数,无参时可省略括号,{ }里面为具体实现的代码
bIsPutSuccess = true;
}
catch (OssException e)
{
Debug.LogError("本地上传失败:" + e.Message);
}
catch (System.Exception e)
{
Debug.LogError("本地上传失败:" + e.Message);
}
finally
{
thread.Abort();//关闭线程
}
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:There are progress bar uploadss; 功能:有进度条的上传]
/// </summary>
/// <param name="_action">委托事件</param>
/// <param name="_localPath">文件的本地相对路径</param>
/// <param name="_fileName">文件名</param>
public void PutObjectWithProcess(Action<float> _action,string _localPath,string _fileName)
{
aPutWithProcessCallBack = _action;
this.str_fileLocalPath = _localPath;
this.str_fileName = _fileName;
thread = new Thread(PutObjectProcess);
thread.Start();
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Progress of uploading local files; 功能:上传本地文件到阿里云服务器的进度]
/// </summary>
/// <param name="_strLocal">本地文件的路径</param>
/// <param name="_strFileName">文件名</param>
//public void PutObjectProcess(string _strLocal,string _strFileName)
public void PutObjectProcess()
{
try
{
using (var fs=File.Open(str_fileLocalPath,FileMode.Open))//文件读取出来
{
PutObjectRequest putObjectRequest = new PutObjectRequest(MyALiYunConfig.Bucket, str_fileName, fs);//上传文件请求的对象
putObjectRequest.StreamTransferProgress += PutStreamProcess;//上传事件添加委托
client.PutObject(putObjectRequest);
Debug.Log("带有进度的上传成功!");
}
}
catch(OssException e)
{
Debug.LogError("带有进度条的上传错误:"+e);
}
catch (Exception e)
{
Debug.LogError("带有进度条的上传错误"+e);
}
finally
{
thread.Abort();
}
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:The flow progress of uploading files; 功能:上传文件的流进度]
/// </summary>
/// <param name="_objSender"></param>
/// <param name="_args"></param>
private void PutStreamProcess(object _objSender,StreamTransferProgressArgs _args)
{
fPutProcess = (_args.TransferredBytes * 100 / _args.TotalBytes) / 100.0f; //上传的进度=(上传字节数*100/文件总的字节数)/100.0f
}
}
1.代码:MyALiYunTest
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Ali Cloud function test; 功能: 阿里云功能测试]
/// </summary>
public class MyALiYunTest : MonoBehaviour
{
/// <summary>Slider:上传进度条</summary>
public Slider slider;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.A))
{
//测试:上传字符串
MyPutObject.Instance.PutObjWithStr("test.txt", "各位帅哥你们好,我是xzy!");
}
else if (Input.GetKeyUp(KeyCode.B))
{
string strPath;
//上传项目内图片+使用Application.streamingAssetsPath:成功
strPath = Application.streamingAssetsPath + "/" + "MyScreenShot" + "/" + "0.png";
MyPutObject.Instance.PutObjFromLocal(strPath, "0.png");
}
else if(Input.GetKeyUp(KeyCode.C))
{
string strPath;
//上传项目内图片+使用Application.streamingAssetsPath:成功
strPath = Application.streamingAssetsPath + "/" + "MyScreenShot" + "/" + "0.png";
//开启线程来上传文件,避免卡顿
MyPutObject.Instance.PutObjectFromLocalThread(PutSuccessCallBack, strPath, "0.png");
}
else if(Input.GetKeyUp(KeyCode.D))
{
string strPath;
//上传项目内图片+使用Application.streamingAssetsPath:成功
strPath = Application.streamingAssetsPath + "/" + "MyScreenShot" + "/" + "0.png";
//开启线程来上传文件,避免卡顿
MyPutObject.Instance.PutObjectWithProcess(PutWithProcessCallBack, strPath, "0.png");
}
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Callback event of uploading success; 功能:上传成功回调事件]
/// </summary>
private void PutSuccessCallBack()
{
Debug.Log("本地文件上传成功!");
}
/// <summary>
/// [Author:xzy][Time:20210905]
/// [Function:There is a progress bar up call event; 功能:有进度条的上传回调事件]
/// </summary>
private void PutWithProcessCallBack(float _process)
{
Debug.Log("上传进度:" + _process);
if (slider == null)
return;
else
slider.value = _process;
}
}
1.Unity设置
1.1 代码设置
- 代码放在空物体为ScriptsHold上面
- ?
?
1.1 Slider设置
- 在UI中新建Slider作为进度条
- 将大的可拖动的物体隐藏掉
- Fill变为绿色
?
?
1.运行效果:成功:
1.1 上传临时的字符串:成功
?
1.1 上传本地图片:不使用线程:成功
?
1.1 上传本地图片:使用线程上传:成功
?
1.1上传本地图片:使用进度条:完成
?
|