본문 바로가기
개발/Firebase

유니티 C# 파이어베이스 클라우드 스토리지 Cloud Storage 간단 사용법

by SPNK 2022. 8. 17.
반응형

파이어베이스 SDK 다운로드

 

Unity 프로젝트에 Firebase 추가  |  Unity용 Firebase

의견 보내기 Unity 프로젝트에 Firebase 추가 Firebase Unity SDK를 활용하여 Unity 게임을 업그레이드 해보세요. Firebase를 Unity 프로젝트에 연결하는 것이 얼마나 간편한지 보여드리기 위해 Google은 MechaHamst

firebase.google.com

 

 

압축 푼 뒤

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);
    }

}
반응형

댓글