- Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathImageExample.cs
More file actions
Latest commit
100 lines (94 loc) · 2.78 KB
/
Copy pathImageExample.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
usingSystem.Collections.Generic;
usingUnityEngine;
usingSystem.Linq;
usingB83.Win32;
publicclassImageExample:MonoBehaviour
{
Texture2D[]textures=newTexture2D[6];
DropInfodropInfo=null;
classDropInfo
{
publicstringfile;
publicVector2pos;
}
voidOnEnable()
{
UnityDragAndDropHook.InstallHook();
UnityDragAndDropHook.OnDroppedFiles+=OnFiles;
}
voidOnDisable()
{
UnityDragAndDropHook.UninstallHook();
}
voidOnFiles(List<string>aFiles,POINTaPos)
{
stringfile="";
// scan through dropped files and filter out supported image types
foreach(varfinaFiles)
{
varfi=newSystem.IO.FileInfo(f);
varext=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!="")
{
varinfo=newDropInfo
{
file=file,
pos=newVector2(aPos.x,aPos.y)
};
dropInfo=info;
}
}
voidLoadImage(intaIndex,DropInfoaInfo)
{
if(aInfo==null)
return;
// get the GUI rect of the last Label / box
varrect=GUILayoutUtility.GetLastRect();
// check if the drop position is inside that rect
if(rect.Contains(aInfo.pos))
{
vardata=System.IO.File.ReadAllBytes(aInfo.file);
vartex=newTexture2D(1,1);
tex.LoadImage(data);
if(textures[aIndex]!=null)
Destroy(textures[aIndex]);
textures[aIndex]=tex;
}
}
privatevoidOnGUI()
{
DropInfotmp=null;
if(Event.current.type==EventType.Repaint&&dropInfo!=null)
{
tmp=dropInfo;
dropInfo=null;
}
GUILayout.BeginHorizontal();
for(inti=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(inti=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();
}
}