private void GeneratePrefab(AnimationBaker.BakedData bakedData)
        {
            string path  = AssetDatabase.GetAssetPath(this);
            int    start = path.LastIndexOf('/');

            path  = path.Remove(start, path.Length - start);
            path += "/" + name + ".prefab";

            // Get info.
            NamingConventionUtils.PositionMapInfo info = bakedData.GetPositionMap.name.GetTextureInfo();

            // Generate Material
            if (!AssetDatabaseUtils.HasChildAsset(this, material))
            {
                material = AnimationMaterial.Create(name, materialShader, positionMap, useNormalA, useInterpolation, info.maxFrames);
                AssetDatabase.AddObjectToAsset(material, this);
            }
            else
            {
                material.Update(name, materialShader, positionMap, useNormalA, useInterpolation, info.maxFrames);
            }

            // Generate Prefab
            prefab = AnimationPrefab.Create(path, name, meshes, material, lodSettings.GetTransitionSettings());
        }
        private void GenerateBook(AnimationBaker.BakedData bakedData)
        {
            // Create book.
            if (!book)
            {
                book = CreateInstance <VA_AnimationBook>();
            }

            book.name        = string.Format("{0}_Book", name);
            book.positionMap = positionMap;
            book.animations  = new List <VA_Animation>();
            book.TryAddMaterial(material);

            // Save book.
            if (!AssetDatabaseUtils.HasChildAsset(this, book))
            {
                AssetDatabase.AddObjectToAsset(book, this);
            }

            // Get animation info.
            List <NamingConventionUtils.PositionMapInfo> info = new List <NamingConventionUtils.PositionMapInfo>();

            foreach (var t in bakedData.positionMaps)
            {
                info.Add(t.name.GetTextureInfo());
            }

            // Create animations.
            for (int i = 0; i < info.Count; i++)
            {
                string           animationName = string.Format("{0}_{1}", name, info[i].name);
                VA_AnimationData newData       = new VA_AnimationData(animationName, info[i].frames, info[i].maxFrames, info[i].fps, i, -1);

                // Either update existing animation or create a new one.
                if (TryGetAnimationWithName(animationName, out VA_Animation animation))
                {
                    animation.SetData(newData);
                }
                else
                {
                    animation      = CreateInstance <VA_Animation>();
                    animation.name = animationName;
                    animation.SetData(newData);
                    animations.Add(animation);
                }

                book.TryAddAnimation(animation);
            }

            // Save animation objects.
            foreach (var a in animations)
            {
                AssetDatabaseUtils.TryAddChildAsset(book, a);
            }
        }
        private void SaveAssets(AnimationBaker.BakedData bakedData)
        {
            AssetDatabaseUtils.RemoveChildAssets(this, new Object[2] {
                book, material
            });

            Bounds bounds = new Bounds
            {
                max = bakedData.maxBounds,
                min = bakedData.minBounds
            };

            for (int i = 0; i < meshes.Length; i++)
            {
                if (applyAnimationBounds)
                {
                    meshes[i].bounds = bounds;
                }

                meshes[i].Finalize();
                AssetDatabase.AddObjectToAsset(meshes[i], this);
            }

            AssetDatabase.AddObjectToAsset(positionMap, this);
            AssetDatabase.SaveAssets();

            if (generatePrefab)
            {
                GeneratePrefab(bakedData);
            }

            if (generateAnimationBook)
            {
                GenerateBook(bakedData);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
Exemple #4
0
        public static GameObject Create(string path, string name, Mesh[] meshes, Material material, float[] lodTransitions)
        {
            GameObject parent = null;

            if (AssetDatabaseUtils.HasAsset(path, typeof(GameObject)))
            {
                // Load existing parent.
                parent = PrefabUtility.LoadPrefabContents(path);

                // Check setup.
                if (!parent.TryGetComponent(out LODGroup _))
                {
                    parent.AddComponent <LODGroup>();
                }

                if (!parent.TryGetComponent(out VA_AnimatorComponentAuthoring _))
                {
                    parent.AddComponent <VA_AnimatorComponentAuthoring>();
                }

                if (!parent.TryGetComponent(out Unity.Entities.ConvertToEntity _))
                {
                    parent.AddComponent <Unity.Entities.ConvertToEntity>();
                }
            }
            else
            {
                // Create parent.
                parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimatorComponentAuthoring), typeof(Unity.Entities.ConvertToEntity));
            }

            // Create all LODs.
            LOD[] lods = new LOD[meshes.Length];

            for (int i = 0; i < meshes.Length; i++)
            {
                string childName = string.Format("{0}_LOD{1}", name, i);

                GameObject child;
                {
                    Transform t = parent.transform.Find(childName);
                    if (t)
                    {
                        child = t.gameObject;
                    }
                    else
                    {
                        child = new GameObject(childName, typeof(MeshFilter), typeof(MeshRenderer));
                    }
                }

                if (child.TryGetComponent(out MeshFilter mf))
                {
                    mf.sharedMesh = meshes[i];
                }

                if (child.TryGetComponent(out MeshRenderer mr))
                {
                    mr.sharedMaterial = material;
                }

                child.transform.SetParent(parent.transform);
                lods[i] = new LOD(lodTransitions[i], new Renderer[1] {
                    mr
                });
            }

            var lodGroup = parent.GetComponent <LODGroup>();

            lodGroup.SetLODs(lods);
            lodGroup.RecalculateBounds();

            // Create prefab.
            GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(parent, path, InteractionMode.AutomatedAction);

            GameObject.DestroyImmediate(parent);

            return(prefab);
        }