반응형
파이어베이스 SDK 다운로드
압축 푼 뒤
dotnet4 / FirebaseStorage.unitypackage 설치
- 코드 작성
using Firebase;
using Firebase.Extensions;
using Firebase.Storage;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
public class FireBaseManager : MonoBehaviour
{
FirebaseStorage storage;
StorageReference storageRef;
void Start() //참조 만들기
{
storage = FirebaseStorage.DefaultInstance;
storageRef = storage.RootReference;
}
void UploadFiles() //파이어베이스 업로드
{
var customBytes = new byte[] { };
// Create a reference to the file you want to upload
StorageReference riversRef = storageRef.Child("images/rivers.jpg");
// Upload the file to the path "images/rivers.jpg"
riversRef.PutBytesAsync(customBytes)
.ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.Log(task.Exception.ToString());
// Uh-oh, an error occurred!
}
else
{
// Metadata contains file metadata such as size, content-type, and md5hash.
StorageMetadata metadata = task.Result;
string md5Hash = metadata.Md5Hash;
Debug.Log("Finished uploading...");
Debug.Log("md5 hash = " + md5Hash);
}
});
}
void LocalUploadFiles() //로컬 파일 업로드
{
// File located on disk
string localFile = "...";
// Create a reference to the file you want to upload
StorageReference riversRef = storageRef.Child("images/rivers.jpg");
// Upload the file to the path "images/rivers.jpg"
riversRef.PutFileAsync(localFile)
.ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.Log(task.Exception.ToString());
// Uh-oh, an error occurred!
}
else
{
// Metadata contains file metadata such as size, content-type, and download URL.
StorageMetadata metadata = task.Result;
string md5Hash = metadata.Md5Hash;
Debug.Log("Finished uploading...");
Debug.Log("md5 hash = " + md5Hash);
}
});
}
void CheckingUpload() //업로드 상황 체크
{
// Start uploading a file
var task = storageRef.Child("images/mountains.jpg")
.PutFileAsync("파일 위치", null,
new StorageProgress<UploadState>(state =>
{
// called periodically during the upload
Debug.Log(string.Format("Progress: {0} of {1} bytes transferred.",
state.BytesTransferred, state.TotalByteCount));
}), CancellationToken.None, null);
task.ContinueWithOnMainThread(resultTask =>
{
if (!resultTask.IsFaulted && !resultTask.IsCanceled)
{
Debug.Log("Upload finished.");
}
});
}
void DownloadFile() //다운로드 파일
{
// Download via a Stream
reference.GetStreamAsync(stream =>
{
// Do something with the stream here.
//
// This code runs on a background thread which reduces the impact
// to your framerate.
//
// If you want to do something on the main thread, you can do that in the
// progress eventhandler (second argument) or ContinueWith to execute it
// at task completion.
}, null, CancellationToken.None);
}
}
반응형