-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathImageExample.cs
More file actions
100 lines (94 loc) · 2.78 KB
/
ImageExample.cs
File metadata and controls
100 lines (94 loc) · 2.78 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using B83.Win32;
public class ImageExample : MonoBehaviour
{
Texture2D[] textures = new Texture2D[6];
DropInfo dropInfo = null;
class DropInfo
{
public string file;
public Vector2 pos;
}
void OnEnable ()
{
UnityDragAndDropHook.InstallHook();
UnityDragAndDropHook.OnDroppedFiles += OnFiles;
}
void OnDisable()
{
UnityDragAndDropHook.UninstallHook();
}
void OnFiles(List<string> aFiles, POINT aPos)
{
string file = "";
// 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 == ".png" || ext == ".jpg" || ext == ".jpeg")
{
file = f;
break;
}
}
// If the user dropped a supported file, create a DropInfo
if (file != "")
{
var info = new DropInfo
{
file = file,
pos = new Vector2(aPos.x, aPos.y)
};
dropInfo = info;
}
}
void LoadImage(int aIndex, DropInfo aInfo)
{
if (aInfo == null)
return;
// get the GUI rect of the last Label / box
var rect = GUILayoutUtility.GetLastRect();
// check if the drop position is inside that rect
if (rect.Contains(aInfo.pos))
{
var data = System.IO.File.ReadAllBytes(aInfo.file);
var tex = new Texture2D(1,1);
tex.LoadImage(data);
if (textures[aIndex] != null)
Destroy(textures[aIndex]);
textures[aIndex] = tex;
}
}
private void OnGUI()
{
DropInfo tmp = null;
if (Event.current.type == EventType.Repaint && dropInfo!= null)
{
tmp = dropInfo;
dropInfo = null;
}
GUILayout.BeginHorizontal();
for (int i = 0; i < 3; i++)
{
if (textures[i] != null)
GUILayout.Label(textures[i], GUILayout.Width(200), GUILayout.Height(200));
else
GUILayout.Box("Drag image here", GUILayout.Width(200), GUILayout.Height(200));
LoadImage(i, tmp);
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
for (int i = 3; i < 6; i++)
{
if (textures[i] != null)
GUILayout.Label(textures[i], GUILayout.Width(200), GUILayout.Height(200));
else
GUILayout.Box("Drag image here", GUILayout.Width(200), GUILayout.Height(200));
LoadImage(i, tmp);
}
GUILayout.EndHorizontal();
}
}