97 lines
2.2 KiB
C#
97 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using B83.Win32;
|
|
using System;
|
|
using B83.Image.BMP;
|
|
using UnityEngine.Tilemaps;
|
|
|
|
public class CustomCardReader : MonoBehaviour
|
|
{
|
|
public SpriteRenderer Renderer;
|
|
public float Timer;
|
|
private BMPLoader _BMPLoader;
|
|
private Texture2D _tex;
|
|
|
|
void OnEnable()
|
|
{
|
|
_BMPLoader = new BMPLoader();
|
|
UnityDragAndDropHook.InstallHook();
|
|
UnityDragAndDropHook.OnDroppedFiles += OnFiles;
|
|
|
|
}
|
|
void OnDisable()
|
|
{
|
|
UnityDragAndDropHook.OnDroppedFiles -= OnFiles;
|
|
UnityDragAndDropHook.UninstallHook();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if(Timer > 0)
|
|
{
|
|
Timer -= Time.deltaTime;
|
|
if(Timer <= 0)
|
|
{
|
|
ClearTextures();
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnFiles(List<string> aFiles, POINT aPos)
|
|
{
|
|
ClearTextures();
|
|
|
|
string file = "";
|
|
bool isBmp = false;
|
|
|
|
// scan through dropped files and filter out supported image types
|
|
foreach (var f in aFiles)
|
|
{
|
|
var fi = new System.IO.FileInfo(f);
|
|
var ext = fi.Extension.ToLower();
|
|
if(ext == ".bmp")
|
|
{
|
|
file = f;
|
|
isBmp = true;
|
|
break;
|
|
}
|
|
else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg")
|
|
{
|
|
file = f;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// If the user dropped a supported file, create a DropInfo
|
|
if (file == "") return;
|
|
|
|
var data = System.IO.File.ReadAllBytes(file);
|
|
|
|
if(isBmp)
|
|
{
|
|
BMPImage bmpImg = _BMPLoader.LoadBMP(data);
|
|
_tex = bmpImg.ToTexture2D();
|
|
}
|
|
else
|
|
{
|
|
_tex = new Texture2D(1, 1);
|
|
_tex.LoadImage(data);
|
|
}
|
|
|
|
Timer = 5;
|
|
Renderer.sprite = Sprite.Create(_tex, new Rect(0.0f, 0.0f, _tex.width, _tex.height), new Vector2(0.5f, 0.5f), 100.0f);
|
|
}
|
|
|
|
private void ClearTextures()
|
|
{
|
|
if (_tex != null)
|
|
{
|
|
Destroy(_tex);
|
|
}
|
|
if (Renderer.sprite != null)
|
|
{
|
|
Destroy(Renderer.sprite);
|
|
}
|
|
}
|
|
} |