Unity Supabase Bucket

Roh·2023년 11월 24일
0

Supabase

목록 보기
2/4
post-thumbnail

Bucket에 .jpg 파일을 올려보자.

Storage 아이콘 클릭 후 New bucket 버튼을 클릭 후 bucket을 만들어주자.

그리고 Policy 클릭후 For full customization을 클릭해주자.

일단 Select, Insert, Update, Delete 다 클릭후 나중에 필요 없는 것들은 Uncheck해주기로 하자.

자 이제 기본적인 세팅은 끝났고 코드를 작성해보자.

코드는 아래와 같이 작성했다. Unity Setting글은 이전 글들을 참고해주세요!

using Postgrest.Models; // Postgrest.Models.BaseModels
using Supabase;
using Supabase.Storage;
using System;
using System.IO;
using UnityEngine;
using static UnityEngine.UI.Image;
using Client = Supabase.Client;
using FileOptions = Supabase.Storage.FileOptions;

namespace com.example
{
    public class user_img : BaseModel
    {
        public string user_id { get; set; }
        public string original { get; set; }

    }

    public class user_size : BaseModel  //
    {
        public string user_id { get; set; }
        public int height { get; set; }
        public int width { get; set; }
        public int s_sleeve { get; set; }
        public int l_sleeve { get; set; }

        public override bool Equals(object obj)
        {
            return obj is user_size productInstance &&
                    height == productInstance.height;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(height);
        }

    }

    public class SupaManager : MonoBehaviour
    {
        public static SupaManager Instance { get; private set; } // singleton
        public SupabaseSettings SupabaseSettings = null!;
        private Client supabase;

        private async void Awake()
        {
            // 싱글톤 인스턴스 초기화
            if (Instance == null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject); // 싱글톤 오브젝트 파괴 방지
            }
            else
            {
                Destroy(gameObject); // 중복 인스턴스 제거
                return;
            }

            var options = new SupabaseOptions
            {
                AutoConnectRealtime = true
            };

            supabase = new Supabase.Client(SupabaseSettings.SupabaseURL, SupabaseSettings.SupabaseAnonKey, options);
            await supabase.InitializeAsync();
           


        }

        

        public async void UploadImage(byte[] imageBytes)
        {
            SaveImage(imageBytes, "androidSample.jpg");
            // "db438da4-6bc4-4c10-9bde-6b52fab38e4f" is id

            var imagePath = Path.Combine(Application.persistentDataPath, "androidSample.jpg");

            await supabase.Storage
                          .From("user_img")
                          .Upload(imagePath, "db438da4-6bc4-4c10-9bde-6b52fab38e4f/androidSample.jpg", new FileOptions { CacheControl = "3600", Upsert = true });

            var model = new user_img()
            {
                user_id = "db438da4-6bc4-4c10-9bde-6b52fab38e4f",
                original = "db438da4-6bc4-4c10-9bde-6b52fab38e4f/androidSample.jpg"
            };

            await supabase.From<user_img>().Insert(model);

        }
    }
}

자 이제 시작해보면???

정상적으로 작동한 모습이다.

profile
Better than doing nothing

0개의 댓글

관련 채용 정보