Mathematica 根据 .plist 分割图片

直接上代码好了……

textureSplit[img_, plist_] := Module[
  {tex, xml, sprites},
  tex = Import[img];
  xml = Import[plist, "XML"];
  sprites = 
   sprites = {#[[1]], ToExpression[#[[2]]], #[[3]] == "true"} & /@ 
     SequenceCases[
      xml[[2, 3, 1, 3, 2, 3]], {XMLElement["key", {}, {path_}], 
        XMLElement[
         "dict", {}, {XMLElement["key", {}, {"aliases"}], 
          XMLElement["array", {}, {}], 
          XMLElement["key", {}, {"spriteOffset"}], 
          XMLElement["string", {}, {_}], 
          XMLElement["key", {}, {"spriteSize"}], 
          XMLElement["string", {}, {_}], 
          XMLElement["key", {}, {"spriteSourceSize"}], 
          XMLElement["string", {}, {_}], 
          XMLElement["key", {}, {"textureRect"}], 
          XMLElement["string", {}, {rect_}], 
          XMLElement["key", {}, {"textureRotated"}], 
          XMLElement[rotated_, {}, {}]}]} :> {path, rect, rotated}, 
      Infinity];
  
  {#[[1]], (ImageRotate[
         ImageTake @@ (Join[{tex}, ({{#[[1, 1]], #[[1, 2]] - 1}, {#[[
                  2, 1]], #[[2, 2]] - 1}} &)@
             Reverse@
              Transpose[{#[[1]], #[[1]] + 
                 If[#2, Reverse[#[[2]]], #[[2]]]}]]), 
         If[#2, 90 Degree, 0]] &)[#[[2]], #[[3]]]} & /@ sprites
  ]
textureSplit[img_] := 
 textureSplit[img, 
  DirectoryName[img] <> FileBaseName[img] <> ".plist"]

你可能感兴趣的:(Mathematica 根据 .plist 分割图片)