using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class LoadImage : MonoBehaviour
{
private Image image;
private Button button;
public void Start()
{
image = this.GetComponent<Image>();
button = this.GetComponent<Button>();
button.onClick.AddListener(LoadImages);
}
public void LoadImages()
{
string imagePath = EditorUtility.OpenFilePanel("Select Image", "", "png,jpg,jpeg");
if (!string.IsNullOrEmpty(imagePath))
{
Texture2D texture = new Texture2D(2, 2);
byte[] imageData = File.ReadAllBytes(imagePath);
texture.LoadImage(imageData);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
image.sprite = sprite;
}
}
public void LoadISprite()
{
string imagePath = UnityEditor.EditorUtility.OpenFolderPanel("Select Image Folder", "", "");
if (!string.IsNullOrEmpty(imagePath))
{
string[] imagePaths = Directory.GetFiles(imagePath, "*.png");
if (imagePaths.Length > 0)
{
string firstImagePath = imagePaths[0];
Texture2D texture = new Texture2D(2, 2);
byte[] imageData = File.ReadAllBytes(firstImagePath);
texture.LoadImage(imageData);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
image.sprite = sprite;
}
}
}
}