private void CreatePolygon(Polygon newPolygon) { GameObject newGameObject = new GameObject(); newGameObject.AddComponent <Rigidbody2D> (); newGameObject.transform.parent = transform; newGameObject.AddComponent <ColliderLineRenderer2D> ().color = Color.black; PolygonGenerator2D.GenerateCollider(newGameObject, newPolygon); Slicer2D smartSlicer = newGameObject.AddComponent <Slicer2D> (); smartSlicer.textureType = Slicer2D.TextureType.Mesh; smartSlicer.material = material; PolygonGenerator2D.GenerateMesh(newGameObject, newPolygon, new Vector2(1, 1)); }
public List <GameObject> PerformResult(List <Polygon> result) { List <GameObject> resultGameObjects = new List <GameObject> (); if (result.Count < 1) { return(resultGameObjects); } if (sliceEvent != null) { Slice2D slice = Slice2D.Create(); slice.polygons = result; if (sliceEvent(slice) == false) { return(resultGameObjects); } } Destroy(gameObject); int name_id = 1; float originMass = Polygon.CreateFromCollider(gameObject).GetArea(); foreach (Polygon id in result) { GameObject gObject = Instantiate(gameObject); resultGameObjects.Add(gObject); Component[] components = GetComponents <Component> (); foreach (Component c in components) { if (c.GetType() == typeof(Rigidbody2D)) { Rigidbody2D originalRigidBody = (Rigidbody2D)c; Rigidbody2D newRigidBody = gObject.GetComponent <Rigidbody2D> (); newRigidBody.velocity = originalRigidBody.velocity; newRigidBody.angularVelocity = originalRigidBody.angularVelocity; newRigidBody.mass = originalRigidBody.mass * (id.ToLocalSpace(newRigidBody.transform).GetArea() / originMass); // 再現したものの重さを撮った比例に応じて変化する } } foreach (Behaviour childCompnent in gObject.GetComponentsInChildren <Behaviour>()) // Wrong { childCompnent.enabled = true; } Slicer2D slicer = gObject.GetComponent <Slicer2D> (); slicer.sliceCounter = sliceCounter + 1; slicer.maxSlices = maxSlices; //オブジェクトの名前を変更 gObject.name = name + " (" + name_id + ")"; gObject.transform.parent = transform.parent; gObject.transform.position = transform.position; gObject.transform.rotation = transform.rotation; gObject.AddComponent <Pauser>().Pause(); if (id.ToLocalSpace(gObject.transform).GetArea() / originMass > 0.02f) //切ったモノの比例を計算して、2パーセント以下の場合は消します { ShotLens.GetComponent <ShotLensController>().ItemListAdd(gObject); // GameObject.Find("ShotLens").GetComponent<ShotLensController>().ItemListAdd(gObject); } else { Destroy(gObject); } switch (textureType) { case TextureType.Sprite: if (gameObject.GetComponent <SpriteRenderer> () != null && gObject.GetComponent <SpriteMesh2D> () == null) { gObject.AddComponent <SpriteMesh2D> (); } break; case TextureType.Mesh: case TextureType.None: default: break; } switch (colliderType) { case Polygon.ColliderType.Box: Destroy(gObject.GetComponent <BoxCollider2D> ()); break; case Polygon.ColliderType.Circle: Destroy(gObject.GetComponent <CircleCollider2D>()); break; case Polygon.ColliderType.Capsule: Destroy(gObject.GetComponent <CapsuleCollider2D>()); break; default: break; } PolygonGenerator2D.GenerateCollider(gObject, id.ToLocalSpace(gObject.transform)); name_id += 1; } if ((resultGameObjects.Count > 0) && (sliceResultEvent != null)) { sliceResultEvent(resultGameObjects); } return(resultGameObjects); }
public List <GameObject> PerformResult(List <Polygon> result) { List <GameObject> resultGameObjects = new List <GameObject> (); if (result.Count < 1) { return(resultGameObjects); } if (sliceEvent != null) { Slice2D slice = Slice2D.Create(); slice.polygons = result; if (sliceEvent(slice) == false) { return(resultGameObjects); } } int name_id = 1; foreach (Polygon id in result) { GameObject gObject = Instantiate(gameObject); resultGameObjects.Add(gObject); Component[] components = GetComponents <Component> (); foreach (Component comp in components) { if (comp.GetType() == typeof(Rigidbody2D)) { Rigidbody2D originalRigidBody = (Rigidbody2D)comp; Rigidbody2D newRigidBody = gObject.GetComponent <Rigidbody2D> (); newRigidBody.velocity = originalRigidBody.velocity; newRigidBody.angularVelocity = originalRigidBody.angularVelocity; } } foreach (Behaviour childCompnent in gObject.GetComponentsInChildren <Behaviour>()) { foreach (Behaviour child in GetComponentsInChildren <Behaviour>()) { if (child.GetType() == childCompnent.GetType()) // Same Components Fail { childCompnent.enabled = child.enabled; break; } } } Slicer2D slicer = gObject.GetComponent <Slicer2D> (); slicer.sliceCounter = sliceCounter + 1; slicer.maxSlices = maxSlices; gObject.name = name + " (" + name_id + ")"; gObject.transform.parent = transform.parent; gObject.transform.position = transform.position; gObject.transform.rotation = transform.rotation; switch (textureType) { case TextureType.Sprite: if (gameObject.GetComponent <SpriteRenderer> () != null && gObject.GetComponent <SpriteMesh2D> () == null) { gObject.AddComponent <SpriteMesh2D> (); } break; case TextureType.Mesh: case TextureType.None: default: break; } switch (colliderType) // if Collider <> Polygon Collider Destroy { case Polygon.ColliderType.Box: Destroy(gObject.GetComponent <BoxCollider2D> ()); break; case Polygon.ColliderType.Circle: Destroy(gObject.GetComponent <CircleCollider2D>()); break; case Polygon.ColliderType.Capsule: Destroy(gObject.GetComponent <CapsuleCollider2D>()); break; default: break; } PolygonGenerator2D.GenerateCollider(gObject, id.ToLocalSpace(gObject.transform)); name_id += 1; } Destroy(gameObject); if ((resultGameObjects.Count > 0) && (sliceResultEvent != null)) { sliceResultEvent(resultGameObjects); } return(resultGameObjects); }