/// <summary> /// Applies to gameObject any custom X-UniTMX properties present on obj /// </summary> /// <param name="gameObject">GameObject to apply custom properties to</param> /// <param name="obj">MapObject to read custom properties from</param> public void ApplyCustomProperties(GameObject gameObject, MapObject obj) { // nothing to do here... if (gameObject == null || obj == null) return; // Set a layer number for gameObject if (obj.HasProperty(Property_Layer)) gameObject.layer = obj.GetPropertyAsInt(Property_Layer); if (obj.HasProperty(Property_LayerName)) gameObject.layer = LayerMask.NameToLayer(obj.GetPropertyAsString(Property_LayerName)); // Add a tag for gameObject if (obj.HasProperty(Property_Tag)) gameObject.tag = obj.GetPropertyAsString(Property_Tag); // Add Components for this gameObject int c = 1; while (obj.HasProperty(Property_AddComponent + c)) { UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/X-UniTMX/Code/Map.cs (2684,5)", obj.GetPropertyAsString(Property_AddComponent + c)); c++; } c = 1; while (obj.HasProperty(Property_SendMessage + c)) { string messageToSend = obj.GetPropertyAsString(Property_SendMessage + c); string[] menssage = messageToSend.Split('|'); if (menssage.Length == 2) { gameObject.BroadcastMessage(menssage[0], menssage[1]); } if (menssage.Length == 1) { gameObject.BroadcastMessage(menssage[0]); } c++; } if (gameObject.GetComponent<Renderer>() != null) { if (obj.HasProperty(Property_SortingLayerName)) gameObject.GetComponent<Renderer>().sortingLayerName = obj.GetPropertyAsString(Property_SortingLayerName); if (obj.HasProperty(Property_SortingOrder)) gameObject.GetComponent<Renderer>().sortingOrder = obj.GetPropertyAsInt(Property_SortingOrder); if (obj.HasProperty(Property_SetMaterialColor)) { string[] splitColor = obj.GetPropertyAsString(Property_SetMaterialColor).Split(','); if (splitColor.Length >= 1) { gameObject.GetComponent<Renderer>().material = new Material(BaseTileMaterial); gameObject.GetComponent<Renderer>().material.color = new Color32( ((byte)(int.Parse(string.IsNullOrEmpty(splitColor[0]) ? "255" : splitColor[0]))), splitColor.Length >= 2 ? ((byte)(int.Parse(splitColor[1]))) : (byte)255, splitColor.Length >= 3 ? ((byte)(int.Parse(splitColor[2]))) : (byte)255, splitColor.Length >= 4 ? ((byte)(int.Parse(splitColor[3]))) : (byte)255); } } } }
/// <summary> /// Generate a prefab based in object colider layer /// </summary> /// <param name="obj">Object which properties will be used to generate a prefab.</param> /// <param name="newColliderObject">if null add relative parent object,.</param> /// <returns>Generated Game Object containing the Collider.</returns> public void AddPrefabs(MapObject obj, GameObject newColliderObject = null, bool is2DColliders = false, bool addTileName = true) { int indexPrefab = 0; while ("".Equals(obj.GetPropertyAsString(indexPrefab + "-prefab")) == false) { string prefabName = obj.GetPropertyAsString(indexPrefab + "-prefab"); string baseResourcePath = obj.GetPropertyAsString(indexPrefab + "-prefab path"); UnityEngine.Object resourceObject = Resources.Load(baseResourcePath + prefabName); Resources.UnloadUnusedAssets(); if (resourceObject != null) { float zDepth = obj.GetPropertyAsFloat(indexPrefab + "-prefab z depth"); GameObject newPrefab = UnityEngine.Object.Instantiate(resourceObject) as GameObject; //if (newColliderObject == null) //{ // newPrefab.transform.parent = MapObject.transform; // newPrefab.transform.localPosition = new Vector3(obj.Bounds.center.x, -obj.Bounds.center.y, zDepth); //} //else //{ // newPrefab.transform.parent = newColliderObject.transform; // newPrefab.transform.localPosition = new Vector3(0, 0, zDepth); //} newPrefab.transform.parent = MapObject.transform; newPrefab.transform.localPosition = new Vector3(obj.Bounds.center.x, -obj.Bounds.center.y, zDepth); // copy coliders from newColliderObject // only copy if type of this object is diferent of "NoCollider" if ("NoCollider".Equals(obj.Type) == false) { if (obj.GetPropertyAsBoolean(indexPrefab + "-prefab add collider")) { CopyCollider(obj, ref newColliderObject, ref newPrefab, is2DColliders); } } newPrefab.name = (addTileName ? (_mapName + "_") : "") + obj.Name; int indexMessage = 0; string prefabMensage = obj.GetPropertyAsString(indexPrefab + "-prefab sendmessage " + indexMessage); while ("".Equals(prefabMensage) == false) { string[] menssage = prefabMensage.Split(new[] { '|' }, StringSplitOptions.None); if (menssage.Length == 2) { newPrefab.BroadcastMessage(menssage[0], menssage[1]); } indexMessage++; prefabMensage = obj.GetPropertyAsString(indexPrefab + "-prefab sendmessage " + indexMessage); } } else { Debug.LogError("Prefab doesn't exist at: Resources/" + baseResourcePath + prefabName); } indexPrefab++; } }
/// <summary> /// Generate a prefab based in object colider layer /// </summary> /// <param name="obj">Object which properties will be used to generate a prefab.</param> /// <param name="newColliderObject">if null add relative parent object,.</param> /// <param name="addTileName">true to add Map's name to the prefab name</param> /// <param name="is2DColliders">true to generate 2D colliders</param> /// <returns>Generated Game Object containing the Collider.</returns> public void AddPrefabs(MapObject obj, GameObject newColliderObject = null, bool is2DColliders = false, bool addTileName = true) { int indexPrefab = 0; while (obj.HasProperty(string.Concat(indexPrefab.ToString(), Property_PrefabName))) { string prefabName = obj.GetPropertyAsString(indexPrefab + Property_PrefabName); string baseResourcePath = obj.GetPropertyAsString(indexPrefab + Property_PrefabPath); UnityEngine.Object resourceObject = Resources.Load(baseResourcePath + prefabName); Resources.UnloadUnusedAssets(); if (resourceObject != null) { float zDepth = obj.GetPropertyAsFloat(indexPrefab + Property_PrefabZDepth); GameObject newPrefab = UnityEngine.Object.Instantiate(resourceObject) as GameObject; newPrefab.transform.parent = obj.ParentObjectLayer != null ? obj.ParentObjectLayer.LayerGameObject.transform : MapObject.transform; newPrefab.transform.localPosition = TiledPositionToWorldPoint(new Vector3(obj.Bounds.center.x, obj.Bounds.center.y, zDepth)); // copy coliders from newColliderObject // only copy if type of this object is diferent of "NoCollider" if (obj.Type.Equals(Object_Type_NoCollider) == false) { if (obj.GetPropertyAsBoolean(indexPrefab + Property_PrefabAddCollider)) { //CopyCollider(obj, ref newColliderObject, ref newPrefab, is2DColliders); AddCollider(newPrefab, obj, obj.Type.Equals(Object_Type_Trigger), is2DColliders, zDepth); } else // since custom properties are automatically added when a collider is added but this prefab has no collider, we must enforce them to be parsed ApplyCustomProperties(newPrefab, obj); } else // since custom properties are automatically added when a collider is added but this prefab has no collider, we must enforce them to be parsed ApplyCustomProperties(newPrefab, obj); if (obj.GetPropertyAsBoolean(indexPrefab + Property_PrefabFixColliderPosition)) { // Mario: Fixed wrong position in instantiate prefabs if(newColliderObject != null && newPrefab != null) newPrefab.transform.position = newColliderObject.transform.position; } newPrefab.name = (addTileName ? (_mapName + "_") : "") + obj.Name; int indexMessage = 1; string prefabMensage = obj.GetPropertyAsString(indexPrefab + Property_PrefabSendMessage + indexMessage); while (string.IsNullOrEmpty(prefabMensage) == false) { string[] menssage = prefabMensage.Split(new[] { '|' }, StringSplitOptions.None); if (menssage.Length == 2) { newPrefab.BroadcastMessage(menssage[0], menssage[1]); } if (menssage.Length == 1) { newPrefab.BroadcastMessage(menssage[0]); } indexMessage++; prefabMensage = obj.GetPropertyAsString(indexPrefab + Property_PrefabSendMessage + indexMessage); } } else { Debug.LogError("Prefab doesn't exist at: Resources/" + baseResourcePath + prefabName); } indexPrefab++; } }
/// <summary> /// Generate a prefab based in object layer /// </summary> /// <param name="obj">Object which properties will be used to generate a prefab.</param> /// <param name="parent">if null add relative parent object,.</param> /// <param name="addMapName">true to add Map's name to the prefab name</param> /// <returns>Generated GameObject from the Prefab.</returns> public static GameObject GeneratePrefab(this Map map, MapObject obj, Vector2 anchorPointValue, GameObject parent = null, bool addMapName = true, bool setNameAsObjectName = false) { if (obj.HasProperty(Map.Property_PrefabName)) { string prefabName = obj.GetPropertyAsString(Map.Property_PrefabName); string baseResourcePath = obj.GetPropertyAsString(Map.Property_PrefabPath); UnityEngine.Object resourceObject = Resources.Load(baseResourcePath + prefabName); Resources.UnloadUnusedAssets(); if (resourceObject != null) { float zDepth = obj.GetPropertyAsFloat(Map.Property_PrefabZDepth); GameObject newPrefab = UnityEngine.Object.Instantiate(resourceObject) as GameObject; newPrefab.transform.parent = obj.ParentObjectLayer != null ? obj.ParentObjectLayer.LayerGameObject.transform : map.MapGameObject.transform; newPrefab.transform.localPosition = map.TiledPositionToWorldPoint( new Vector3(obj.Bounds.xMin + obj.Bounds.width * anchorPointValue.x, obj.Bounds.yMin + obj.Bounds.height * anchorPointValue.y, zDepth)); if (obj.HasProperty(Map.Property_PrefabAddCollider)) { string colliderType = obj.GetPropertyAsString(Map.Property_PrefabAddCollider); AddCollider(map, newPrefab, obj, !colliderType.Contains("3")); } // since custom properties are automatically added only when a collider is added, we must enforce them to be parsed ApplyCustomProperties(newPrefab, obj); if (parent) newPrefab.transform.parent = parent.transform; if (setNameAsObjectName) newPrefab.name = obj.Name; if (addMapName) newPrefab.name = string.Concat(map.MapName, "_", newPrefab.name); obj.LinkedGameObject = newPrefab; return newPrefab; } else { Debug.LogError("Prefab doesn't exist at: Resources/" + baseResourcePath + prefabName); } } return null; }
public void Init(MapObject door) { if (door.HasProperty("name")) name = door.GetPropertyAsString("name"); Out = Helper.TranslateDirection(door.GetPropertyAsString("out")); In = Helper.TranslateDirection(door.GetPropertyAsString("in")); ForceOut = door.HasProperty("force out") ? door.GetPropertyAsFloat("force out") : 1; ForceIn = door.HasProperty("force in") ? door.GetPropertyAsFloat("force in") : 1; GoTo = door.GetPropertyAsString("go to"); Scene = door.GetPropertyAsString("scene"); }
/// <summary> /// Generate a Polyline collider mesh /// </summary> /// <param name="obj">Object which properties will be used to generate this collider.</param> /// <param name="zDepth">Z Depth of the collider.</param> /// <param name="colliderWidth">Width of the collider.</param> /// <param name="innerCollision">If true, calculate normals facing the center of the collider (inside collisions), else, outside collisions.</param> /// <returns>Generated Game Object containing the Collider.</returns> public GameObject GeneratePolylineCollider(MapObject obj, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false) { GameObject polylineCollider = new GameObject(obj.Name); polylineCollider.transform.parent = this.Parent.transform; Mesh colliderMesh = new Mesh(); MeshCollider mc = polylineCollider.AddComponent<MeshCollider>(); List<Vector3> vertices = new List<Vector3>(); List<int> triangles = new List<int>(); Vector3 firstPoint = (Vector3)obj.Points[0]; for(int i = 1; i < obj.Points.Count; i++) { Vector3 secondPoint = (Vector3)obj.Points[i]; Vector3 firstFront = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth - colliderWidth); Vector3 firstBack = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth + colliderWidth); Vector3 secondFront = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth - colliderWidth); Vector3 secondBack = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth + colliderWidth); if (innerCollision) { vertices.Add(firstBack); // 3 vertices.Add(firstFront); // 2 vertices.Add(secondBack); // 1 vertices.Add(secondFront); // 0 } else { vertices.Add(firstFront); // 3 vertices.Add(firstBack); // 2 vertices.Add(secondFront); // 1 vertices.Add(secondBack); // 2 } triangles.Add((i - 1) * 4 + 3); triangles.Add((i - 1) * 4 + 2); triangles.Add((i - 1) * 4 + 0); triangles.Add((i - 1) * 4 + 0); triangles.Add((i - 1) * 4 + 1); triangles.Add((i - 1) * 4 + 3); firstPoint = secondPoint; } colliderMesh.vertices = vertices.ToArray(); colliderMesh.triangles = triangles.ToArray(); colliderMesh.RecalculateNormals(); mc.sharedMesh = colliderMesh; polylineCollider.isStatic = true; polylineCollider.tag = "soundStopper"; polylineCollider.layer = 8; Rigidbody _rigid = polylineCollider.AddComponent<Rigidbody>(); _rigid.isKinematic = false; _rigid.useGravity = true; _rigid.constraints = RigidbodyConstraints.FreezeAll; _rigid.angularDrag = 0.000001f; _rigid.mass = 0.000001f; Environment _env = polylineCollider.AddComponent<Environment>(); _env.typeImport = obj.GetPropertyAsString("env"); return polylineCollider; }
/// <summary> /// Generate a Pebble collider mesh /// </summary> /// <param name="obj">Object which properties will be used to generate this collider.</param> /// <param name="zDepth">Z Depth of the collider.</param> /// <returns>Generated Game Object containing the Collider.</returns> public GameObject GeneratePebbleCollider(MapObject obj, float zDepth = 0, float colliderWidth = 30f) { GameObject boxCollider = new GameObject("PebbleCollider"); BoxCollider bx = boxCollider.AddComponent<BoxCollider>(); boxCollider.transform.parent = this.Parent.transform; // bx.center = new Vector3(obj.Bounds.center.x, -obj.Bounds.center.y, zDepth); // bx.size = new Vector3(obj.Bounds.width, obj.Bounds.height, colliderWidth); boxCollider.transform.position = new Vector3(obj.Bounds.center.x, -obj.Bounds.center.y, 0f); bx.center = new Vector3(0f,0f, zDepth); bx.size = new Vector3(obj.Bounds.width, obj.Bounds.height, colliderWidth); BoxCollider bx2 = boxCollider.AddComponent<BoxCollider>(); bx2.center = new Vector3(0f,0f, zDepth); bx2.size = new Vector3(obj.Bounds.width, obj.Bounds.height, colliderWidth); bx2.isTrigger = true; boxCollider.isStatic = true; boxCollider.tag = "pebbleKiller"; boxCollider.layer = 8; Rigidbody _rigid = boxCollider.AddComponent<Rigidbody>(); _rigid.isKinematic = true; _rigid.useGravity = false; _rigid.constraints = RigidbodyConstraints.FreezeAll; Environment _env = boxCollider.AddComponent<Environment>(); _env.typeImport = obj.GetPropertyAsString("env"); return boxCollider; }