-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadImage.cs
125 lines (102 loc) · 3.32 KB
/
ReadImage.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ReadImage : ReaderFile
{
protected Texture2D img;
protected Texture2D[] imageFiles;
protected List<Color> unknownColors = new List<Color>();
protected string levelsPath = "Levels/Images";
public override bool ContainsFile => imageFiles != null && imageFiles.Length > 0;
public ReadImage(LevelElements levelElements, Texture2D[] images = null)
{
this.levelElements = levelElements;
if (images != null)
{
if (images.Length == 0)
{
images = Resources.LoadAll<Texture2D>(levelsPath);
}
if (images.Length > 0)
{
if (images.Length > 1)
{
img = images[Random.Range(0, images.Length)];
}
else
{
img = images[0];
}
imageFiles = images;
}
}
}
public override ReaderFile Parse()
{
if (img != null)
{
dimensions = (img.width, img.height);
Color[] pixels = img.GetPixels();
characters.AddRange(pixels.OfType<object>());
}
return this;
}
protected override void AddElements(Vector3[] spawnPositions, Transform parent)
{
int count = 0;
foreach (Vector3 pos in spawnPositions)
{
if (characters[count] is Color)
{
Color? pixel = characters[count] as Color?;
if (pixel.Equals(Color.white))
{
if (levelElements.ground != null)
{
Object.Instantiate(levelElements.ground, pos, Quaternion.identity, parent);
}
}
else if (pixel.Equals(Color.black))
{
if (levelElements.wall != null)
{
Object.Instantiate(levelElements.wall, pos, Quaternion.identity, parent);
}
} else
{
unknownColors.Add(pixel.GetValueOrDefault());
var hexaDecimal = ColorUtility.ToHtmlStringRGBA(pixel.GetValueOrDefault());
Debug.LogWarningFormat("The COLOR is unknown to parse => HEXADECIMAL: #{0}, RGB: {1}", hexaDecimal, pixel?.ToString());
}
count++;
}
}
}
public virtual void DebugGrayImage(Texture2D img)
{
#if UNITY_EDITOR
Color[] pixels = img.GetPixels();
int black = 0, white = 0;
foreach (var colorPixel in pixels)
{
if (colorPixel.Equals(Color.white))
{
white++;
}
else if (colorPixel.Equals(Color.black))
{
black++;
}
}
if (black == 0 && white == 0)
{
Debug.LogWarningFormat("Maybe the image {0} is not in grayscale", img.name);
}
else
{
Debug.Log($"White = {white}");
Debug.Log($"Black = {black}");
}
#endif
}
}