|
| 1 | +using System; |
| 2 | +using UnityEngine; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +public class FAtlasElement |
| 7 | +{ |
| 8 | + public string name; |
| 9 | + |
| 10 | + public int indexInAtlas; |
| 11 | + |
| 12 | + public FAtlas atlas; |
| 13 | + public int atlasIndex; |
| 14 | + |
| 15 | + public Rect uvRect; |
| 16 | + public Vector2 uvTopLeft; |
| 17 | + public Vector2 uvTopRight; |
| 18 | + public Vector2 uvBottomRight; |
| 19 | + public Vector2 uvBottomLeft; |
| 20 | + |
| 21 | + public Rect sourceRect; |
| 22 | + public Vector2 sourceSize; |
| 23 | + public bool isTrimmed; |
| 24 | + //public bool isRotated; |
| 25 | + |
| 26 | + public FAtlasElement Clone() |
| 27 | + { |
| 28 | + FAtlasElement element = new FAtlasElement(); |
| 29 | + |
| 30 | + element.name = name; |
| 31 | + |
| 32 | + element.indexInAtlas = indexInAtlas; |
| 33 | + |
| 34 | + element.atlas = atlas; |
| 35 | + element.atlasIndex = atlasIndex; |
| 36 | + |
| 37 | + element.uvRect = uvRect; |
| 38 | + element.uvTopLeft = uvTopLeft; |
| 39 | + element.uvTopRight = uvTopRight; |
| 40 | + element.uvBottomRight = uvBottomRight; |
| 41 | + element.uvBottomLeft = uvBottomLeft; |
| 42 | + |
| 43 | + element.sourceRect = sourceRect; |
| 44 | + element.sourceSize = sourceSize; |
| 45 | + element.isTrimmed = isTrimmed; |
| 46 | + |
| 47 | + return element; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +public class FAtlas |
| 52 | +{ |
| 53 | + private string _name; |
| 54 | + private string _imagePath; |
| 55 | + private string _dataPath; |
| 56 | + |
| 57 | + private int _index; |
| 58 | + |
| 59 | + private List<FAtlasElement> _elements = new List<FAtlasElement>(); |
| 60 | + |
| 61 | + private Dictionary<string, FAtlasElement> _elementsByName = new Dictionary<string, FAtlasElement>(); |
| 62 | + |
| 63 | + private Texture _texture; |
| 64 | + private Vector2 _textureSize; |
| 65 | + |
| 66 | + private bool _isSingleImage; |
| 67 | + |
| 68 | + private bool _isTextureAnAsset = false; |
| 69 | + |
| 70 | + //TODO: allow users to pass a dictionary of pre-built atlas data if they want |
| 71 | + public FAtlas (string name, Texture texture, int index) //single image |
| 72 | + { |
| 73 | + _name = name; |
| 74 | + _imagePath = ""; |
| 75 | + _dataPath = ""; |
| 76 | + _index = index; |
| 77 | + |
| 78 | + _texture = texture; |
| 79 | + _textureSize = new Vector2(_texture.width,_texture.height); |
| 80 | + |
| 81 | + CreateAtlasFromSingleImage(); |
| 82 | + } |
| 83 | + |
| 84 | + public FAtlas (string name, string dataPath, Texture texture, int index) //atlas with data path |
| 85 | + { |
| 86 | + _name = name; |
| 87 | + _imagePath = ""; |
| 88 | + _dataPath = dataPath; |
| 89 | + _index = index; |
| 90 | + |
| 91 | + _texture = texture; |
| 92 | + _textureSize = new Vector2(_texture.width,_texture.height); |
| 93 | + |
| 94 | + _isSingleImage = false; |
| 95 | + LoadAtlasData(); |
| 96 | + } |
| 97 | + |
| 98 | + public FAtlas (string name, string imagePath, string dataPath, int index, bool shouldLoadAsSingleImage) |
| 99 | + { |
| 100 | + _name = name; |
| 101 | + _imagePath = imagePath; |
| 102 | + _dataPath = dataPath; |
| 103 | + |
| 104 | + _index = index; |
| 105 | + |
| 106 | + LoadTexture(); |
| 107 | + |
| 108 | + if(shouldLoadAsSingleImage) |
| 109 | + { |
| 110 | + _isSingleImage = true; |
| 111 | + CreateAtlasFromSingleImage(); |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + _isSingleImage = false; |
| 116 | + LoadAtlasData(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void LoadTexture() |
| 121 | + { |
| 122 | + _texture = Resources.Load (_imagePath, typeof(Texture)) as Texture; |
| 123 | + |
| 124 | + if(_texture == null) |
| 125 | + { |
| 126 | + throw new FutileException("Couldn't load the atlas texture from: " + _imagePath); |
| 127 | + } |
| 128 | + |
| 129 | + _isTextureAnAsset = true; |
| 130 | + |
| 131 | + _textureSize = new Vector2(_texture.width,_texture.height); |
| 132 | + } |
| 133 | + |
| 134 | + private void LoadAtlasData() |
| 135 | + { |
| 136 | + TextAsset dataAsset = Resources.Load (_dataPath, typeof(TextAsset)) as TextAsset; |
| 137 | + |
| 138 | + if(dataAsset == null) |
| 139 | + { |
| 140 | + throw new FutileException("Couldn't load the atlas data from: " + _dataPath); |
| 141 | + } |
| 142 | + |
| 143 | + Dictionary<string,object> dict = dataAsset.text.dictionaryFromJson(); |
| 144 | + |
| 145 | + if(dict == null) |
| 146 | + { |
| 147 | + throw new FutileException("The atlas at " + _dataPath + " was not a proper JSON file. Make sure to select \"Unity3D\" in TexturePacker."); |
| 148 | + } |
| 149 | + |
| 150 | + Dictionary<string,object> frames = (Dictionary<string,object>) dict["frames"]; |
| 151 | + |
| 152 | + float scaleInverse = Futile.resourceScaleInverse; |
| 153 | + |
| 154 | + int index = 0; |
| 155 | + |
| 156 | + foreach(KeyValuePair<string,object> item in frames) |
| 157 | + { |
| 158 | + FAtlasElement element = new FAtlasElement(); |
| 159 | + |
| 160 | + element.indexInAtlas = index++; |
| 161 | + |
| 162 | + string name = (string) item.Key; |
| 163 | + |
| 164 | + if(Futile.shouldRemoveAtlasElementFileExtensions) |
| 165 | + { |
| 166 | + int extensionPosition = name.LastIndexOf("."); |
| 167 | + if (extensionPosition >= 0) name = name.Substring(0, extensionPosition); |
| 168 | + } |
| 169 | + |
| 170 | + element.name = name; |
| 171 | + |
| 172 | + IDictionary itemDict = (IDictionary)item.Value; |
| 173 | + |
| 174 | + element.isTrimmed = (bool)itemDict["trimmed"]; |
| 175 | + |
| 176 | + if((bool)itemDict["rotated"]) |
| 177 | + { |
| 178 | + throw new NotSupportedException("Futile no longer supports TexturePacker's \"rotated\" flag. Please disable it when creating the "+_dataPath+" atlas."); |
| 179 | + } |
| 180 | + |
| 181 | + IDictionary frame = (IDictionary)itemDict["frame"]; |
| 182 | + |
| 183 | + float rectX = float.Parse(frame["x"].ToString()); |
| 184 | + float rectY = float.Parse(frame["y"].ToString()); |
| 185 | + float rectW = float.Parse(frame["w"].ToString()); |
| 186 | + float rectH = float.Parse(frame["h"].ToString()); |
| 187 | + |
| 188 | + Rect uvRect = new Rect |
| 189 | + ( |
| 190 | + rectX/_textureSize.x, |
| 191 | + ((_textureSize.y - rectY - rectH)/_textureSize.y), |
| 192 | + rectW/_textureSize.x, |
| 193 | + rectH/_textureSize.y |
| 194 | + ); |
| 195 | + |
| 196 | + element.uvRect = uvRect; |
| 197 | + |
| 198 | + element.uvTopLeft.Set(uvRect.xMin,uvRect.yMax); |
| 199 | + element.uvTopRight.Set(uvRect.xMax,uvRect.yMax); |
| 200 | + element.uvBottomRight.Set(uvRect.xMax,uvRect.yMin); |
| 201 | + element.uvBottomLeft.Set(uvRect.xMin,uvRect.yMin); |
| 202 | + |
| 203 | + |
| 204 | + IDictionary sourceRect = (IDictionary)itemDict["spriteSourceSize"]; |
| 205 | + |
| 206 | + rectX = float.Parse(sourceRect["x"].ToString()) * scaleInverse; |
| 207 | + rectY = float.Parse(sourceRect["y"].ToString()) * scaleInverse; |
| 208 | + rectW = float.Parse(sourceRect["w"].ToString()) * scaleInverse; |
| 209 | + rectH = float.Parse(sourceRect["h"].ToString()) * scaleInverse; |
| 210 | + |
| 211 | + element.sourceRect = new Rect(rectX,rectY,rectW,rectH); |
| 212 | + |
| 213 | + |
| 214 | + IDictionary sourceSize = (IDictionary)itemDict["sourceSize"]; |
| 215 | + element.sourceSize.x = float.Parse(sourceSize["w"].ToString()) * scaleInverse; |
| 216 | + element.sourceSize.y = float.Parse(sourceSize["h"].ToString()) * scaleInverse; |
| 217 | + |
| 218 | + _elements.Add (element); |
| 219 | + _elementsByName.Add(element.name, element); |
| 220 | + } |
| 221 | + |
| 222 | + Resources.UnloadAsset(dataAsset); |
| 223 | + } |
| 224 | + |
| 225 | + private void CreateAtlasFromSingleImage() |
| 226 | + { |
| 227 | + FAtlasElement element = new FAtlasElement(); |
| 228 | + |
| 229 | + element.name = _name; |
| 230 | + element.indexInAtlas = 0; |
| 231 | + |
| 232 | + //TODO: may have to offset the rect slightly |
| 233 | + float scaleInverse = Futile.resourceScaleInverse; |
| 234 | + |
| 235 | + Rect uvRect = new Rect(0.0f,0.0f,1.0f,1.0f); |
| 236 | + |
| 237 | + element.uvRect = uvRect; |
| 238 | + |
| 239 | + element.uvTopLeft.Set(uvRect.xMin,uvRect.yMax); |
| 240 | + element.uvTopRight.Set(uvRect.xMax,uvRect.yMax); |
| 241 | + element.uvBottomRight.Set(uvRect.xMax,uvRect.yMin); |
| 242 | + element.uvBottomLeft.Set(uvRect.xMin,uvRect.yMin); |
| 243 | + |
| 244 | + element.sourceRect = new Rect(0,0,_textureSize.x*scaleInverse,_textureSize.y*scaleInverse); |
| 245 | + |
| 246 | + element.sourceSize = new Vector2(_textureSize.x*scaleInverse,_textureSize.y*scaleInverse); |
| 247 | + element.isTrimmed = false; |
| 248 | + |
| 249 | + _elements.Add (element); |
| 250 | + _elementsByName.Add (element.name, element); |
| 251 | + } |
| 252 | + |
| 253 | + public void Unload () |
| 254 | + { |
| 255 | + if(_isTextureAnAsset) |
| 256 | + { |
| 257 | + Resources.UnloadAsset(_texture); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + public List<FAtlasElement> elements |
| 262 | + { |
| 263 | + get {return _elements;} |
| 264 | + } |
| 265 | + |
| 266 | + public int index |
| 267 | + { |
| 268 | + get {return _index;} |
| 269 | + } |
| 270 | + |
| 271 | + public Texture texture |
| 272 | + { |
| 273 | + get {return _texture;} |
| 274 | + } |
| 275 | + |
| 276 | + public Vector2 textureSize |
| 277 | + { |
| 278 | + get {return _textureSize;} |
| 279 | + } |
| 280 | + |
| 281 | + public string name |
| 282 | + { |
| 283 | + get {return _name;} |
| 284 | + } |
| 285 | + |
| 286 | + public string imagePath |
| 287 | + { |
| 288 | + get {return _imagePath;} |
| 289 | + } |
| 290 | + |
| 291 | + public string dataPath |
| 292 | + { |
| 293 | + get {return _dataPath;} |
| 294 | + } |
| 295 | + |
| 296 | + public bool isSingleImage |
| 297 | + { |
| 298 | + get {return _isSingleImage;} |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | + |
0 commit comments