[Test] public void CloneJointRigidBodies() { // Create two joint bodies GameObject sourceA = new GameObject("ObjectA"); GameObject sourceB = new GameObject("ObjectB", sourceA); sourceA.AddComponent <Transform>(); sourceB.AddComponent <Transform>(); RigidBody sourceBodyA = sourceA.AddComponent <RigidBody>(); RigidBody sourceBodyB = sourceB.AddComponent <RigidBody>(); sourceBodyA.AddJoint(new DistanceJointInfo(), sourceBodyB); // Are the two bodies joint together as expected? Assert.AreEqual(1, sourceBodyA.Joints.Count()); Assert.AreSame(sourceBodyA.Joints.First().OtherBody, sourceBodyB); Assert.IsTrue(sourceBodyB.Joints == null || !sourceBodyB.Joints.Any()); // Clone the object hierarchy GameObject targetA = sourceA.DeepClone(); GameObject targetB = targetA.ChildAtIndex(0); RigidBody targetBodyA = targetA.GetComponent <RigidBody>(); RigidBody targetBodyB = targetB.GetComponent <RigidBody>(); // Is the cloned hierarchy joint together as expected? Assert.AreEqual(1, targetBodyA.Joints.Count()); Assert.IsTrue(targetBodyB.Joints == null || !targetBodyB.Joints.Any()); Assert.AreSame(targetBodyA.Joints.First().OtherBody, targetBodyB); Assert.AreNotSame(sourceBodyA.Joints.First(), targetBodyA.Joints.First()); }
[Test] public void CloneShapeRigidBodies() { // Create a body with a simple shape GameObject source = new GameObject("ObjectA"); source.AddComponent <Transform>(); RigidBody sourceBody = source.AddComponent <RigidBody>(); CircleShapeInfo sourceShape = new CircleShapeInfo(32.0f, Vector2.Zero, 1.0f); sourceBody.AddShape(sourceShape); // Clone the object hierarchy GameObject target = source.DeepClone(); RigidBody targetBody = target.GetComponent <RigidBody>(); CircleShapeInfo targetShape = targetBody.Shapes.FirstOrDefault() as CircleShapeInfo; // Is the cloned shape set up like the source shape? Assert.AreEqual(1, targetBody.Shapes.Count()); Assert.AreSame(targetShape.Parent, targetBody); Assert.AreSame(sourceShape.Parent, sourceBody); Assert.AreNotSame(targetShape.Parent, sourceBody); Assert.AreNotSame(sourceShape.Parent, targetBody); // Clone only the source shape, but not any body CircleShapeInfo isolatedSourceShape = sourceShape; CircleShapeInfo isolatedTargetShape = isolatedSourceShape.DeepClone(); // Is the cloned joint still isolated, and not attached to any body? Assert.IsNotNull(isolatedSourceShape.Parent); Assert.IsNull(isolatedTargetShape.Parent); Assert.AreEqual(1, sourceBody.Shapes.Count()); }
[Test] public void TransformHierarchyInitialized() { Random rnd = new Random(); // Create a simple parent-child relation GameObject sourceParentObj = new GameObject("Parent"); GameObject sourceChildObj = new GameObject("Child", sourceParentObj); Transform sourceParentTransform = sourceParentObj.AddComponent <Transform>(); Transform sourceChildTransform = sourceChildObj.AddComponent <Transform>(); // Test whether transform values work relative as expected { Transform parent = sourceParentTransform; Transform child = sourceChildTransform; Vector3 parentPosAbs = rnd.NextVector3(); Vector3 childPosRel = rnd.NextVector3(); parent.Pos = parentPosAbs; child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); childPosRel = rnd.NextVector3(); child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); } // Clone the object hierarchy GameObject targetParentObj = sourceParentObj.DeepClone(); GameObject targetChildObj = targetParentObj.ChildByName("Child"); Transform targetParentTransform = targetParentObj.Transform; Transform targetChildTransform = targetChildObj.Transform; // Test whether transform values also work for the cloned hierarchy { Transform parent = targetParentTransform; Transform child = targetChildTransform; Vector3 parentPosAbs = rnd.NextVector3(); Vector3 childPosRel = rnd.NextVector3(); parent.Pos = parentPosAbs; child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); childPosRel = rnd.NextVector3(); child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); } }
[Test] public void CloneGameObject() { GameObject source = new GameObject("ObjectA"); source.AddComponent(CreateTestSource <TestComponent>()); GameObject target = source.DeepClone(); Assert.AreNotSame(source, target); Assert.AreEqual(source.Name, target.Name); TestClone(source.GetComponent <TestComponent>(), target.GetComponent <TestComponent>()); }
[Test] public void CloneGameObject() { Random rnd = new Random(); GameObject source = new GameObject("ObjectA"); source.AddComponent(new TestComponent(rnd)); GameObject target = source.DeepClone(); Assert.AreNotSame(source, target); Assert.AreEqual(source.Name, target.Name); Assert.AreEqual(source.GetComponent<TestComponent>(), target.GetComponent<TestComponent>()); Assert.AreNotSame(source.GetComponent<TestComponent>(), target.GetComponent<TestComponent>()); Assert.AreNotSame(source.GetComponent<TestComponent>().TestReferenceList, target.GetComponent<TestComponent>().TestReferenceList); }
[Test] public void CloneGameObject() { Random rnd = new Random(); GameObject source = new GameObject("ObjectA"); source.AddComponent(new TestComponent(rnd)); GameObject target = source.DeepClone(); Assert.AreNotSame(source, target); Assert.AreEqual(source.Name, target.Name); Assert.AreEqual(source.GetComponent <TestComponent>(), target.GetComponent <TestComponent>()); Assert.AreNotSame(source.GetComponent <TestComponent>(), target.GetComponent <TestComponent>()); Assert.AreNotSame(source.GetComponent <TestComponent>().TestReferenceList, target.GetComponent <TestComponent>().TestReferenceList); }
[Test] public void CloneJointRigidBodies() { // Create two joint bodies GameObject sourceA = new GameObject("ObjectA"); GameObject sourceB = new GameObject("ObjectB", sourceA); sourceA.AddComponent <Transform>(); sourceB.AddComponent <Transform>(); RigidBody sourceBodyA = sourceA.AddComponent <RigidBody>(); RigidBody sourceBodyB = sourceB.AddComponent <RigidBody>(); sourceBodyA.AddJoint(new DistanceJointInfo(), sourceBodyB); // Are the two bodies joint together as expected? Assert.AreEqual(1, sourceBodyA.Joints.Count()); Assert.AreSame(sourceBodyA.Joints.First().OtherBody, sourceBodyB); Assert.IsTrue(sourceBodyB.Joints == null || !sourceBodyB.Joints.Any()); // Clone the object hierarchy GameObject targetA = sourceA.DeepClone(); GameObject targetB = targetA.Children[0]; RigidBody targetBodyA = targetA.GetComponent <RigidBody>(); RigidBody targetBodyB = targetB.GetComponent <RigidBody>(); // Is the cloned hierarchy joint together as expected? Assert.AreEqual(1, targetBodyA.Joints.Count()); Assert.IsTrue(targetBodyB.Joints == null || !targetBodyB.Joints.Any()); Assert.AreSame(targetBodyA.Joints.First().OtherBody, targetBodyB); Assert.AreNotSame(sourceBodyA.Joints.First(), targetBodyA.Joints.First()); // Clone only the source joint, but not any body JointInfo isolatedSourceJoint = sourceBodyA.Joints.FirstOrDefault(); JointInfo isolatedTargetJoint = isolatedSourceJoint.DeepClone(); // Is the cloned joint still isolated, and not attached to any body? Assert.IsNotNull(isolatedSourceJoint.ParentBody); Assert.IsNotNull(isolatedSourceJoint.OtherBody); Assert.IsNull(isolatedTargetJoint.ParentBody); Assert.IsNull(isolatedTargetJoint.OtherBody); Assert.AreEqual(1, sourceBodyA.Joints.Count()); }
[Test] public void RealWorldPerformanceTest() { Random rnd = new Random(0); GameObject data = new GameObject("CloneRoot"); for (int i = 0; i < 1000; i++) { GameObject child = new GameObject("Child", data); child.AddComponent <Transform>(); if (i % 3 != 0) { child.AddComponent <SpriteRenderer>(); } if (i % 3 == 0) { child.AddComponent <RigidBody>(); } if (i % 7 == 0) { child.AddComponent <TextRenderer>(); } } GameObject[] results = new GameObject[25]; GC.Collect(); var watch = new System.Diagnostics.Stopwatch(); watch.Start(); for (int i = 0; i < results.Length; i++) { results[i] = data.DeepClone(); } watch.Stop(); TestHelper.LogNumericTestResult(this, "CloneGameObjectGraph", watch.Elapsed.TotalMilliseconds, "ms"); GC.Collect(); var watch2 = new System.Diagnostics.Stopwatch(); watch2.Start(); for (int j = 0; j < results.Length; j++) { GameObject obj = new GameObject("CloneRoot"); for (int i = 0; i < 1000; i++) { GameObject child = new GameObject("Child", data); child.AddComponent <Transform>(); if (i % 3 != 0) { child.AddComponent <SpriteRenderer>(); } if (i % 3 == 0) { child.AddComponent <RigidBody>(); } if (i % 7 == 0) { child.AddComponent <TextRenderer>(); } } results[j] = data; } watch2.Stop(); TestHelper.LogNumericTestResult(this, "CreateWithoutClone", watch2.Elapsed.TotalMilliseconds, "ms"); TestHelper.LogNumericTestResult(this, "CloneVersusRaw", watch.Elapsed.TotalMilliseconds / watch2.Elapsed.TotalMilliseconds, null); GC.Collect(); Assert.Pass(); }
[Test] public void RealWorldPerformanceTest() { Random rnd = new Random(0); GameObject data = new GameObject("CloneRoot"); for (int i = 0; i < 1000; i++) { GameObject child = new GameObject("Child", data); child.AddComponent<Transform>(); if (i % 3 != 0) child.AddComponent<SpriteRenderer>(); if (i % 3 == 0) child.AddComponent<RigidBody>(); if (i % 7 == 0) child.AddComponent<TextRenderer>(); } GameObject[] results = new GameObject[25]; GC.Collect(); var watch = new System.Diagnostics.Stopwatch(); watch.Start(); for (int i = 0; i < results.Length; i++) { results[i] = data.DeepClone(); } watch.Stop(); TestHelper.LogNumericTestResult(this, "CloneGameObjectGraph", watch.Elapsed.TotalMilliseconds, "ms"); GC.Collect(); var watch2 = new System.Diagnostics.Stopwatch(); watch2.Start(); for (int j = 0; j < results.Length; j++) { GameObject obj = new GameObject("CloneRoot"); for (int i = 0; i < 1000; i++) { GameObject child = new GameObject("Child", data); child.AddComponent<Transform>(); if (i % 3 != 0) child.AddComponent<SpriteRenderer>(); if (i % 3 == 0) child.AddComponent<RigidBody>(); if (i % 7 == 0) child.AddComponent<TextRenderer>(); } results[j] = data; } watch2.Stop(); TestHelper.LogNumericTestResult(this, "CreateWithoutClone", watch2.Elapsed.TotalMilliseconds, "ms"); TestHelper.LogNumericTestResult(this, "CloneVersusRaw", watch.Elapsed.TotalMilliseconds / watch2.Elapsed.TotalMilliseconds, null); GC.Collect(); Assert.Pass(); }
[Test] public void CloneJointRigidBodies() { // Create two joint bodies GameObject sourceA = new GameObject("ObjectA"); GameObject sourceB = new GameObject("ObjectB", sourceA); sourceA.AddComponent<Transform>(); sourceB.AddComponent<Transform>(); RigidBody sourceBodyA = sourceA.AddComponent<RigidBody>(); RigidBody sourceBodyB = sourceB.AddComponent<RigidBody>(); sourceBodyA.AddJoint(new DistanceJointInfo(), sourceBodyB); // Are the two bodies joint together as expected? Assert.AreEqual(1, sourceBodyA.Joints.Count()); Assert.AreEqual(1, sourceBodyB.Joints.Count()); Assert.AreSame(sourceBodyA.Joints.First(), sourceBodyB.Joints.First()); // Clone the object hierarchy GameObject targetA = sourceA.DeepClone(); GameObject targetB = targetA.ChildAtIndex(0); RigidBody targetBodyA = targetA.GetComponent<RigidBody>(); RigidBody targetBodyB = targetB.GetComponent<RigidBody>(); // Is the cloned hierarchy joint together as expected? Assert.AreEqual(1, targetBodyA.Joints.Count()); Assert.AreEqual(1, targetBodyB.Joints.Count()); Assert.AreSame(targetBodyA.Joints.First(), targetBodyB.Joints.First()); Assert.AreNotSame(sourceBodyA.Joints.First(), targetBodyA.Joints.First()); }
[Test] public void TransformHierarchyInitialized() { Random rnd = new Random(); // Create a simple parent-child relation GameObject sourceParentObj = new GameObject("Parent"); GameObject sourceChildObj = new GameObject("Child", sourceParentObj); Transform sourceParentTransform = sourceParentObj.AddComponent<Transform>(); Transform sourceChildTransform = sourceChildObj.AddComponent<Transform>(); // Test whether transform values work relative as expected { Transform parent = sourceParentTransform; Transform child = sourceChildTransform; Vector3 parentPosAbs = rnd.NextVector3(); Vector3 childPosRel = rnd.NextVector3(); parent.Pos = parentPosAbs; child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); childPosRel = rnd.NextVector3(); child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); } // Clone the object hierarchy GameObject targetParentObj = sourceParentObj.DeepClone(); GameObject targetChildObj = targetParentObj.ChildByName("Child"); Transform targetParentTransform = targetParentObj.Transform; Transform targetChildTransform = targetChildObj.Transform; // Test whether transform values also work for the cloned hierarchy { Transform parent = targetParentTransform; Transform child = targetChildTransform; Vector3 parentPosAbs = rnd.NextVector3(); Vector3 childPosRel = rnd.NextVector3(); parent.Pos = parentPosAbs; child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); childPosRel = rnd.NextVector3(); child.RelativePos = childPosRel; Assert.AreEqual(parentPosAbs.X + childPosRel.X, child.Pos.X, 0.000001f); Assert.AreEqual(parentPosAbs.Y + childPosRel.Y, child.Pos.Y, 0.000001f); Assert.AreEqual(parentPosAbs.Z + childPosRel.Z, child.Pos.Z, 0.000001f); } }