public void preloadPrefab(string path) { if (!_prefabs.ContainsKey(path)) { GameObject obj = Resources.Load <GameObject>(path); if (obj == null) { CCDebug.Warning("CC3SpriteFactory#preloadPrefab: {0} not found.", path); } _prefabs [path] = obj; } }
public void preloadAnimationClips(string path) { if (!_animationClips.ContainsKey(path)) { AnimationClip[] obj = Resources.LoadAll <AnimationClip>(path); if (obj == null) { CCDebug.Warning("CC3SpriteFactory#preloadAnimationClips: {0} not found.", path); } _animationClips [path] = obj; } }
public void playBackgroundMusic(string filePath, bool loop = true) { AudioClip audio = getAudioClip(filePath); if (audio == null) { CCDebug.Warning("cocos2d:SimpleAudioEngine: Audio {0} not found.", filePath); } else { playBackgroundMusic(audio, loop); } }
public GameObject getPrefab(string path) { GameObject prefab; if (!_prefabs.TryGetValue(path, out prefab)) { preloadPrefab(path); prefab = _prefabs [path]; } if (prefab == null) { CCDebug.Warning("CC3SpriteFactory {0} not found.", path); } return(prefab); }
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 { return(playEffect(audio)); } }
// ------------------------------------------------------------------------------ // 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); } } }
/** 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 runtime metod // ------------------------------------------------------------------------------ public CCFactoryGear takeGear(string category) { Storage storage = getStorage(category, false); if (storage != null) { while (storage.gears.Count > 0) { CCFactoryGear gear = storage.gears[0]; storage.gears.RemoveAt(0); gear.gameObject.hideFlags = HideFlags.None; gear.gameObject.SetActive(true); if (gear.gameObject.transform.childCount != 0) { CCDebug.Warning("CCFactory try to take a not empty gear: {0}-{1}.", gear.gameObject, gear.gameObject.transform.GetChild(0)); DestroyObject(gear.gameObject); } else { return(gear); } } //no gear caches { Type[] componentTypes = new Type[storage.componentTypeNames.Length]; for (int i = 0; i < componentTypes.Length; i++) { componentTypes[i] = Type.GetType(storage.componentTypeNames[i]); } CCFactoryGear gear = buildGear(componentTypes); gear.gameObject.hideFlags = HideFlags.None; gear.gameObject.SetActive(true); return(gear); } } return(null); }
void addSpriteFrames(NSDictionary dictionary, Texture2D texture, string textureFileName) { NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); // get the format int format = 0; if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); } // get texture size Vector2 textureSize = new Vector2(texture.width, texture.height); // check the format NSUtils.Assert(format >= 0 && format <= 3, @"cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:"); // SpriteFrame info Rect rect = new Rect(); bool textureRotated = false; Vector2 spriteOffset = Vector2.zero; Vector2 originalSize = Vector2.zero; bool semi = false; 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"); 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); textureRotated = false; spriteOffset = new Vector2(ox, oy); originalSize = new Vector2(ow, oh); semi = frameDict.objectForKey <bool>("semi"); } 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; textureRotated = rotated; spriteOffset = offset; originalSize = sourceSize; semi = frameDict.objectForKey <bool>("semi"); } 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); textureRotated = textureRotated_; spriteOffset = spriteOffset_; originalSize = spriteSourceSize; semi = frameDict.objectForKey <bool>("semi"); } if (textureRotated) { rect.size = new Vector2(rect.size.y, rect.size.x); } rect.y = textureSize.y - rect.y - rect.height; // add sprite frame CCSpriteFrame spriteFrame = new CCSpriteFrame(texture, rect, textureRotated, spriteOffset, originalSize, semi); spriteFrame.frameFileName = frameDictKey; spriteFrame.textureFilename = textureFileName; _spriteFrames [frameDictKey] = spriteFrame; } }
static void CheckSemiTransparentSprite(NSDictionary dictionary, Texture2D texture) { NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); // get the format int format = 0; if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); } // get texture size Vector2 textureSize = new Vector2(texture.width, texture.height); // check the format NSUtils.Assert(format >= 0 && format <= 3, @"cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:"); // SpriteFrame info Rect rect = new Rect(); bool textureRotated = false; // add real frames var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; 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); textureRotated = 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; textureRotated = 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); textureRotated = textureRotated_; } if (textureRotated) { rect.size = new Vector2(rect.size.y, rect.size.x); } rect.y = textureSize.y - rect.y - rect.height; // add sprite frame int rectX = Mathf.RoundToInt(rect.xMin); int rectY = Mathf.RoundToInt(rect.yMin); int rectW = Mathf.RoundToInt(rect.width); int rectH = Mathf.RoundToInt(rect.height); bool isSemi = false; for (int x = 0; x < rectW; x++) { for (int y = 0; y < rectH; y++) { Color color = texture.GetPixel(rectX + x, rectY + y); if (FloatUtils.Big(color.a, 0) && FloatUtils.Small(color.a, 1)) { isSemi = true; break; } } if (isSemi) { break; } } frameDict.Add("semi", isSemi); } }
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); }
public bool recycleGear(string category, CCFactoryGear gear, bool constraint = false) { if (gear.gameObject.transform.childCount != 0) { CCDebug.Warning("CCFactory try to recyle a not empty gear: {0}-{1}.", gear.gameObject, gear.gameObject.transform.GetChild(0)); DestroyObject(gear.gameObject); return(false); } Storage storage = getStorage(category, true); if (storage.componentTypeNames == null) { string[] componentTypeNames = new string[gear.components.Length]; for (int i = 0; i < componentTypeNames.Length; i++) { componentTypeNames [i] = gear.components [i].GetType().FullName; } storage.componentTypeNames = componentTypeNames; } else if (constraint) { if (storage.componentTypeNames.Length != gear.components.Length) { return(false); } else { HashSet <string> typeSet = new HashSet <string>(storage.componentTypeNames); int count = gear.components.Length; for (int i = 0; i < count; i++) { Component com = gear.components[i]; string tName = com.GetType().FullName; if (!typeSet.Remove(tName)) { return(false); } } } } gear.gameObject.layer = LayerMask.NameToLayer("Default"); gear.gameObject.transform.SetParent(this.transform); gear.gameObject.transform.localEulerAngles = Vector3.zero; gear.gameObject.transform.localScale = new Vector3(1, 1, 1); gear.gameObject.transform.localPosition = Vector3.zero; gear.gameObject.name = string.Format("{0}-{1}", category, storage.gears.Count); gear.gameObject.hideFlags = HideFlags.HideInHierarchy; gear.gameObject.SetActive(false); storage.gears.Add(gear); if (gear.gameObject.transform.parent == null) { NSUtils.Assert(gear.gameObject.transform.parent != null, "Recyle# set parent fail!"); } return(true); }