這個(gè)給大家?guī)?lái)的是Unity與阿里云服務(wù)器數(shù)據(jù)的上傳和下載,如有不正之處,還望指出!
1.導(dǎo)入Ailiyun.dll的SDK(阿里云官網(wǎng)的開(kāi)發(fā)者指南)
2.引入相應(yīng)的命名空間
3.上傳字符串到云服務(wù)器
A:在阿里云服務(wù)器的對(duì)象存儲(chǔ)OSS,新建Bucket,這個(gè)Bucket就可以做為上傳文件的存儲(chǔ)空間
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Aliyun.OSS;
using Aliyun.OSS.Common;
using System.Text;
using System.IO;
public class PutObejct : MonoBehaviour {
private OssClient clinet;
public static PutObejct Instance;
void Awake ()?
{
Instance = this;
clinet = new OssClient(Config.EndPoint,Config.AccessKeyid,Config.AccessKeySecret);
}
B:新建一個(gè)Config的腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Config? {
? ? public const string AccessKeyid = "LTAI5IcR2fyUo734";
? ? public const string AccessKeySecret = "k3lPVxasn5cBzWRLY6PGL4Amzf0m01";
? ? public const string EndPoint = "oss-cn-shenzhen.aliyuncs.com";
? ? public const string Bucket = "zzrto";
? }
D:實(shí)現(xiàn)字符串上傳
//字符串上傳
public void PutObjWithStr(string fileName, string text)
{
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? byte[] b = Encoding.UTF8.GetBytes(text);
? ? ? ? ? ? using (Stream stream = new MemoryStream(b))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? clinet.PutObject(Config.Bucket, fileName, stream);
? ? ? ? ? ? ? ? Debug.Log("字符串上傳成功" + text);
? ? ? ? ? ? }
?}
?catch (OssException e)
{
? ? ? ? ? ? Debug.Log("字符串上傳錯(cuò)誤:" + e);
? ? ? ? }
? ? ? ? catch (System.Exception e)
? ? ? ? {
? ? ? ? ? ? Debug.Log("字符串上傳錯(cuò)誤:" + e);
? ? ? ? }
}
E:實(shí)現(xiàn)文件上傳(txt,zip各種格式都可以)
public void PutObjFormLocal(string localPath,string fileName)
{
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? clinet.PutObject(Config.Bucket, fileName, localPath);
? ? ? ? ? ? Debug.Log("本地上傳成功" + localPath);
? ? ? ? }
? ? ? ? catch(OssException e)
? ? ? ? {
? ? ? ? ? ? Debug.Log("本地上傳:" + e.Message);
? ? ? ? }
? ? ? ? catch (System.Exception e)
? ? ? ?{
? ? ? ? ? ? Debug.Log("本地上傳:" + e.Message);
? ? ? ? }
}
F:將FutObject設(shè)置為單例模式,創(chuàng)建一個(gè)新的Test腳本,當(dāng)按下A鍵上傳文件
//本地字符串上傳測(cè)試
void Update ()
?{
? ?if(Input.GetKeyDown(KeyCode.Space))
? ? {
? ? ? ? ? ? //字符串上傳測(cè)試
? ? ? ? ? ? //? PutObejct.Instance.PutObjWithStr("Text.txt", "EveryOne,My Name is Zzr");
? ? ? ? ? ? //本地字符串上傳測(cè)試
? ? ? ? ? ? PutObejct.Instance.PutObjFormLocal(@"C:\Users\Administrator\Desktop\新建文本文檔.txt", "zzr.txt");
? ? ? ? ? //模型上傳
? ? ? ? ? PutObejct.Instance.PutObjFormLocal(@"E:\Unity3D游戲案例\SIKI學(xué)院案例\中級(jí)案例\AliyunOSS\Assets\AssetBundles\Capsule\capsule.zzr", "capsule.zzr");
? ? ? ? ? PutObejct.Instance.PutObjFormLocal(@"E:\Unity3D游戲案例\SIKI學(xué)院案例\中級(jí)案例\AliyunOSS\Assets\AssetBundles\Cube\cube.zzr", "cube.zzr");
? ? ? ? ? ? PutObejct.Instance.PutObjFormLocal(@"E:\Unity3D游戲案例\SIKI學(xué)院案例\中級(jí)案例\AliyunOSS\Assets\AssetBundles\Cylinder\cylinder.zzr", "cylinder.zzr");
? ? ? }
? ?}
}
本地文件上傳時(shí),需要傳遞的參數(shù)是文件的路徑,文件可以自己創(chuàng)建,路徑的前面要加轉(zhuǎn)義符@,第二個(gè)參數(shù)是xxx.txt類型的字符串,可以隨意定義
注:字符串上傳失敗要將Build Setting里面的Player setting的.Net版本改為4.6
模型上傳:需要填打包后模型所在的路徑
以上就是unity結(jié)合阿里云字符串?dāng)?shù)據(jù)上傳和本地文件上傳的代碼,接下來(lái)我會(huì)發(fā)布如果通過(guò)網(wǎng)絡(luò)加載模型!