本文整理汇总了C#中GameObject.DeepClone方法的典型用法代码示例。如果您正苦于以下问题:C# GameObject.DeepClone方法的具体用法?C# GameObject.DeepClone怎么用?C# GameObject.DeepClone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GameObject
的用法示例。
在下文中一共展示了GameObject.DeepClone方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CloneGameObject
[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);
}
示例2: CloneJointRigidBodies
[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());
}
示例3: RealWorldPerformanceTest
[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();
}
示例4: TransformHierarchyInitialized
[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);
}
}