Ejemplo n.º 1
0
        public void MapNameToSourceTest()
        {
            //Create a cube with 3 children game objects
            var cube    = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            var sphere  = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            var quad    = GameObject.CreatePrimitive(PrimitiveType.Quad);

            capsule.transform.parent = cube.transform;
            sphere.transform.parent  = cube.transform;
            quad.transform.parent    = cube.transform;
            capsule.transform.SetSiblingIndex(0);

            //Create a similar Heirarchy that we can use as our phony "exported" hierarchy.
            var cube2    = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var capsule2 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            var sphere2  = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            var quad2    = GameObject.CreatePrimitive(PrimitiveType.Quad);

            capsule2.transform.parent = cube2.transform;
            sphere2.transform.parent  = cube2.transform;
            quad2.transform.parent    = cube2.transform;
            capsule.transform.SetSiblingIndex(1);

            ConvertToNestedPrefab.GatherSceneHierarchy(cube);
            ConvertToNestedPrefab.MapNameToSourceRecursive(cube, cube2);

            var dictionary = ConvertToNestedPrefab.s_nameToInfo;

            //We expect these to pass because we've given it an identical game object, as it would have after a normal export.
            Assert.AreSame(capsule2, dictionary[capsule.name].destGO);
            Assert.AreSame(sphere2, dictionary[sphere.name].destGO);
            Assert.AreSame(quad2, dictionary[quad.name].destGO);
            Assert.True(dictionary.Count == 4);

            //Create a broken hierarchy, one that is missing a primitive
            var cube3    = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var capsule3 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            var sphere3  = GameObject.CreatePrimitive(PrimitiveType.Sphere);

            capsule3.transform.parent = cube3.transform;
            sphere3.transform.parent  = cube3.transform;

            ConvertToNestedPrefab.GatherSceneHierarchy(cube);
            ConvertToNestedPrefab.MapNameToSourceRecursive(cube, cube3);
            var dictionaryBroken = ConvertToNestedPrefab.s_nameToInfo;

            //the dictionary size should be equal to the amount of children + the parent
            Assert.That(dictionaryBroken.Count, Is.EqualTo(4));

            Assert.IsNull(dictionaryBroken[quad.name].destGO);
            Assert.AreSame(capsule3, dictionaryBroken[capsule.name].destGO);
            Assert.AreSame(sphere3, dictionaryBroken[sphere.name].destGO);
        }
Ejemplo n.º 2
0
        public void TestStaticHelpers()
        {
            // Test IncrementFileName
            {
                var tempPath = Path.GetTempPath();
                var basename = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
                basename = basename + "yo"; // add some non-numeric stuff

                var filename1 = basename + ".fbx";
                var filename2 = Path.Combine(tempPath, basename + " 1.fbx");
                Assert.AreEqual(filename2, ConvertToNestedPrefab.IncrementFileName(tempPath, filename1));

                filename1 = basename + " 1.fbx";
                filename2 = Path.Combine(tempPath, basename + " 2.fbx");
                Assert.AreEqual(filename2, ConvertToNestedPrefab.IncrementFileName(tempPath, filename1));

                filename1 = basename + "1.fbx";
                filename2 = Path.Combine(tempPath, basename + "2.fbx");
                Assert.AreEqual(filename2, ConvertToNestedPrefab.IncrementFileName(tempPath, filename1));

                // UNI-25513: bug was that Cube01.fbx => Cube2.fbx
                filename1 = basename + "01.fbx";
                filename2 = Path.Combine(tempPath, basename + "02.fbx");
                Assert.AreEqual(filename2, ConvertToNestedPrefab.IncrementFileName(tempPath, filename1));
            }

            // Test EnforceUniqueNames
            {
                var a  = new GameObject("a");
                var b  = new GameObject("b");
                var a1 = new GameObject("a");
                var a2 = new GameObject("a");
                ConvertToNestedPrefab.EnforceUniqueNames(new GameObject[] { a, b, a1, a2 });
                Assert.AreEqual("a", a.name);
                Assert.AreEqual("b", b.name);
                Assert.AreEqual("a 1", a1.name);
                Assert.AreEqual("a 2", a2.name);
            }

            // Test GetOrCreateFbxAsset and WillExportFbx
            {
                var a = CreateHierarchy();

                // Test on an object in the scene
                Assert.That(ConvertToNestedPrefab.WillExportFbx(a));
                var aAsset = ConvertToNestedPrefab.GetOrCreateFbxAsset(a, fbxFullPath: GetRandomFbxFilePath());
                Assert.AreNotEqual(a, aAsset);
                AssertSameHierarchy(a, aAsset, ignoreRootName: true, ignoreRootTransform: true);
                Assert.AreEqual(PrefabAssetType.Model, PrefabUtility.GetPrefabAssetType(aAsset));
                Assert.AreEqual(PrefabInstanceStatus.NotAPrefab, PrefabUtility.GetPrefabInstanceStatus(aAsset));

                // Test on an fbx asset
                Assert.That(!ConvertToNestedPrefab.WillExportFbx(aAsset));
                var aAssetAsset = ConvertToNestedPrefab.GetOrCreateFbxAsset(aAsset, fbxFullPath: GetRandomFbxFilePath());
                Assert.AreEqual(aAsset, aAssetAsset);

                // Test on an fbx instance
                var aAssetInstance = PrefabUtility.InstantiatePrefab(aAsset) as GameObject;
                Assert.That(!ConvertToNestedPrefab.WillExportFbx(aAssetInstance));
                var aAssetInstanceAsset = ConvertToNestedPrefab.GetOrCreateFbxAsset(aAssetInstance, fbxFullPath: GetRandomFbxFilePath());
                Assert.AreEqual(aAsset, aAssetInstanceAsset);
            }

            // Test CopyComponents
            {
                var a = GameObject.CreatePrimitive(PrimitiveType.Cube);
                a.name = "a";
                var b = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                b.name = "b";
                a.AddComponent <BoxCollider>();
                a.transform.localPosition += new Vector3(1, 2, 3);
                Assert.IsFalse(b.GetComponent <BoxCollider>());
                Assert.AreEqual(Vector3.zero, b.transform.localPosition);
                Assert.AreNotEqual(a.GetComponent <MeshFilter>().sharedMesh, b.GetComponent <MeshFilter>().sharedMesh);
                ConvertToNestedPrefab.GatherSceneHierarchy(b);
                ConvertToNestedPrefab.MapNameToSourceRecursive(b, a);
                var fixSceneRefsMap = new Dictionary <Object, Object>();
                ConvertToNestedPrefab.CopyComponents(b, a, a, fixSceneRefsMap);
                Assert.IsTrue(b.GetComponent <BoxCollider>());
                Assert.AreEqual(a.transform.localPosition, b.transform.localPosition);
                Assert.AreNotEqual(a.GetComponent <MeshFilter>().sharedMesh, b.GetComponent <MeshFilter>().sharedMesh);
            }

            // Test UpdateFromSourceRecursive. Very similar but recursive.
            {
                var a = GameObject.CreatePrimitive(PrimitiveType.Cube);
                a.name = "a";
                var a1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
                a1.name = "AA";
                var a2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
                a2.name             = "BB";
                a2.transform.parent = a.transform;
                a1.transform.parent = a.transform; // out of alpha order!
                var b = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                b.name = "b";
                var b1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                b1.name = "AA";
                var b2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                b2.name             = "BB";
                b1.transform.parent = b.transform;
                b2.transform.parent = b.transform; // in alpha order
                a.AddComponent <BoxCollider>();
                a1.transform.localPosition = new Vector3(1, 2, 3);
                a.transform.localPosition  = new Vector3(4, 5, 6);

                Assert.AreNotEqual(b.GetComponent <MeshFilter>().sharedMesh, a.GetComponent <MeshFilter>().sharedMesh);
                Assert.IsFalse(b.GetComponent <BoxCollider>());
                Assert.AreEqual("BB", b.transform.GetChild(1).name);
                Assert.AreEqual(Vector3.zero, b1.transform.localPosition);

                ConvertToNestedPrefab.GatherSceneHierarchy(a);
                ConvertToNestedPrefab.UpdateFromSourceRecursive(b, a);

                // everything except the mesh + materials and child transforms should change
                Assert.AreNotEqual(b.GetComponent <MeshFilter>().sharedMesh, a.GetComponent <MeshFilter>().sharedMesh);
                Assert.IsTrue(b.GetComponent <BoxCollider>());
                Assert.AreEqual("BB", b.transform.GetChild(1).name);
                Assert.AreEqual(a.transform.localPosition, b.transform.localPosition);
                Assert.AreNotEqual(a1.transform.localPosition, b1.transform.localPosition);
            }

            // Test GetFbxAssetOrNull
            {
                // regular GO should return null
                var a = GameObject.CreatePrimitive(PrimitiveType.Cube);
                var b = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                b.transform.parent = a.transform;
                Assert.That(ConvertToNestedPrefab.GetFbxAssetOrNull(a), Is.Null);

                // try root FBX asset
                var fbx = ExportToFbx(a);
                Assert.That(ConvertToNestedPrefab.GetFbxAssetOrNull(fbx), Is.EqualTo(fbx));

                // try child of FBX asset
                Assert.That(ConvertToNestedPrefab.GetFbxAssetOrNull(fbx.transform.GetChild(0).gameObject), Is.Null);

                // try root of FBX instance
                var fbxInstance = PrefabUtility.InstantiatePrefab(fbx) as GameObject;//GameObject.Instantiate(fbx) as GameObject;
                Assert.That(fbxInstance);
                Assert.That(ConvertToNestedPrefab.GetFbxAssetOrNull(fbxInstance), Is.EqualTo(fbx));

                // try child of FBX instance
                Assert.That(ConvertToNestedPrefab.GetFbxAssetOrNull(fbxInstance.transform.GetChild(0).gameObject), Is.Null);

                // try root of prefab asset
                var prefab = PrefabUtility.SaveAsPrefabAsset(fbxInstance, GetRandomPrefabAssetPath());
                Assert.That(prefab);
                Assert.That(ConvertToNestedPrefab.GetFbxAssetOrNull(prefab), Is.Null);
            }

            // Test CopySerializedProperty
            {
                // test with ReferenceComponent
                var a = GameObject.CreatePrimitive(PrimitiveType.Capsule);
                var aReferenceComponent = a.AddComponent <ReferenceComponent>();
                aReferenceComponent.m_transform = a.transform;
                a.name = "test";
                var b = GameObject.CreatePrimitive(PrimitiveType.Cube);
                var bReferenceComponent = b.AddComponent <ReferenceComponent>();
                bReferenceComponent.m_transform = b.transform;
                b.name = "test2";

                var aSerObj = new SerializedObject(aReferenceComponent);
                var bSerObj = new SerializedObject(bReferenceComponent);

                var fromProp = aSerObj.FindProperty("m_transform");

                ConvertToNestedPrefab.s_nameToInfo = new Dictionary <string, ConvertToNestedPrefab.SourceObjectInfo>()
                {
                    { "test", new ConvertToNestedPrefab.SourceObjectInfo(a) }
                };
                ConvertToNestedPrefab.CopySerializedProperty(bSerObj, fromProp);

                Assert.That(bReferenceComponent.m_transform.name, Is.EqualTo(a.transform.name));
            }

            // Test GetSceneReferencesToObject()
            {
                var a = GameObject.CreatePrimitive(PrimitiveType.Cube);
                var b = new GameObject();
                var c = new GameObject();

                var reference  = b.AddComponent <ReferenceComponent>();
                var constraint = c.AddComponent <UnityEngine.Animations.PositionConstraint>();

                reference.m_collider = a.GetComponent <BoxCollider>();

                var constraintSource = new UnityEngine.Animations.ConstraintSource();
                constraintSource.sourceTransform = a.transform;
                constraintSource.weight          = 0.5f;
                constraint.AddSource(constraintSource);

#if !UNITY_2021_2_OR_NEWER
                var sceneRefs = ConvertToNestedPrefab.GetSceneReferencesToObject(a);
                Assert.That(sceneRefs.Count, Is.EqualTo(2));
                Assert.That(sceneRefs.Contains(a)); // GameObjects also reference themself
                Assert.That(sceneRefs.Contains(b));

                sceneRefs = ConvertToNestedPrefab.GetSceneReferencesToObject(a.GetComponent <BoxCollider>());
                Assert.That(sceneRefs.Count, Is.EqualTo(1));
                Assert.That(sceneRefs.Contains(b));

                sceneRefs = ConvertToNestedPrefab.GetSceneReferencesToObject(a.transform);
                Assert.That(sceneRefs.Count, Is.EqualTo(2));
                Assert.That(sceneRefs.Contains(b));
                Assert.That(sceneRefs.Contains(c));
#endif // !UNITY_2021_2_OR_NEWER
            }
        }