Unityでxmlにデータを保存するためにこんな感じにコードを書きました。
C#
1 /// <summary> 2/// 保存 3/// </summary> 4/// <param name="path">xmlファイルの場所</param> 5/// <param name="obj">シリアライズするオブジェクト</param> 6public static void Save(string path, System.Object obj) 7{ 8 Type type = obj.GetType(); 9 XmlSerializer ser = new XmlSerializer(type); 10 StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8); 11 12 ser.Serialize(sw, obj); 13 sw.Close(); 14 }
こうやってSave()を呼び出しました。
C#
1 string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\Roaming\qqqq"; 2 var task1 = new Task(() => SavedataManager.Save( path + "Player.xml", player)); 3 task1.Start(); 4 task1.Wait();
そして呼び出すと
InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. UnityEngine.Transform does not implement Add(System.Object). System.Xml.Serialization.TypeScope.GetEnumeratorElementType (System.Type type, System.Xml.Serialization.TypeFlags& flags) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.TypeScope.ImportTypeDesc (System.Type type, System.Reflection.MemberInfo memberInfo, System.Boolean directReference) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.TypeScope.GetTypeDesc (System.Type type, System.Reflection.MemberInfo source, System.Boolean directReference, System.Boolean throwOnError) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.StructModel.GetPropertyModel (System.Reflection.PropertyInfo propertyInfo) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.StructModel.GetFieldModel (System.Reflection.MemberInfo memberInfo) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (System.Xml.Serialization.StructMapping mapping, System.Xml.Serialization.StructModel model, System.Boolean openModel, System.String typeName, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping (System.Xml.Serialization.StructModel model, System.String ns, System.Boolean openModel, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeModel model, System.String ns, System.Xml.Serialization.XmlReflectionImporter+ImportContext context, System.String dataType, System.Xml.Serialization.XmlAttributes a, System.Boolean repeats, System.Boolean openModel, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) Rethrow as InvalidOperationException: There was an error reflecting type 'Player'. System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeModel model, System.String ns, System.Xml.Serialization.XmlReflectionImporter+ImportContext context, System.String dataType, System.Xml.Serialization.XmlAttributes a, System.Boolean repeats, System.Boolean openModel, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeModel model, System.String ns, System.Xml.Serialization.XmlReflectionImporter+ImportContext context, System.String dataType, System.Xml.Serialization.XmlAttributes a, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportElement (System.Xml.Serialization.TypeModel model, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.RecursionLimiter limiter) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.String defaultNamespace) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.Serialization.XmlSerializer..ctor (System.Type type) (at <7fd195060d8c41448694ab221d3b56ca>:0) SavedataManager.Save (System.String path, System.Object obj) (at Assets/AST/SavedataManager/script/SavedataManager.cs:17) GameController.<Save>m__0 () (at Assets/script/GameController.cs:47) System.Threading.Tasks.Task.InnerInvoke () (at <e1a80661d61443feb3dbdaac88eeb776>:0) System.Threading.Tasks.Task.Execute () (at <e1a80661d61443feb3dbdaac88eeb776>:0) Rethrow as AggregateException: One or more errors occurred. System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) (at <e1a80661d61443feb3dbdaac88eeb776>:0) System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) (at <e1a80661d61443feb3dbdaac88eeb776>:0) System.Threading.Tasks.Task.Wait () (at <e1a80661d61443feb3dbdaac88eeb776>:0) GameController.Save (System.Boolean isExit) (at Assets/script/GameController.cs:49) UnityEngine.Events.InvokableCall`1[T1].Invoke (T1 args0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:207) UnityEngine.Events.CachedInvokableCall`1[T].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:345) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:70) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem.Update()
例外の原因、アドバイスなどをしていただけたら助かります。

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