-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFilePickerManager.cs
More file actions
45 lines (36 loc) · 1.18 KB
/
FilePickerManager.cs
File metadata and controls
45 lines (36 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using SFB;
public class FilePickerManager : MonoBehaviour
{
public Image previewImage;
private string selectedFilePath;
private AuthManager authManager;
private void Start()
{
authManager = FindObjectOfType<AuthManager>();
}
public void OpenFilePicker()
{
string[] paths = StandaloneFileBrowser.OpenFilePanel("Select an Image", "", "png,jpg,jpeg", false);
if (paths.Length > 0 && !string.IsNullOrEmpty(paths[0]))
{
selectedFilePath = paths[0];
DisplayImage(selectedFilePath);
}
else
{
Debug.LogError("No image selected.");
}
}
private void DisplayImage(string filePath)
{
if (string.IsNullOrEmpty(filePath)) return;
byte[] imageBytes = File.ReadAllBytes(filePath);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(imageBytes);
previewImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
authManager.UploadAvatar(filePath);
}
}