/// <summary> /// Load character from json. /// </summary> public void LoadFromJson() { StartCoroutine(StandaloneFilePicker.OpenFile("Open as JSON", "", "json", (success, path, bytes) => { if (success) { var json = System.IO.File.ReadAllText(path); Character.FromJson(json, silent: false); } })); }
/// <summary> /// Save character to json. /// </summary> public void SaveToJson() { //string name = GameObject.Find("CharacterName").GetComponent<Text>().text; //string saveFile = Application.persistentDataPath + "/" + name + ".data"; //// Does it exist? //if (File.Exists(saveFile)) //{ // // File exists! //} //else //{ // // File does not exist. // // // // This could mean it was deleted or has not been created yet. //} StartCoroutine(StandaloneFilePicker.SaveFile("Save as JSON", "", name, "json", Encoding.Default.GetBytes(Character.ToJson()), (success, path) => { Debug.Log(success ? $"Saved as {path}" : "Error saving."); })); }
private IEnumerator CaptureFrames(Vector2 direction, List <CaptureOption> options, int frameSize, int frameCount, bool shadow) { _character = FindObjectOfType <Character4D>(); _character.SetDirection(direction); _character.Shadows.ForEach(i => i.SetActive(false)); _character.Shadows[0].SetActive(shadow); var stateHandler = _character.Animator.GetBehaviours <StateHandler>().SingleOrDefault(i => i.Name == "Death"); if (stateHandler) { stateHandler.StateExit.RemoveAllListeners(); } var clips = new Dictionary <string, List <Texture2D> >(); foreach (var option in options) { _character.Animator.SetInteger("State", (int)option.State); if (option.Action != null) { _character.Animator.SetTrigger(option.Action); } else { _character.Animator.ResetTrigger("Slash1H"); _character.Animator.ResetTrigger("Slash2H"); _character.Animator.ResetTrigger("Jab"); } _character.Animator.SetBool("Action", option.Action != null); _character.Animator.speed = 2; yield return(new WaitForSeconds(0.1f)); _character.Animator.speed = 0; var lowerClip = option.State == CharacterState.Death ? null : _character.Animator.GetCurrentAnimatorClipInfo(0)[0].clip; var upperClip = option.State == CharacterState.Death ? null : _character.Animator.GetCurrentAnimatorClipInfo(1)[0].clip; var complexClip = option.State == CharacterState.Death ? _character.Animator.GetCurrentAnimatorClipInfo(2)[0].clip : null; for (var j = 0; j < frameCount; j++) { var normalizedTime = (float)j / (frameCount - 1); var clip = upperClip ?? complexClip; var expressionEvent = clip.events.Where(i => i.functionName == "SetExpression" && Mathf.Abs(i.time / clip.length - normalizedTime) <= 1f / (frameCount - 1)) .OrderBy(i => Mathf.Abs(i.time / clip.length - normalizedTime)).FirstOrDefault(); if (expressionEvent != null) { _character.SetExpression(expressionEvent.stringParameter); } yield return(ShowFrame(lowerClip?.name, upperClip?.name, complexClip?.name, normalizedTime)); var frame = CaptureFrame(frameSize, frameSize); var animationName = option.Action ?? option.State.ToString(); if (clips.ContainsKey(animationName)) { clips[animationName].Add(frame); } else { clips.Add(animationName, new List <Texture2D> { frame }); } } } _character.LayerManager.Sprites[0].gameObject.SetActive(false); _character.AnimationManager.SetState(CharacterState.Idle); _character.Animator.speed = 1; if (stateHandler) { stateHandler.StateExit.AddListener(() => _character.SetExpression("Default")); } var texture = CreateSheet(clips, frameSize, frameSize); yield return(StandaloneFilePicker.SaveFile("Save as sprite sheet", "", "Character", "png", texture.EncodeToPNG(), (success, path) => { Debug.Log(success ? $"Saved as {path}" : "Error saving."); })); }