/** Adds an animation from a plist file. * Make sure that the frames were previously loaded in the CCSpriteFrameCache. * @since v1.1 */ public void addAnimationsWithFile(string plist) { NSUtils.Assert(plist != null, "Invalid texture file name"); NSDictionary dict = NSDictionary.DictionaryWithContentsOfFileFromResources(plist); NSUtils.Assert(dict != null, "CCAnimationCache: File could not be found: {0}", plist); addAnimationsWithDictionary(dict); }
/** Removes multiple Sprite Frames from a plist file. * Sprite Frames stored in this file will be removed. * It is convenient to call this method when a specific texture needs to be removed. * @since v0.99.5 */ public void removeSpriteFramesFromFile(string plist) { string path = plist; string ext = Path.GetExtension(path); if (ext != null && ext.Length > 0) { path = path.Replace(ext, ""); } NSDictionary dict = NSDictionary.DictionaryWithContentsOfFileFromResources(string.Concat(path, ".txt")); removeSpriteFramesFromDictionary(dict); // remove it from the cache if (_loadedFilenames.Contains(plist)) { _loadedFilenames.Remove(plist); } }
// ------------------------------------------------------------------------------ // Adds multiple Sprite Frames from a plist file. // ------------------------------------------------------------------------------ /** Adds multiple Sprite Frames from a plist file. * A texture will be loaded automatically. The texture name will composed by replacing the .plist suffix with .png . * If you want to use another texture, you should use the addSpriteFramesWithFile:texture method. */ public void addSpriteFramesWithFile(string path) { path = FileUtils.GetFilePathWithoutExtends(path); if (_loadedFilenames.Contains(path)) { CCDebug.Warning("cocos2d: CCSpriteFrameCache: file already loaded: {0}", path); } else { _loadedFilenames.Add(path); NSDictionary dict = NSDictionary.DictionaryWithContentsOfFileFromResources(string.Concat(path, ".txt")); Texture2D texture = Resources.Load <Texture2D> (path); if (texture != null) { addSpriteFrames(dict, texture, path); } else { CCDebug.Log("cocos2d: CCSpriteFrameCache: Couldn't load texture: {0}", path); } } }
/* * renamed plist file of texture packer to .plist.txt */ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { bool hasNewAsset = false; foreach (var asset in importedAssets) { string assetExt = Path.GetExtension(asset); if (assetExt == ".plist") { NSDictionary plist = NSDictionary.DictionaryWithContentsOfFileFromResources(asset); NSDictionary metadataDict = plist.objectForKey <NSDictionary>("metadata"); if (metadataDict != null) { string textureFileName = metadataDict.objectForKey <string>("textureFileName"); string texturePath = asset.Replace(Path.GetFileName(asset), "") + textureFileName; if (File.Exists(texturePath)) { byte[] fileData = File.ReadAllBytes(texturePath); Texture2D texture = new Texture2D(2, 2); texture.LoadImage(fileData); CheckSemiTransparentSprite(plist, texture); //update *.plist.txt string txtPath = asset.Replace(".plist", ".txt"); // if (File.Exists (txtPath)) plist.writeToFile(txtPath); File.Delete(asset); // File.Move (asset, txtPath); hasNewAsset = true; } } } } if (hasNewAsset) { AssetDatabase.Refresh(); } }
static void ParseImagePlist(int firstGid, int tileWidth, int tileHeight, string filename, Dictionary <int, string> gidToFiles, NSDictionary tilesetCaches) { string path = filename + ".txt"; NSDictionary plist = null; if (tilesetCaches != null) { plist = tilesetCaches.objectForKey <NSDictionary>(path); } if (plist == null) { plist = NSDictionary.DictionaryWithContentsOfFileFromResources(filename + ".txt"); } NSDictionary metaDataDict = plist.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = plist.objectForKey <NSDictionary>("frames"); int format = 0; if (metaDataDict != null) { format = metaDataDict.objectForKey <int> ("format"); } int width = 0; if (metaDataDict != null) { Vector2 size = ccUtils.PointFromString((string)metaDataDict ["size"]); width = Mathf.RoundToInt(size.x); } else { NSDictionary texture = metaDataDict.objectForKey <NSDictionary>("texture"); width = texture.objectForKey <int>("width"); } var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; string frameDictKey = (string)frameDictKeyValue.Key; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; int x = 0, y = 0, w = 0, h = 0; if (format == 0) { float ox = frameDict.objectForKey <float>("x"); float oy = frameDict.objectForKey <float>("y"); float ow = frameDict.objectForKey <float>("width"); float oh = frameDict.objectForKey <float>("height"); x = Mathf.RoundToInt(ox); y = Mathf.RoundToInt(oy); w = Mathf.RoundToInt(ow); h = Mathf.RoundToInt(oh); } else if (format == 1 || format == 2) { Rect frame = ccUtils.RectFromString(frameDict.objectForKey <string>("frame")); x = Mathf.RoundToInt(frame.x); y = Mathf.RoundToInt(frame.y); w = Mathf.RoundToInt(frame.width); h = Mathf.RoundToInt(frame.height); } else if (format == 3) { Rect frame = ccUtils.RectFromString(frameDict.objectForKey <string>("textureRect")); x = Mathf.RoundToInt(frame.x); y = Mathf.RoundToInt(frame.y); w = Mathf.RoundToInt(frame.width); h = Mathf.RoundToInt(frame.height); } else { NSUtils.Assert(false, "cocos2d:CCTMXMap: Uknown TexturePack format."); } NSUtils.Assert(w == tileWidth && h == tileHeight, "cocos2d:CCTMXMap: Frame size of tileset file must be same with tmx tilesize."); int col = Mathf.RoundToInt(x / tileWidth); int row = Mathf.RoundToInt(y / tileHeight); int cols = Mathf.RoundToInt(width / tileWidth); int gid = firstGid + col + row * cols; gidToFiles[gid] = frameDictKey; } }