public void playBackgroundMusic(string filePath, bool loop = true) { stopBackgroundMusic(); AudioClip audio = getAudioClip(filePath); if (audio == null) { CCDebug.Warning("cocos2d:SimpleAudioEngine: Audio {0} not found.", filePath); } else { playAudio(audio, _backgroundMusicSource, loop, _backgroundMusicVolume); } }
public MovieImpAction(MovieImp movie, int startFrame, int endFrame) { if (startFrame == endFrame) { CCDebug.Warning("MovieAction-MovieID:{0}: startFrame should not equals to endFrame.", movie.characterId); } _movie = movie; _startFrame = startFrame; _endFrame = endFrame; _stoped = false; float duration = (Mathf.Abs(_endFrame - _startFrame) + 1) / movie.fps; base.initWithDuration(duration); }
public int playEffect(string filePath) { if (filePath == null) { CCDebug.Warning("cocos2d:SimpleAudioEngine: Audio file path should not be null."); return(-1); } AudioClip audio = getAudioClip(filePath); if (audio == null) { CCDebug.Warning("cocos2d:SimpleAudioEngine: Audio {0} not found.", filePath); return(-1); } else { _currentSource = (_currentSource + 1) % AUDIO_SOURCES_NUM; playAudio(audio, _audioSources[_currentSource], false, _effectsVolume); return(_currentSource); } }
/** Preloads a music file so it will be ready to play as background music */ public void preloadBackgroundMusic(string filePath) { if (_preLoadedAudios == null) { _preLoadedAudios = new Dictionary <string, AudioClip> (); } string ext = Path.GetExtension(filePath); if (ext != null && ext.Length > 0) { filePath = filePath.Replace(ext, ""); } AudioClip audio = Resources.Load <AudioClip> (filePath); if (audio == null) { CCDebug.Warning("cocos2d:SimpleAudioEngine: Audio {0} not found.", filePath); } else { _preLoadedAudios [filePath] = audio; } }
static NSDictionary DictionaryWithContentsOfString(string text, FileUtils.ZipDelegate zip = null) { if (text == null || text.Length == 0) { return(null); } try{ text = System.Text.RegularExpressions.Regex.Replace(text, "<.*\\.dtd\">", string.Empty); XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; settings.ValidationType = ValidationType.None; XmlDocument xmlDoc = new XmlDocument(); using (StringReader sr = new StringReader(text)) using (XmlReader reader = XmlReader.Create(sr, settings)) { xmlDoc.Load(reader); } // XmlDocument xmlDoc = new XmlDocument(); // xmlDoc.LoadXml (text); XmlNode rootNode = xmlDoc.DocumentElement.ChildNodes[0]; if (rootNode.Name != "dict") { return(null); } NSDictionary dict = NSCollectionUtils.ParseDictionary(rootNode); if (dict != null) { dict.zip = zip; } return(dict); }catch (Exception e) { CCDebug.Warning("NSDicitonary:DictionaryWithContentsOfString:Error:{0}", e); return(null); } }
public static CCTMXMap Parse(string file, NSDictionary tilesetCaches = null) { string text = LoadText(file); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(text); XmlNodeList nodeList = xmlDoc.DocumentElement.ChildNodes; // get mapWidth, mapHeight, tileWidth, tileHeight XmlNode mapNode = xmlDoc.DocumentElement; float version = float.Parse(mapNode.Attributes["version"].InnerText); if (!FloatUtils.EQ(version, 1.0f)) { CCDebug.Warning("cocos2d:CCTMXMap: Found {0} tmx file, but only 1.0 version was tested.", version); } string dir = file.Replace(Path.GetFileName(file), ""); CCTMXMap map = new CCTMXMap(); map.fileName = file; map.cols = int.Parse(mapNode.Attributes["width"].InnerText); map.rows = int.Parse(mapNode.Attributes["height"].InnerText); map.tileWidth = int.Parse(mapNode.Attributes["tilewidth"].InnerText); map.tileHeight = int.Parse(mapNode.Attributes["tileheight"].InnerText); Dictionary <int, string> gidToFiles = new Dictionary <int, string> (256); Dictionary <int, Dictionary <string, string> > gidToTileProperties = new Dictionary <int, Dictionary <string, string> > (256); List <CCTMXLayer> layers = new List <CCTMXLayer> (8); var enumerator = nodeList.GetEnumerator(); while (enumerator.MoveNext()) { XmlNode nodeData = (XmlNode)enumerator.Current; if (nodeData.Name == "tileset") { ParseTileset(nodeData, gidToFiles, gidToTileProperties, dir, tilesetCaches); } else if (nodeData.Name == "layer") { CCTMXLayer layer = ParseLayer(nodeData, map.cols, map.rows, gidToFiles, gidToTileProperties); layers.Add(layer); } else if (nodeData.Name == "objectgroup") { CCTMXLayer layer = ParseObjectGroup(nodeData, map.cols, map.rows, map.tileWidth, map.tileHeight, gidToFiles, gidToTileProperties); layers.Add(layer); } else if (nodeData.Name == "properties") { if (map.properties == null) { map.properties = new Dictionary <string, string>(); } ParseProperties(nodeData, map.properties); } } map.gidToFiles = gidToFiles; map.gidToTileProperties = gidToTileProperties; map.layers = layers.ToArray(); return(map); }
void analyze(string image, string plist) { NSDictionary dictionary = NSDictionary.DictionaryWithContentsOfFileFromResources(plist); NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); Vector2 atlasSize; int format = 0; // get the format if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); atlasSize = ccUtils.PointFromString(metadataDict.objectForKey <string> ("size")); } else { NSDictionary texture = dictionary.objectForKey <NSDictionary>("texture"); float width = texture.objectForKey <float>("width"); float height = texture.objectForKey <float>("height"); atlasSize = new Vector2(width, height); } // SpriteFrame info Rect rect = new Rect(); bool isRotated = false; List <SpriteMetaData> metaData = new List <SpriteMetaData> (); // add real frames var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; string frameDictKey = (string)frameDictKeyValue.Key; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; if (format == 0) { float x = frameDict.objectForKey <float>("x"); float y = frameDict.objectForKey <float>("y"); float w = frameDict.objectForKey <float>("width"); float h = frameDict.objectForKey <float>("height"); int ow = frameDict.objectForKey <int>("originalWidth"); int oh = frameDict.objectForKey <int>("originalHeight"); // check ow/oh if (ow == 0 || oh == 0) { CCDebug.Warning("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // set frame info rect = new Rect(x, y, w, h); isRotated = false; } else if (format == 1 || format == 2) { Rect frame = ccUtils.RectFromString(frameDict.objectForKey <string>("frame")); bool rotated = false; // rotation if (format == 2) { rotated = frameDict.objectForKey <bool>("rotated"); } // set frame info rect = frame; isRotated = rotated; } else if (format == 3) { // get values Vector2 spriteSize = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteSize")); Rect textureRect = ccUtils.RectFromString(frameDict.objectForKey <string>("textureRect")); bool textureRotated = frameDict.objectForKey <bool>("textureRotated"); // set frame info rect = new Rect(textureRect.position.x, textureRect.position.y, spriteSize.x, spriteSize.y); isRotated = textureRotated; } if (isRotated) { rect.size = new Vector2(rect.size.y, rect.size.x); } // add sprite metadata { SpriteMetaData smd = new SpriteMetaData(); smd.name = frameDictKey; rect.y = atlasSize.y - rect.y - rect.height; smd.rect = rect; smd.alignment = (int)UnityEngine.SpriteAlignment.Center; smd.pivot = smd.rect.center; metaData.Add(smd); } } //update texutre importer TextureImporter importer = assetImporter as TextureImporter; if (importer.textureType != TextureImporterType.Advanced) { importer.textureType = TextureImporterType.Sprite; } importer.maxTextureSize = 4096; importer.spriteImportMode = SpriteImportMode.Multiple; importer.spritesheet = metaData.ToArray(); //update #tp.txt string textureBase = plist.Replace(Path.GetFileName(plist), ""); string txtPath = textureBase + Path.GetFileNameWithoutExtension(plist) + "-tp.txt"; // dictionary.writeToFile (assetPath); if (File.Exists(txtPath)) { File.Delete(txtPath); } File.Move(plist, txtPath); //update unity edior widnow AssetDatabase.ImportAsset(txtPath); AssetDatabase.Refresh(); //update asset info // CCTPData data = ScriptableObject.CreateInstance<CCTPData> (); // data.frames = spriteFrames; // data.aliases = spriteFramesAliases; // // string textureBase = plist.Replace(Path.GetFileName(plist), ""); // string assetPath = textureBase + Path.GetFileNameWithoutExtension(plist) + "#tp.asset"; //// string path = Path.ChangeExtension (plist, "tp") + ".asset"; // AssetDatabase.CreateAsset (data, assetPath); // AssetDatabase.SaveAssets (); }
void addSpriteFrames(NSDictionary dictionary, Sprite[] sprites) { Dictionary <string, Sprite> spritesDict = new Dictionary <string, Sprite> (); for (int i = 0; i < sprites.Length; i++) { Sprite s = sprites[i]; spritesDict.Add(s.name, s); } NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); int format = 0; // get the format if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); } // SpriteFrame info // Rect rect = new Rect(); bool isRotated = false; Vector2 frameOffset = Vector2.zero; Vector2 originalSize = Vector2.zero; // add real frames var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; string frameDictKey = (string)frameDictKeyValue.Key; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; CCSpriteFrame spriteFrame = null; if (format == 0) { // float x = frameDict.objectForKey<float>("x"); // float y = frameDict.objectForKey<float>("y"); // float w = frameDict.objectForKey<float>("width"); // float h = frameDict.objectForKey<float>("height"); float ox = frameDict.objectForKey <float>("offsetX"); float oy = frameDict.objectForKey <float>("offsetY"); int ow = frameDict.objectForKey <int>("originalWidth"); int oh = frameDict.objectForKey <int>("originalHeight"); // check ow/oh if (ow == 0 || oh == 0) { CCDebug.Warning("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // set frame info // rect = new Rect(x, y, w, h); isRotated = false; frameOffset = new Vector2(ox, oy); originalSize = new Vector2(ow, oh); // if(isRotated) // rect.size = new Vector2(rect.size.y, rect.size.x); } else if (format == 1 || format == 2) { // Rect frame = ccUtils.RectFromString(frameDict.objectForKey<string>("frame")); bool rotated = false; // rotation if (format == 2) { rotated = frameDict.objectForKey <bool>("rotated"); } Vector2 offset = ccUtils.PointFromString(frameDict.objectForKey <string>("offset")); Vector2 sourceSize = ccUtils.PointFromString(frameDict.objectForKey <string>("sourceSize")); // set frame info // rect = frame; isRotated = rotated; frameOffset = offset; originalSize = sourceSize; } else if (format == 3) { // get values // Vector2 spriteSize = ccUtils.PointFromString(frameDict.objectForKey<string>("spriteSize")); Vector2 spriteOffset = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteOffset")); Vector2 spriteSourceSize = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteSourceSize")); // Rect textureRect = ccUtils.RectFromString(frameDict.objectForKey<string>("textureRect")); bool textureRotated = frameDict.objectForKey <bool>("textureRotated"); // get aliases NSArray aliases = frameDict.objectForKey <NSArray>("aliases"); var aliasesEnumerator = aliases.GetEnumerator(); while (aliasesEnumerator.MoveNext()) { string alias = (string)aliasesEnumerator.Current; if (_spriteFramesAliases.ContainsKey(alias)) { CCDebug.Warning("cocos2d: WARNING: an alias with name {0} already exists", alias); } _spriteFramesAliases[alias] = frameDictKey; } // set frame info // rect = new Rect(textureRect.position.x, textureRect.position.y, spriteSize.x, spriteSize.y); isRotated = textureRotated; frameOffset = spriteOffset; originalSize = spriteSourceSize; } Sprite spt; if (!spritesDict.TryGetValue(frameDictKey, out spt)) { CCDebug.Warning("cocos2d: WARNING: a sprite frame with name {0} not found", frameDictKey); continue; } // add sprite frame spriteFrame = new CCSpriteFrame(spt, originalSize, frameOffset, isRotated); _spriteFrames.Add(frameDictKey, spriteFrame); } }