예제 #1
0
파일: VCIImporter.cs 프로젝트: notargs/VCI
        public void ExtractAnimation(UnityPath prefabPath)
        {
#if UNITY_EDITOR
            if (GLTF.animations == null || GLTF.animations.Count == 0)
            {
                return;
            }

            var prefabParentDir = prefabPath.Parent;
            var folder          = prefabPath.GetAssetFolder(".Animation");
            folder.EnsureFolder();

            var animationClips = new AnimationClip[GLTF.animations.Count];
            for (int i = 0; i < GLTF.nodes.Count; i++)
            {
                var node = GLTF.nodes[i];
                if (node.extensions == null || node.extensions.VCAST_vci_animation == null)
                {
                    continue;
                }

                var vciAnimation = node.extensions.VCAST_vci_animation;

                foreach (var animationReference in vciAnimation.animationReferences)
                {
                    var           gltfAnimation = GLTF.animations[animationReference.animation];
                    AnimationClip clip          = null;
                    if (animationClips[animationReference.animation] == null)
                    {
                        clip = AnimationImporterUtil.ImportAnimationClip(this, gltfAnimation, node);
                        animationClips[animationReference.animation] = clip;
                    }
                }
            }

            // Import root animation clips.
            for (var i = 0; i < animationClips.Length; i++)
            {
                if (animationClips[i] != null)
                {
                    continue;
                }
                animationClips[i] = AnimationImporterUtil.ImportAnimationClip(this, GLTF.animations[i]);
            }

            var saveAnimationPath = new string[GLTF.animations.Count];
            for (int i = 0; i < animationClips.Count(); i++)
            {
                if (animationClips[i] != null)
                {
                    var path = string.Format("{0}/{1}.{2}", folder.Value, animationClips[i].name, "asset");
                    AssetDatabase.CreateAsset(animationClips[i], path);
                    saveAnimationPath[i] = path;
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            var saveAnimationClips = new AnimationClip[GLTF.animations.Count];
            for (int i = 0; i < saveAnimationPath.Count(); i++)
            {
                if (!string.IsNullOrEmpty(saveAnimationPath[i]))
                {
                    saveAnimationClips[i] = AssetDatabase.LoadAssetAtPath(saveAnimationPath[i], typeof(AnimationClip)) as AnimationClip;
                }
            }

            AnimationClips = new List <AnimationClip>(saveAnimationClips);
#endif
        }