コード例 #1
0
 static bool TryDeserialize(TContainer container, Stream stream, string assetPath = null)
 {
     try
     {
         container.Reset();
         using (var result = JsonSerialization.DeserializeFromStream(stream, ref container))
         {
             if (result.Succeeded)
             {
                 container.m_AssetContent = !string.IsNullOrEmpty(assetPath) ? File.ReadAllText(assetPath) : null;
                 container.Sanitize();
                 return(true);
             }
             else
             {
                 var errors = result.AllEvents.Select(e => e.ToString());
                 LogDeserializeError(string.Join("\n", errors), container, assetPath);
                 container.Sanitize();
                 return(false);
             }
         }
     }
     catch (Exception e)
     {
         LogDeserializeError(e.Message, container, assetPath);
         container.Sanitize();
         return(false);
     }
 }
コード例 #2
0
        static bool TryDeserialize(ComponentContainer <TComponent> container, Stream stream, string path = null)
        {
            try
            {
                container.ClearComponents();
                container.ClearDependencies();
                using (var result = JsonSerialization.DeserializeFromStream(stream, ref container))
                {
                    if (!result.Succeeded)
                    {
                        var assetPath       = AssetDatabase.GetAssetPath(container);
                        var instanceMessage = string.IsNullOrEmpty(assetPath) ? $"in memory container of type {container.GetType().Name}" : assetPath.ToHyperLink();
                        var builder         = new StringBuilder();
                        builder.AppendLine($"Encountered problems while deserializing {instanceMessage}:");
                        foreach (var evt in result.AllEvents)
                        {
                            builder.AppendLine($"    {evt}");
                        }
                        Debug.LogError(builder.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
                return(false);
            }
            container.m_AssetAsJson = !string.IsNullOrEmpty(path) ? File.ReadAllText(path) : null;

            container.Components.RemoveAll(c => null == c);
            return(true);
        }