/// <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++; } }
private void AddPolylineCollider3D(GameObject gameObject, MapObject obj, bool isTrigger = false, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false, bool createRigidbody = false, bool rigidbodyIsKinematic = true) { Mesh colliderMesh = new Mesh(); colliderMesh.name = "Collider_" + obj.Name; MeshCollider mc = gameObject.AddComponent<MeshCollider>(); mc.isTrigger = isTrigger || obj.Type.Equals(Object_Type_Trigger); List<Vector3> vertices = new List<Vector3>(); List<int> triangles = new List<int>(); GenerateVerticesAndTris(obj.Points, vertices, triangles, zDepth, colliderWidth, innerCollision); colliderMesh.vertices = vertices.ToArray(); colliderMesh.uv = new Vector2[colliderMesh.vertices.Length]; colliderMesh.uv2 = colliderMesh.uv; colliderMesh.uv2 = colliderMesh.uv; colliderMesh.triangles = triangles.ToArray(); colliderMesh.RecalculateNormals(); mc.sharedMesh = colliderMesh; if (createRigidbody) { gameObject.AddComponent<Rigidbody>(); gameObject.GetComponent<Rigidbody>().isKinematic = rigidbodyIsKinematic; } if (obj.Rotation != 0) gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward); if (obj.GetPropertyAsBoolean(Property_CreateMesh)) { if (gameObject.GetComponent<MeshFilter>() == null) gameObject.AddComponent<MeshFilter>(); if (gameObject.GetComponent<MeshRenderer>() == null) gameObject.AddComponent<MeshRenderer>(); MeshFilter _meshFilter = gameObject.GetComponent<MeshFilter>(); if (mc != null) { mc.sharedMesh.RecalculateBounds(); mc.sharedMesh.RecalculateNormals(); MathfExtensions.CalculateMeshTangents(mc.sharedMesh); _meshFilter.sharedMesh = mc.sharedMesh; } } ApplyCustomProperties(gameObject, obj); }
/// <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++; } }
private void AddEllipseCollider3D(GameObject gameObject, MapObject obj, bool isTrigger = false, float zDepth = 0, float colliderWidth = 1.0f, bool createRigidbody = false, bool rigidbodyIsKinematic = true) { GameObject gameObjectMesh = null; if (Orientation != X_UniTMX.Orientation.Isometric && obj.Bounds.width == obj.Bounds.height) { CapsuleCollider cc = null; if (obj.GetPropertyAsBoolean(Property_CreateMesh)) { gameObjectMesh = GameObject.CreatePrimitive(PrimitiveType.Capsule); gameObjectMesh.name = obj.Name; gameObjectMesh.transform.parent = gameObject.transform; gameObjectMesh.transform.localPosition = new Vector3(obj.Bounds.height / 2.0f, -obj.Bounds.width / 2.0f); cc = gameObjectMesh.GetComponent<Collider>() as CapsuleCollider; gameObjectMesh.GetComponent<Collider>().isTrigger = isTrigger || obj.Type.Equals(Object_Type_Trigger); gameObjectMesh.transform.localScale = new Vector3(obj.Bounds.width, colliderWidth, obj.Bounds.height); gameObjectMesh.transform.localRotation = Quaternion.AngleAxis(90, Vector3.right); } else { cc = gameObject.AddComponent<CapsuleCollider>(); cc.isTrigger = isTrigger || obj.Type.Equals(Object_Type_Trigger); cc.center = new Vector3(obj.Bounds.height / 2.0f, -obj.Bounds.width / 2.0f); cc.direction = 0; cc.radius = obj.Bounds.height / 2.0f; cc.height = obj.Bounds.width; } } else { ApproximateEllipse3D(gameObject, obj, isTrigger, zDepth, colliderWidth, createRigidbody, rigidbodyIsKinematic); } if (createRigidbody) { gameObject.AddComponent<Rigidbody>(); gameObject.GetComponent<Rigidbody>().isKinematic = rigidbodyIsKinematic; } if (obj.Rotation != 0) gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward); if(gameObjectMesh) ApplyCustomProperties(gameObjectMesh, obj); else ApplyCustomProperties(gameObject, obj); }
private void AddBoxCollider3D(GameObject gameObject, MapObject obj, bool isTrigger = false, float zDepth = 0, float colliderWidth = 1.0f, bool createRigidbody = false, bool rigidbodyIsKinematic = true) { GameObject gameObjectMesh = null; // Orthogonal and Staggered maps can use BoxCollider, Isometric maps must use polygon collider if (Orientation != X_UniTMX.Orientation.Isometric) { if (obj.GetPropertyAsBoolean(Property_CreateMesh)) { gameObjectMesh = GameObject.CreatePrimitive(PrimitiveType.Cube); gameObjectMesh.name = obj.Name; gameObjectMesh.transform.parent = gameObject.transform; gameObjectMesh.transform.localPosition = Vector3.zero; gameObjectMesh.GetComponent<Collider>().isTrigger = isTrigger || obj.Type.Equals(Object_Type_Trigger); } else { gameObject.AddComponent<BoxCollider>(); gameObject.GetComponent<Collider>().isTrigger = isTrigger || obj.Type.Equals(Object_Type_Trigger); } gameObject.transform.localScale = new Vector3(obj.Bounds.width, obj.Bounds.height, colliderWidth); } else { List<Vector2> points = new List<Vector2>(); points.Add(new Vector2(obj.Bounds.xMin - obj.Bounds.x, obj.Bounds.yMax - obj.Bounds.y)); points.Add(new Vector2(obj.Bounds.xMin - obj.Bounds.x, obj.Bounds.yMin - obj.Bounds.y)); points.Add(new Vector2(obj.Bounds.xMax - obj.Bounds.x, obj.Bounds.yMin - obj.Bounds.y)); points.Add(new Vector2(obj.Bounds.xMax - obj.Bounds.x, obj.Bounds.yMax - obj.Bounds.y)); X_UniTMX.MapObject isoBox = new MapObject(obj.Name, obj.Type, obj.Bounds, obj.Properties, obj.GID, points, obj.Rotation, obj.ParentObjectLayer); AddPolygonCollider3D(gameObject, isoBox, isTrigger, zDepth, colliderWidth); //gameObject = GeneratePolygonCollider3D(isoBox, isTrigger, zDepth, colliderWidth); } if (createRigidbody) { gameObject.AddComponent<Rigidbody>(); gameObject.GetComponent<Rigidbody>().isKinematic = rigidbodyIsKinematic; } if (obj.Rotation != 0) gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward); if(gameObjectMesh != null) ApplyCustomProperties(gameObjectMesh, obj); else ApplyCustomProperties(gameObject, obj); }
public static void AddPolygonCollider3D(Map map, GameObject gameObject, MapObject obj, bool isTrigger = false, PhysicMaterial physicsMaterial = null, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false, bool createRigidbody = false, bool rigidbodyIsKinematic = true) { Mesh colliderMesh = new Mesh(); colliderMesh.name = "Collider_" + obj.Name; MeshCollider mc = gameObject.AddComponent<MeshCollider>(); mc.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger); List<Vector3> vertices = new List<Vector3>(); List<int> triangles = new List<int>(); GenerateVerticesAndTris(map, obj.Points, vertices, triangles, zDepth, colliderWidth, innerCollision); // Connect last point with first point (create the face between them) triangles.Add(vertices.Count - 1); triangles.Add(1); triangles.Add(0); triangles.Add(0); triangles.Add(vertices.Count - 2); triangles.Add(vertices.Count - 1); // Fill Faces FillFaces(obj.Points, triangles); colliderMesh.vertices = vertices.ToArray(); colliderMesh.uv = new Vector2[colliderMesh.vertices.Length]; //colliderMesh.uv1 = colliderMesh.uv; colliderMesh.uv2 = colliderMesh.uv; colliderMesh.triangles = triangles.ToArray(); colliderMesh.RecalculateNormals(); mc.sharedMesh = colliderMesh; if (physicsMaterial != null) mc.sharedMaterial = physicsMaterial; if (createRigidbody) { Rigidbody r = gameObject.AddComponent<Rigidbody>(); r.isKinematic = rigidbodyIsKinematic; } if (obj.Rotation != 0) { gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward); } if (obj.GetPropertyAsBoolean(Map.Property_CreateMesh)) { if (gameObject.GetComponent<MeshFilter>() == null) gameObject.AddComponent<MeshFilter>(); if (gameObject.GetComponent<MeshRenderer>() == null) gameObject.AddComponent<MeshRenderer>(); MeshFilter _meshFilter = gameObject.GetComponent<MeshFilter>(); if (mc != null) { mc.sharedMesh.RecalculateBounds(); mc.sharedMesh.RecalculateNormals(); MathfExtensions.CalculateMeshTangents(mc.sharedMesh); _meshFilter.sharedMesh = mc.sharedMesh; } } ApplyCustomProperties(gameObject, obj); // Link this collider to the MapObject obj.LinkedGameObject = gameObject; }
public static void AddEllipseCollider3D(Map map, GameObject gameObject, MapObject obj, bool isTrigger = false, PhysicMaterial physicsMaterial = null, float zDepth = 0, float colliderWidth = 1.0f, bool createRigidbody = false, bool rigidbodyIsKinematic = true) { GameObject gameObjectMesh = null; if (map.MapRenderParameter.Orientation != X_UniTMX.Orientation.Isometric && obj.Bounds.width == obj.Bounds.height) { CapsuleCollider cc = null; if (obj.GetPropertyAsBoolean(Map.Property_CreateMesh)) { gameObjectMesh = GameObject.CreatePrimitive(PrimitiveType.Capsule); gameObjectMesh.name = obj.Name; gameObjectMesh.transform.parent = gameObject.transform; gameObjectMesh.transform.localPosition = new Vector3(obj.Bounds.height / 2.0f, -obj.Bounds.width / 2.0f); cc = gameObjectMesh.GetComponent<CapsuleCollider>(); cc.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger); gameObjectMesh.transform.localScale = new Vector3(obj.Bounds.width, colliderWidth, obj.Bounds.height); gameObjectMesh.transform.localRotation = Quaternion.AngleAxis(90, Vector3.right); } else { cc = gameObject.AddComponent<CapsuleCollider>(); cc.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger); cc.center = new Vector3(obj.Bounds.height / 2.0f, -obj.Bounds.width / 2.0f); cc.direction = 0; cc.radius = obj.Bounds.height / 2.0f; cc.height = obj.Bounds.width; } if (physicsMaterial != null) cc.sharedMaterial = physicsMaterial; } else { ApproximateEllipse3D(map, gameObject, obj, isTrigger, physicsMaterial, zDepth, colliderWidth, createRigidbody, rigidbodyIsKinematic); } if (createRigidbody) { Rigidbody r = gameObject.AddComponent<Rigidbody>(); r.isKinematic = rigidbodyIsKinematic; } if (obj.Rotation != 0) gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward); if (gameObjectMesh) ApplyCustomProperties(gameObjectMesh, obj); else ApplyCustomProperties(gameObject, obj); // Link this collider to the MapObject obj.LinkedGameObject = gameObject; }
/// <summary> /// Adds a 3D BoxCollider to a GameObject /// </summary> /// <param name="gameObject">GameObject to add the collider</param> /// <param name="obj">MapObject which properties will be used to generate this collider</param> /// <param name="isTrigger">True for Trigger Collider, false otherwise</param> /// <param name="physicsMaterial">PhysicMaterial to be set to the collider</param> /// <param name="zDepth">Z Depth of the collider</param> /// <param name="colliderWidth">Width of the collider, in Units</param> /// <param name="createRigidbody">True to attach a Rigidbody to the created collider</param> /// <param name="rigidbodyIsKinematic">Sets if the attached rigidbody is kinematic or not</param> public static void AddBoxCollider3D(Map map, GameObject gameObject, MapObject obj, bool isTrigger = false, PhysicMaterial physicsMaterial = null, float zDepth = 0, float colliderWidth = 1.0f, bool createRigidbody = false, bool rigidbodyIsKinematic = true) { GameObject gameObjectMesh = null; // Orthogonal and Staggered maps can use BoxCollider, Isometric maps must use polygon collider if (map.MapRenderParameter.Orientation != X_UniTMX.Orientation.Isometric) { BoxCollider boxCollider = null; if (obj.GetPropertyAsBoolean(Map.Property_CreateMesh)) { gameObjectMesh = GameObject.CreatePrimitive(PrimitiveType.Cube); gameObjectMesh.name = obj.Name; gameObjectMesh.transform.parent = gameObject.transform; gameObjectMesh.transform.localPosition = new Vector3(0.5f, -0.5f); boxCollider = gameObjectMesh.GetComponent<BoxCollider>(); } else { boxCollider = gameObject.AddComponent<BoxCollider>(); boxCollider.center = new Vector3(0.5f, -0.5f); } boxCollider.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger); if (physicsMaterial != null) boxCollider.sharedMaterial = physicsMaterial; gameObject.transform.localScale = new Vector3(obj.Bounds.width, obj.Bounds.height, colliderWidth); } else { List<Vector2> points = new List<Vector2>(); points.Add(new Vector2(obj.Bounds.xMin - obj.Bounds.x, obj.Bounds.yMax - obj.Bounds.y)); points.Add(new Vector2(obj.Bounds.xMin - obj.Bounds.x, obj.Bounds.yMin - obj.Bounds.y)); points.Add(new Vector2(obj.Bounds.xMax - obj.Bounds.x, obj.Bounds.yMin - obj.Bounds.y)); points.Add(new Vector2(obj.Bounds.xMax - obj.Bounds.x, obj.Bounds.yMax - obj.Bounds.y)); X_UniTMX.MapObject isoBox = new MapObject(obj.Name, obj.Type, obj.Bounds, obj.Properties, obj.GID, points, obj.Rotation, obj.ParentObjectLayer); AddPolygonCollider3D(map, gameObject, isoBox, isTrigger, physicsMaterial, zDepth, colliderWidth); //gameObject = GeneratePolygonCollider3D(isoBox, isTrigger, zDepth, colliderWidth); } if (createRigidbody) { Rigidbody r = gameObject.AddComponent<Rigidbody>(); r.isKinematic = rigidbodyIsKinematic; } if (obj.Rotation != 0) gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward); if (gameObjectMesh != null) ApplyCustomProperties(gameObjectMesh, obj); else ApplyCustomProperties(gameObject, obj); // Link this collider to the MapObject obj.LinkedGameObject = gameObject; }