mirror of
https://github.com/photonstorm/phaser
synced 2024-11-26 06:30:38 +00:00
Added ability to load Unity meta data files for sprite sheets
This commit is contained in:
parent
2955df2f4f
commit
ad4f102335
6 changed files with 199 additions and 1 deletions
|
@ -373,6 +373,21 @@ var BaseLoader = new Class({
|
|||
}
|
||||
break;
|
||||
|
||||
case 'unityatlas':
|
||||
|
||||
fileA = file.fileA;
|
||||
fileB = file.fileB;
|
||||
|
||||
if (fileA.type === 'image')
|
||||
{
|
||||
textures.addUnityAtlas(fileA.key, fileA.data, fileB.data);
|
||||
}
|
||||
else
|
||||
{
|
||||
textures.addUnityAtlas(fileB.key, fileB.data, fileA.data);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'bitmapfont':
|
||||
|
||||
fileA = file.fileA;
|
||||
|
|
20
v3/src/loader/filetypes/UnityAtlasFile.js
Normal file
20
v3/src/loader/filetypes/UnityAtlasFile.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
var ImageFile = require('./ImageFile.js');
|
||||
var TextFile = require('./TextFile.js');
|
||||
|
||||
var UnityAtlasFile = function (key, textureURL, atlasURL, path, textureXhrSettings, atlasXhrSettings)
|
||||
{
|
||||
var image = new ImageFile(key, textureURL, path, textureXhrSettings);
|
||||
var data = new TextFile(key, atlasURL, path, atlasXhrSettings);
|
||||
|
||||
// Link them together
|
||||
image.linkFile = data;
|
||||
data.linkFile = image;
|
||||
|
||||
// Set the type
|
||||
image.linkType = 'unityatlas';
|
||||
data.linkType = 'unityatlas';
|
||||
|
||||
return { texture: image, data: data };
|
||||
};
|
||||
|
||||
module.exports = UnityAtlasFile;
|
|
@ -14,6 +14,7 @@ var ScriptFile = require('../loader/filetypes/ScriptFile');
|
|||
var SpriteSheet = require('../loader/filetypes/SpriteSheet');
|
||||
var SVGFile = require('../loader/filetypes/SVGFile');
|
||||
var TextFile = require('../loader/filetypes/TextFile');
|
||||
var UnityAtlasFile = require('../loader/filetypes/UnityAtlasFile');
|
||||
var XMLFile = require('../loader/filetypes/XMLFile');
|
||||
|
||||
var Loader = new Class({
|
||||
|
@ -91,6 +92,17 @@ var Loader = new Class({
|
|||
// Multi-File Loaders
|
||||
// ---------------------------------------------------
|
||||
|
||||
unityAtlas: function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
|
||||
{
|
||||
// Returns an object with two properties: 'texture' and 'data'
|
||||
var files = new UnityAtlasFile(key, textureURL, atlasURL, this.path, textureXhrSettings, atlasXhrSettings);
|
||||
|
||||
this.addFile(files.texture);
|
||||
this.addFile(files.data);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
atlas: function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
|
||||
{
|
||||
// Returns an object with two properties: 'texture' and 'data'
|
||||
|
|
|
@ -138,6 +138,15 @@ var TextureManager = new Class({
|
|||
return texture;
|
||||
},
|
||||
|
||||
addUnityAtlas: function (key, source, data)
|
||||
{
|
||||
var texture = this.create(key, source);
|
||||
|
||||
Parser.UnityYAML(texture, 0, data);
|
||||
|
||||
return texture;
|
||||
},
|
||||
|
||||
/**
|
||||
* [addSpriteSheet description]
|
||||
* @param {[type]} key [description]
|
||||
|
|
141
v3/src/textures/parsers/UnityYAML.js
Normal file
141
v3/src/textures/parsers/UnityYAML.js
Normal file
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
var imageHeight = 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;
|
||||
|
||||
var newFrame = texture.add(name, sourceIndex, frame.x, y, frame.width, frame.height);
|
||||
|
||||
// console.log('name', name, 'rect', 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
|
||||
);
|
||||
}
|
||||
*/
|
||||
};
|
||||
|
||||
// https://docs.unity3d.com/ScriptReference/SpriteMetaData.html
|
||||
|
||||
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;
|
|
@ -7,5 +7,6 @@ module.exports = {
|
|||
Pyxel: require('./Pyxel'),
|
||||
SpriteSheet: require('./SpriteSheet'),
|
||||
SpriteSheetFromAtlas: require('./SpriteSheetFromAtlas'),
|
||||
StarlingXML: require('./StarlingXML')
|
||||
StarlingXML: require('./StarlingXML'),
|
||||
UnityYAML: require('./UnityYAML')
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue