実現したいこと
Unity用の会話シーン作成用アセット「Fungus」を利用されたことのある方にお聞きいたします。
アセット上で管理している変数(Variableクラス)をJson文字列としてシリアライズし、会話変数のセーブ機能を作成したいです。
また、ロードの場合はVariableクラスへデシリアライズする。
<参考>
Fungusパッケージ(github)
公式サイト
発生している問題・分からないこと
変数(Variableクラス)をJson文字列へシリアライズすることは上記図のとおりできた(項目が足りているかは不明)のですが、デシリアライズができずに以下のエラーが出力されます。
なお、ソースコードはコード数が多いので中略していることご了承ください。
<エラー文>
ArgumentException: Cannot deserialize JSON to new instances of type 'Variable.'
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <0c2e081264354d5689a31eb5ff26b1d1>:0)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <0c2e081264354d5689a31eb5ff26b1d1>:0)
Fungus.Flowchart.Start () (at Assets/ProjectSystems/Fungus/Fungus/Scripts/Components/Flowchart.cs:137)
該当のソースコード
C#
1using UnityEngine; 2using UnityEngine.EventSystems; 3using System; 4using System.Text; 5using System.Linq; 6using System.Collections.Generic; 7using System.Text.RegularExpressions; 8 9namespace Fungus 10{ 11 /// <summary> 12 /// Visual scripting controller for the Flowchart programming language. 13 /// Flowchart objects may be edited visually using the Flowchart editor window. 14 /// </summary> 15 [ExecuteInEditMode] 16 [System.Serializable] 17 public class Flowchart : MonoBehaviour, ISubstitutionHandler 18 { 19 [HideInInspector] 20 [SerializeField] private List<Variable> variables = new List Variable>(); 21 22 //中略・・・ 23 24 protected virtual void Start() 25 { 26 //とりあえずリストの0番目をシリアライズしてみる。 27 string jsonstr = JsonUtility.ToJson(variables[0]); 28 Debug.Log(jsonstr); //出力結果はこんな感じです。{"scope":0,"key":"Flag","value":false} 29 30 //デシリアライズする。 31 var test = JsonUtility.FromJson<Variable>(jsonstr); //(エラー発生) 32 Debug.Log(test); 33 34 CheckEventSystem(); 35 } 36 } 37}
C#
1using UnityEngine; 2 3namespace Fungus 4{ 5 6 //中略・・・ 7 8 /// <summary> 9 /// Abstract base class for variables. 10 /// </summary> 11 [RequireComponent(typeof(Flowchart))] 12 [System.Serializable] 13 public abstract class Variable : MonoBehaviour 14 { 15 [SerializeField] protected VariableScope scope; 16 17 [SerializeField] protected string key = ""; 18 19 #region Public members 20 21 /// <summary> 22 /// Visibility scope for the variable. 23 /// </summary> 24 public virtual VariableScope Scope { get { return scope; } set { scope = value; } } 25 26 /// <summary> 27 /// String identifier for the variable. 28 /// </summary> 29 public virtual string Key { get { return key; } set { key = value; } } 30 31 /// <summary> 32 /// Callback to reset the variable if the Flowchart is reset. 33 /// </summary> 34 public abstract void OnReset(); 35 36 /// <summary> 37 /// Used by SetVariable, child classes required to declare and implement operators. 38 /// </summary> 39 /// <param name="setOperator"></param> 40 /// <param name="value"></param> 41 public abstract void Apply(SetOperator setOperator, object value); 42 43 /// <summary> 44 /// Used by Ifs, While, and the like. Child classes required to declare and implement comparisons. 45 /// </summary> 46 /// <param name="compareOperator"></param> 47 /// <param name="value"></param> 48 /// <returns></returns> 49 public abstract bool Evaluate(CompareOperator compareOperator, object value); 50 51 /// <summary> 52 /// Does the underlying type provide support for +-*/ 53 /// </summary> 54 public virtual bool IsArithmeticSupported(SetOperator setOperator) { return false; } 55 56 /// <summary> 57 /// Does the underlying type provide support for < <= > >= 58 /// </summary> 59 public virtual bool IsComparisonSupported() { return false; } 60 61 /// <summary> 62 /// Boxed or referenced value of type defined within inherited types. 63 /// Not recommended for direct use, primarily intended for use in editor code. 64 /// </summary> 65 public abstract object GetValue(); 66 67 //we are required to be on a flowchart so we provide this as a helper 68 public virtual Flowchart GetFlowchart() 69 { 70 return GetComponent<Flowchart>(); 71 } 72 #endregion 73 } 74 75 //中略・・・ 76}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
①MonoBehaviour や ScriptableObjectを継承しているクラスはJsonUtility.FromJsonではデシリアライズできないことを知ったため、以下のサイト様を参考にAddComponentしてからのデシリアライズを試みましたがうまくいきませんでした。
参考にしたサイト
②上記理由が原因と推察を立て、VariableクラスのMonoBehaviourの継承をはずしましたが、他スクリプトにてコンパイルエラー発生したため断念しました。
有識者の方がいらっしゃいましたら、ご教示いただきたくよろしくお願いします。
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。