GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — UnityWindowsFileDrag-Drop/ImageExample.cs at master · Bunny83/UnityWindowsFileDrag-Drop

Expand file tree

/

ImageExample.cs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
100 lines (94 loc) · 2.78 KB
/

ImageExample.cs

Copy path

File metadata and controls

100 lines (94 loc) · 2.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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();
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.