/
UnityYAML.js
164 lines (137 loc) · 3.82 KB
/
UnityYAML.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var imageHeight = 0;
/**
* @function addFrame
* @private
* @since 3.0.0
*/
var addFrame = function (texture, sourceIndex, name, frame)
{
// The frame values are the exact coordinates to cut the frame out of the atlas from
var y = imageHeight - frame.y - frame.height;
texture.add(name, sourceIndex, frame.x, y, frame.width, frame.height);
// These are the original (non-trimmed) sprite values
/*
if (src.trimmed)
{
newFrame.setTrim(
src.sourceSize.w,
src.sourceSize.h,
src.spriteSourceSize.x,
src.spriteSourceSize.y,
src.spriteSourceSize.w,
src.spriteSourceSize.h
);
}
*/
};
/**
* Parses a Unity YAML File and creates Frames in the Texture.
* For more details about Sprite Meta Data see https://docs.unity3d.com/ScriptReference/SpriteMetaData.html
*
* @function Phaser.Textures.Parsers.UnityYAML
* @memberof Phaser.Textures.Parsers
* @private
* @since 3.0.0
*
* @param {Phaser.Textures.Texture} texture - The Texture to add the Frames to.
* @param {number} sourceIndex - The index of the TextureSource.
* @param {object} yaml - The YAML data.
*
* @return {Phaser.Textures.Texture} The Texture modified by this parser.
*/
var UnityYAML = function (texture, sourceIndex, yaml)
{
// Add in a __BASE entry (for the entire atlas)
var source = texture.source[sourceIndex];
texture.add('__BASE', sourceIndex, 0, 0, source.width, source.height);
imageHeight = source.height;
var data = yaml.split('\n');
var lineRegExp = /^[ ]*(- )*(\w+)+[: ]+(.*)/;
var prevSprite = '';
var currentSprite = '';
var rect = { x: 0, y: 0, width: 0, height: 0 };
// var pivot = { x: 0, y: 0 };
// var border = { x: 0, y: 0, z: 0, w: 0 };
for (var i = 0; i < data.length; i++)
{
var results = data[i].match(lineRegExp);
if (!results)
{
continue;
}
var isList = (results[1] === '- ');
var key = results[2];
var value = results[3];
if (isList)
{
if (currentSprite !== prevSprite)
{
addFrame(texture, sourceIndex, currentSprite, rect);
prevSprite = currentSprite;
}
rect = { x: 0, y: 0, width: 0, height: 0 };
}
if (key === 'name')
{
// Start new list
currentSprite = value;
continue;
}
switch (key)
{
case 'x':
case 'y':
case 'width':
case 'height':
rect[key] = parseInt(value, 10);
break;
// case 'pivot':
// pivot = eval('var obj = ' + value);
// break;
// case 'border':
// border = eval('var obj = ' + value);
// break;
}
}
if (currentSprite !== prevSprite)
{
addFrame(texture, sourceIndex, currentSprite, rect);
}
return texture;
};
module.exports = UnityYAML;
/*
Example data:
TextureImporter:
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
spriteSheet:
sprites:
- name: asteroids_0
rect:
serializedVersion: 2
x: 5
y: 328
width: 65
height: 82
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
- name: asteroids_1
rect:
serializedVersion: 2
x: 80
y: 322
width: 53
height: 88
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
spritePackingTag: Asteroids
*/