Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
ImageExample.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.IO;
3using B83.Win32;
4using UnityEngine;
5
6public class ImageExample : MonoBehaviour
7{
8 private class DropInfo
9 {
10 public string file;
11
12 public Vector2 pos;
13 }
14
15 private Texture2D[] textures = new Texture2D[6];
16
18
19 private void OnEnable()
20 {
22 UnityDragAndDropHook.OnDroppedFiles += OnFiles;
23 }
24
25 private void OnDisable()
26 {
28 }
29
30 private void OnFiles(List<string> aFiles, POINT aPos)
31 {
32 string text = "";
33 foreach (string aFile in aFiles)
34 {
35 switch (new FileInfo(aFile).Extension.ToLower())
36 {
37 case ".png":
38 case ".jpg":
39 case ".jpeg":
40 text = aFile;
41 goto end_IL_0053;
42 }
43 continue;
44 end_IL_0053:
45 break;
46 }
47 if (text != "")
48 {
50 {
51 file = text,
52 pos = new Vector2(aPos.x, aPos.y)
53 };
54 this.dropInfo = dropInfo;
55 }
56 }
57
58 private void LoadImage(int aIndex, DropInfo aInfo)
59 {
60 if (aInfo != null && GUILayoutUtility.GetLastRect().Contains(aInfo.pos))
61 {
62 byte[] data = File.ReadAllBytes(aInfo.file);
63 Texture2D texture2D = new Texture2D(1, 1);
64 texture2D.LoadImage(data);
65 if (textures[aIndex] != null)
66 {
67 Object.Destroy(textures[aIndex]);
68 }
69 textures[aIndex] = texture2D;
70 }
71 }
72
73 private void OnGUI()
74 {
75 DropInfo aInfo = null;
76 if (Event.current.type == EventType.Repaint && dropInfo != null)
77 {
78 aInfo = dropInfo;
79 dropInfo = null;
80 }
81 GUILayout.BeginHorizontal();
82 for (int i = 0; i < 3; i++)
83 {
84 if (textures[i] != null)
85 {
86 GUILayout.Label(textures[i], GUILayout.Width(200f), GUILayout.Height(200f));
87 }
88 else
89 {
90 GUILayout.Box("Drag image here", GUILayout.Width(200f), GUILayout.Height(200f));
91 }
92 LoadImage(i, aInfo);
93 }
94 GUILayout.EndHorizontal();
95 GUILayout.BeginHorizontal();
96 for (int j = 3; j < 6; j++)
97 {
98 if (textures[j] != null)
99 {
100 GUILayout.Label(textures[j], GUILayout.Width(200f), GUILayout.Height(200f));
101 }
102 else
103 {
104 GUILayout.Box("Drag image here", GUILayout.Width(200f), GUILayout.Height(200f));
105 }
106 LoadImage(j, aInfo);
107 }
108 GUILayout.EndHorizontal();
109 }
110}
void OnFiles(List< string > aFiles, POINT aPos)
Definition: ImageExample.cs:30
Texture2D[] textures
Definition: ImageExample.cs:15
void OnEnable()
Definition: ImageExample.cs:19
void OnDisable()
Definition: ImageExample.cs:25
DropInfo dropInfo
Definition: ImageExample.cs:17
void LoadImage(int aIndex, DropInfo aInfo)
Definition: ImageExample.cs:58
void OnGUI()
Definition: ImageExample.cs:73