BrashMonkey.Spriter.DataPlugins.SpriterDataUnity.CreateCharacterPrefab C# (CSharp) Method

CreateCharacterPrefab() private method

private CreateCharacterPrefab ( ) : void
return void
        private void CreateCharacterPrefab()
        {
            // All character creation is done in the hierarchy, then saved to a prefab

            // Create root object
            Transform characterRoot = new GameObject(entity.name).transform;
            characterRoot.gameObject.AddComponent<Animation>();

            if (Selection.activeTransform)
            {
                characterRoot.parent = Selection.activeTransform;
                characterRoot.localScale = Vector3.one;
            }

            //Do an inital pass to ensure all sprites are created
            foreach (SpriterAnimation anim in entity.animations)
            {
                // Create an animation clip
                var tempClip = new AnimationClip();
                var curves = new Dictionary<string, AnimationCurve>[14];
                for (int i = 0; i < 14; i++)
                {
                    curves[i] = new Dictionary<string, AnimationCurve>();
                }

                // Process each keyframe
                foreach (SpriterMainlineKey t in anim.mainline.keys)
                {
                    RecordFrame(anim, characterRoot, t, null, t.time/1000f, tempClip.frameRate, curves);
                }
            }

            // Go through each animation
            foreach(SpriterAnimation anim in entity.animations)
            {
                // Create an animation clip
                var clip = new AnimationClip();
                clip.EnsureQuaternionContinuity();
                clip.name = anim.name;
                clip.wrapMode = WrapMode.Loop;

                bool last = anim == entity.animations.LastOrDefault();

                // Create animation curves
                var curves = new Dictionary<string, AnimationCurve>[14];

                for(int i = 0; i < 14; i++)
                {
                    curves[i] = new Dictionary<string, AnimationCurve>();
                }

                // Go through each keyframe
                foreach (SpriterMainlineKey t in anim.mainline.keys)
                {
                    // Record the frame
                    RecordFrame(anim, characterRoot, t, null, t.time/1000f, clip.frameRate, curves);
                }
                //if there's no keyframe at the end, give an end target to set the animations length
                if (anim.mainline.keys.Last().time < anim.length)
                {
                    if (anim.playbackType == PlaybackType.Loop || anim.playbackType == PlaybackType.Unknown)
                        RecordFrame(anim, characterRoot, anim.mainline.keys[0], last ? anim.mainline.keys[0] : null, anim.length / 1000f, clip.frameRate,
                                    curves);
                    else
                        RecordFrame(anim, characterRoot, anim.mainline.keys.Last(), last ? anim.mainline.keys.Last() : null, anim.length / 1000f, clip.frameRate,
                                    curves);
                }

                switch (anim.playbackType)
                {
                    case PlaybackType.Unknown:
                        clip.wrapMode = WrapMode.Loop;
                        break;
                    case PlaybackType.PlayOnce:
                        clip.wrapMode = WrapMode.Once;
                        break;
                    case PlaybackType.Loop:
                        clip.wrapMode = WrapMode.Loop;
                        break;
                    case PlaybackType.PingPong:
                        clip.wrapMode = WrapMode.PingPong;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                RemoveTangents(curves);

                // Bind the animation
                BindAnimation(clip, curves);

                // Save the animation
                string p = GetSaveFolder() + characterRoot.name + "@" + clip.name + ".anim";
                if (AssetDatabase.LoadAssetAtPath(p, typeof(AnimationClip)))
                {
                    AssetDatabase.DeleteAsset(p);
                    AssetDatabase.CreateAsset(clip, p);
                }
                else
                {
                    AssetDatabase.CreateAsset(clip, p);
                }

                // Update the reference
                clip = (AnimationClip)AssetDatabase.LoadAssetAtPath(p, typeof(AnimationClip));

                // Add the clip to the root object
                characterRoot.animation.AddClip(clip, clip.name);

                if (!characterRoot.animation.clip)
                {
                    characterRoot.animation.clip = characterRoot.animation.GetClip(clip.name);
                }
            }

            SaveAssets(characterRoot.gameObject);
        }