private void AssignTagTo(GameObject gameObject, XElement xml, ImportBehaviour importComponent) { string tag = ImportUtils.GetAttributeAsString(xml, "tag", ""); if (String.IsNullOrEmpty(tag)) { return; } // Let the user know if the tag doesn't exist in our project sttings try { gameObject.tag = tag; } catch (UnityException) { importComponent.RecordError("Tag '{0}' is not defined for '{1}'. Check project settings in Edit->Project Settings->Tags & Layers", tag, GetFullGameObjectName(gameObject.transform)); } }
private void AssignLayerTo(GameObject gameObject, XElement xml, ImportBehaviour importComponent) { string layerName = ImportUtils.GetAttributeAsString(xml, "layer", ""); if (String.IsNullOrEmpty(layerName)) { return; } int layerId = LayerMask.NameToLayer(layerName); if (layerId == -1) { importComponent.RecordError("Layer '{0}' is not defined for '{1}'. Check project settings in Edit->Project Settings->Tags & Layers", layerName, GetFullGameObjectName(gameObject.transform)); return; } // Set the layer on ourselves (and our children) AssignLayerIdTo(gameObject, layerId); }
private void AddGameObjectsTo(GameObject parent, XElement xml, bool isParentTrigger, ImportBehaviour importComponent, IList <ICustomTiledImporter> customImporters) { foreach (XElement goXml in xml.Elements("GameObject")) { string name = ImportUtils.GetAttributeAsString(goXml, "name", ""); string copyFrom = ImportUtils.GetAttributeAsString(goXml, "copy", ""); string mesh = ImportUtils.GetAttributeAsString(goXml, "mesh", ""); GameObject child = null; if (!String.IsNullOrEmpty(mesh)) { child = CreateGameObjectWithMesh(mesh, importComponent); } else if (!String.IsNullOrEmpty(copyFrom)) { child = CreateCopyFromMeshObj(copyFrom, importComponent); } else { child = new GameObject(); } if (child == null) { importComponent.RecordError("Error creating child object '{0}'", name); return; } // Assign the given name to our child if (!String.IsNullOrEmpty(name)) { child.name = name; } // Assign the child to the parent child.transform.parent = parent.transform; // Set the position float x = ImportUtils.GetAttributeAsFloat(goXml, "x", 0); float y = ImportUtils.GetAttributeAsFloat(goXml, "y", 0); float z = ImportUtils.GetAttributeAsFloat(goXml, "z", 0); child.transform.localPosition = new Vector3(x, y, z); // Add any layer components AddTileLayerComponentsTo(child, goXml); AddObjectLayerComponentsTo(child, goXml); AddGroupLayerComponentsTo(child, goXml); // Add any object group items AddTmxObjectComponentsTo(child, goXml); AddRectangleObjectComponentsTo(child, goXml); AddCircleObjectComponentsTo(child, goXml); AddPolygonObjectComponentsTo(child, goXml); AddPolylineObjectComponentsTo(child, goXml); AddTileObjectComponentsTo(child, goXml); // Add any tile animators AddTileAnimatorsTo(child, goXml); // Do we have any collision data? // Check if we are setting 'isTrigger' for ourselves or for our children bool isTrigger = ImportUtils.GetAttributeAsBoolean(goXml, "isTrigger", isParentTrigger); AddCollidersTo(child, isTrigger, goXml); // Do we have any children of our own? AddGameObjectsTo(child, goXml, isTrigger, importComponent, customImporters); // Does this game object have a tag? AssignTagTo(child, goXml, importComponent); // Does this game object have a layer? AssignLayerTo(child, goXml, importComponent); // Assign from various attributes AssignOpacityTo(child, goXml, importComponent); AssignSortingLayerNameTo(child, goXml, importComponent); AssignSortingOrderTo(child, goXml, importComponent); // Set scale and rotation *after* children are added otherwise Unity will have child+parent transform cancel each other out float sx = ImportUtils.GetAttributeAsFloat(goXml, "scaleX", 1.0f); float sy = ImportUtils.GetAttributeAsFloat(goXml, "scaleY", 1.0f); child.transform.localScale = new Vector3(sx, sy, 1.0f); // Set the rotation // Use negative rotation on the z component because of change in coordinate systems between Tiled and Unity Vector3 localRotation = new Vector3(); localRotation.z = -ImportUtils.GetAttributeAsFloat(goXml, "rotation", 0); child.transform.eulerAngles = localRotation; // Are there any custom properties? (This comes last - after all transformations have taken place.) HandleCustomProperties(child, goXml, customImporters); } }
private void AddGameObjectsTo(GameObject parent, XElement xml, bool isParentTrigger, string objPath, ImportBehaviour importComponent, IList <ICustomTiledImporter> customImporters) { foreach (XElement goXml in xml.Elements("GameObject")) { string name = ImportUtils.GetAttributeAsString(goXml, "name", ""); string copyFrom = ImportUtils.GetAttributeAsString(goXml, "copy", ""); GameObject child = null; if (!String.IsNullOrEmpty(copyFrom)) { float opacity = ImportUtils.GetAttributeAsFloat(goXml, "opacity", 1); child = CreateCopyFromMeshObj(copyFrom, objPath, opacity, importComponent); if (child == null) { // We're in trouble. Errors should already be in the log. return; } // Apply the sorting to the renderer of the mesh object we just copied into the child Renderer renderer = child.GetComponent <Renderer>(); string sortingLayer = ImportUtils.GetAttributeAsString(goXml, "sortingLayerName", ""); if (!String.IsNullOrEmpty(sortingLayer) && !SortingLayerExposedEditor.GetSortingLayerNames().Contains(sortingLayer)) { importComponent.RecordError("Sorting Layer \"{0}\" does not exist. Check your Project Settings -> Tags and Layers", sortingLayer); renderer.sortingLayerName = "Default"; } else { renderer.sortingLayerName = sortingLayer; } // Set the sorting order renderer.sortingOrder = ImportUtils.GetAttributeAsInt(goXml, "sortingOrder", 0); } else { child = new GameObject(); } if (!String.IsNullOrEmpty(name)) { child.name = name; } // Assign the child to the parent child.transform.parent = parent.transform; // Set the position float x = ImportUtils.GetAttributeAsFloat(goXml, "x", 0); float y = ImportUtils.GetAttributeAsFloat(goXml, "y", 0); float z = ImportUtils.GetAttributeAsFloat(goXml, "z", 0); child.transform.localPosition = new Vector3(x, y, z); // Add any layer components AddTileLayerComponentsTo(child, goXml); AddObjectLayerComponentsTo(child, goXml); AddGroupLayerComponentsTo(child, goXml); // Add any object group items AddTmxObjectComponentsTo(child, goXml); AddRectangleObjectComponentsTo(child, goXml); AddCircleObjectComponentsTo(child, goXml); AddPolygonObjectComponentsTo(child, goXml); AddPolylineObjectComponentsTo(child, goXml); AddTileObjectComponentsTo(child, goXml); // Add any tile animators AddTileAnimatorsTo(child, goXml); // Do we have any collision data? // Check if we are setting 'isTrigger' for ourselves or for our childen bool isTrigger = ImportUtils.GetAttributeAsBoolean(goXml, "isTrigger", isParentTrigger); AddCollidersTo(child, isTrigger, goXml); // Do we have any children of our own? AddGameObjectsTo(child, goXml, isTrigger, objPath, importComponent, customImporters); // Does this game object have a tag? AssignTagTo(child, goXml, importComponent); // Does this game object have a layer? AssignLayerTo(child, goXml, importComponent); // Set scale and rotation *after* children are added otherwise Unity will have child+parent transform cancel each other out float sx = ImportUtils.GetAttributeAsFloat(goXml, "scaleX", 1.0f); float sy = ImportUtils.GetAttributeAsFloat(goXml, "scaleY", 1.0f); child.transform.localScale = new Vector3(sx, sy, 1.0f); // Set the rotation // Use negative rotation on the z component because of change in coordinate systems between Tiled and Unity Vector3 localRotation = new Vector3(); localRotation.z = -ImportUtils.GetAttributeAsFloat(goXml, "rotation", 0); child.transform.eulerAngles = localRotation; // Are there any custom properties? (This comes last - after all transformations have taken place.) HandleCustomProperties(child, goXml, customImporters); } }