|
| 1 | +/* |
| 2 | +Example data: |
| 3 | +
|
| 4 | +TextureImporter: |
| 5 | + spritePivot: {x: .5, y: .5} |
| 6 | + spriteBorder: {x: 0, y: 0, z: 0, w: 0} |
| 7 | + spritePixelsToUnits: 100 |
| 8 | + spriteSheet: |
| 9 | + sprites: |
| 10 | + - name: asteroids_0 |
| 11 | + rect: |
| 12 | + serializedVersion: 2 |
| 13 | + x: 5 |
| 14 | + y: 328 |
| 15 | + width: 65 |
| 16 | + height: 82 |
| 17 | + alignment: 0 |
| 18 | + pivot: {x: 0, y: 0} |
| 19 | + border: {x: 0, y: 0, z: 0, w: 0} |
| 20 | + - name: asteroids_1 |
| 21 | + rect: |
| 22 | + serializedVersion: 2 |
| 23 | + x: 80 |
| 24 | + y: 322 |
| 25 | + width: 53 |
| 26 | + height: 88 |
| 27 | + alignment: 0 |
| 28 | + pivot: {x: 0, y: 0} |
| 29 | + border: {x: 0, y: 0, z: 0, w: 0} |
| 30 | + spritePackingTag: Asteroids |
| 31 | +*/ |
| 32 | + |
| 33 | +var imageHeight = 0; |
| 34 | + |
| 35 | +var addFrame = function (texture, sourceIndex, name, frame) |
| 36 | +{ |
| 37 | + // The frame values are the exact coordinates to cut the frame out of the atlas from |
| 38 | + |
| 39 | + var y = imageHeight - frame.y - frame.height; |
| 40 | + |
| 41 | + var newFrame = texture.add(name, sourceIndex, frame.x, y, frame.width, frame.height); |
| 42 | + |
| 43 | + // console.log('name', name, 'rect', frame.x, y, frame.width, frame.height); |
| 44 | + |
| 45 | + // These are the original (non-trimmed) sprite values |
| 46 | + /* |
| 47 | + if (src.trimmed) |
| 48 | + { |
| 49 | + newFrame.setTrim( |
| 50 | + src.sourceSize.w, |
| 51 | + src.sourceSize.h, |
| 52 | + src.spriteSourceSize.x, |
| 53 | + src.spriteSourceSize.y, |
| 54 | + src.spriteSourceSize.w, |
| 55 | + src.spriteSourceSize.h |
| 56 | + ); |
| 57 | + } |
| 58 | + */ |
| 59 | +}; |
| 60 | + |
| 61 | +// https://docs.unity3d.com/ScriptReference/SpriteMetaData.html |
| 62 | + |
| 63 | +var UnityYAML = function (texture, sourceIndex, yaml) |
| 64 | +{ |
| 65 | + // Add in a __BASE entry (for the entire atlas) |
| 66 | + var source = texture.source[sourceIndex]; |
| 67 | + |
| 68 | + texture.add('__BASE', sourceIndex, 0, 0, source.width, source.height); |
| 69 | + |
| 70 | + imageHeight = source.height; |
| 71 | + |
| 72 | + var data = yaml.split('\n'); |
| 73 | + |
| 74 | + var lineRegExp = /^[ ]*(- )*(\w+)+[: ]+(.*)/; |
| 75 | + |
| 76 | + var prevSprite = ''; |
| 77 | + var currentSprite = ''; |
| 78 | + var rect = { x: 0, y: 0, width: 0, height: 0 }; |
| 79 | + var pivot = { x: 0, y: 0 }; |
| 80 | + var border = { x: 0, y: 0, z: 0, w: 0 }; |
| 81 | + |
| 82 | + for (var i = 0; i < data.length; i++) |
| 83 | + { |
| 84 | + var results = data[i].match(lineRegExp); |
| 85 | + |
| 86 | + if (!results) |
| 87 | + { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + var isList = (results[1] === '- '); |
| 92 | + var key = results[2]; |
| 93 | + var value = results[3]; |
| 94 | + |
| 95 | + if (isList) |
| 96 | + { |
| 97 | + if (currentSprite !== prevSprite) |
| 98 | + { |
| 99 | + addFrame(texture, sourceIndex, currentSprite, rect); |
| 100 | + |
| 101 | + prevSprite = currentSprite; |
| 102 | + } |
| 103 | + |
| 104 | + rect = { x: 0, y: 0, width: 0, height: 0 }; |
| 105 | + } |
| 106 | + |
| 107 | + if (key === 'name') |
| 108 | + { |
| 109 | + // Start new list |
| 110 | + currentSprite = value; |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + switch (key) |
| 115 | + { |
| 116 | + case 'x': |
| 117 | + case 'y': |
| 118 | + case 'width': |
| 119 | + case 'height': |
| 120 | + rect[key] = parseInt(value, 10); |
| 121 | + break; |
| 122 | + |
| 123 | + case 'pivot': |
| 124 | + pivot = eval('var obj = ' + value); |
| 125 | + break; |
| 126 | + |
| 127 | + case 'border': |
| 128 | + border = eval('var obj = ' + value); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (currentSprite !== prevSprite) |
| 134 | + { |
| 135 | + addFrame(texture, sourceIndex, currentSprite, rect); |
| 136 | + } |
| 137 | + |
| 138 | + return texture; |
| 139 | +}; |
| 140 | + |
| 141 | +module.exports = UnityYAML; |
0 commit comments