private void AssignTiledProperties(TmxHasProperties tmxHasProperties, XElement xml) { var properties = TmxHelper.GetPropertiesWithTypeDefaults(tmxHasProperties, this.tmxMap.ObjectTypes); List <XElement> xmlProperties = new List <XElement>(); foreach (var prop in properties.PropertyMap) { // Ignore properties that start with "unity:" if (prop.Key.StartsWith("unity:")) { continue; } var alreadyProperty = from p in xml.Elements("Property") where p.Attribute("name") != null where p.Attribute("name").Value == prop.Key select p; if (alreadyProperty.Count() > 0) { // Don't override property that is already there continue; } XElement xmlProp = new XElement("Property", new XAttribute("name", prop.Key), new XAttribute("value", prop.Value.Value)); xmlProperties.Add(xmlProp); } xml.Add(xmlProperties); }
private void AssignUnityProperties(TmxHasProperties tmxHasProperties, XElement xml, PrefabContext context) { var properties = TmxHelper.GetPropertiesWithTypeDefaults(tmxHasProperties, this.tmxMap.ObjectTypes); // Only the root of the prefab can have a scale { string unityScale = properties.GetPropertyValueAsString("unity:scale", ""); if (!String.IsNullOrEmpty(unityScale)) { float scale = 1.0f; if (context != PrefabContext.Root) { Logger.WriteWarning("unity:scale only applies to map properties\n{0}", xml.ToString()); } else if (!Single.TryParse(unityScale, out scale)) { Logger.WriteError("unity:scale property value '{0}' could not be converted to a float", unityScale); } else { xml.SetAttributeValue("scale", unityScale); } } } // Only the root of the prefab can be marked a resource { string unityResource = properties.GetPropertyValueAsString("unity:resource", ""); if (!String.IsNullOrEmpty(unityResource)) { bool resource = false; if (context != PrefabContext.Root) { Logger.WriteWarning("unity:resource only applies to map properties\n{0}", xml.ToString()); } else if (!Boolean.TryParse(unityResource, out resource)) { Logger.WriteError("unity:resource property value '{0}' could not be converted to a boolean", unityResource); } else { xml.SetAttributeValue("resource", unityResource); } } } // Some users may want resource prefabs to be saved to a particular path { string unityResourcePath = properties.GetPropertyValueAsString("unity:resourcePath", ""); if (!String.IsNullOrEmpty(unityResourcePath)) { if (context != PrefabContext.Root) { Logger.WriteWarning("unity:resourcePath only applies to map properties\n{0}", xml.ToString()); } else { bool isInvalid = Path.GetInvalidPathChars().Any(c => unityResourcePath.Contains(c)); if (isInvalid) { Logger.WriteError("unity:resourcePath has invalid path characters: {0}", unityResourcePath); } else { xml.SetAttributeValue("resourcePath", unityResourcePath); } } } } // Any object can carry the 'isTrigger' setting and we assume any children to inherit the setting { string unityIsTrigger = properties.GetPropertyValueAsString("unity:isTrigger", ""); if (!String.IsNullOrEmpty(unityIsTrigger)) { bool isTrigger = false; if (!Boolean.TryParse(unityIsTrigger, out isTrigger)) { Logger.WriteError("unity:isTrigger property value '{0}' cound not be converted to a boolean", unityIsTrigger); } else { xml.SetAttributeValue("isTrigger", unityIsTrigger); } } } // Any part of the prefab can be assigned a 'layer' { string unityLayer = properties.GetPropertyValueAsString("unity:layer", ""); if (!String.IsNullOrEmpty(unityLayer)) { xml.SetAttributeValue("layer", unityLayer); } } // Any part of the prefab can be assigned a 'tag' { string unityTag = properties.GetPropertyValueAsString("unity:tag", ""); if (!String.IsNullOrEmpty(unityTag)) { xml.SetAttributeValue("tag", unityTag); } } List <String> knownProperties = new List <string>(); knownProperties.Add("unity:layer"); knownProperties.Add("unity:tag"); knownProperties.Add("unity:sortingLayerName"); knownProperties.Add("unity:sortingOrder"); knownProperties.Add("unity:scale"); knownProperties.Add("unity:isTrigger"); knownProperties.Add("unity:convex"); knownProperties.Add("unity:ignore"); knownProperties.Add("unity:resource"); knownProperties.Add("unity:resourcePath"); var unknown = from p in properties.PropertyMap where p.Key.StartsWith("unity:") where knownProperties.Contains(p.Key) == false select p.Key; foreach (var p in unknown) { Logger.WriteWarning("Unknown unity property '{0}' in GameObject '{1}'", p, tmxHasProperties.ToString()); } }