当前位置:   article > 正文

Unity编辑器扩展-Json转c#类_unity sirenix

unity sirenix

该工具的作用是输入json文本,生成对应的c#解析类型。

在这里插入图片描述
这个功能其实有很多在线json转换网页都能实现,我就是闲的时候想省掉打开网页这一步骤…
而且网页转换了copy过来也要自己改类名,因为网页生成的类名是固定的,这里我也做了自动化处理。

好了上干货

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;

public class Yjj_JosnEditor : OdinEditorWindow
{
    [MultiLineProperty(10),HideLabel,Title("json",TitleAlignment = TitleAlignments.Centered)]
    public string json;
    [MenuItem("Yjj/Json工具")]
    private static void Open()
    {
        GetWindow<Yjj_JosnEditor>().Show();
    }
    [LabelText("生成的类型前缀")]
    public string classHead;
    [LabelText("region注释")]
    public string regionName = "json数据类";
    [MultiLineProperty(10), HideLabel, Title("生成结果", TitleAlignment = TitleAlignments.Centered)]
    public string result;
    [Button]
    private void Generate()
    {
        var obj = JsonConvert.DeserializeObject<JObject>(json);
        var sb = new StringBuilder();
        sb.AppendLine("");
        sb.AppendLine($"#region {regionName}");
        Parse(obj, sb);
        sb.AppendLine("#endregion");
        result = sb.ToString();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="sb"></param>
    /// <param name="className"></param>
    private void Parse(JObject obj,StringBuilder sb,string className = "Root")
    {
        if (obj == null) return;
        List<JObject> objs = new List<JObject>();
        List<string> names = new List<string>();
        sb.AppendLine($"public class {classHead}{className}");
        sb.AppendLine("{");
        foreach (var p in obj.Properties())
        {
            if(p.Value.Type!= JTokenType.Array)
            {
                //Debug.Log($"{p.Name}  {p.Value.Type}  {p.Value}");
                string type = "string";
                switch (p.Value.Type)
                {
                    case JTokenType.Object:
                        var cm = UpperFirst(p.Name);
                        sb.AppendLine($"    public {classHead}{cm} {p.Name};");
                        names.Add(cm);
                        objs.Add(p.Value as JObject);
                        continue;
                    case JTokenType.Integer:
                        if((long)p.Value>int.MaxValue)
                        {
                            type = "long";
                        }
                        else
                        {
                            type = "int";
                        }
                        break;
                    case JTokenType.Float:
                        type = "float";
                        break;
                    case JTokenType.Boolean:
                        type = "bool";
                        break;
                    case JTokenType.TimeSpan:
                        type = "DateTime";
                        break;
                }
                if(p.HasValues && type == "string")
                {
                    sb.AppendLine("    /// <summary>");
                    sb.AppendLine($"    /// {p.Value}");
                    sb.AppendLine("    /// </summary>");
                }
                sb.AppendLine($"    public {type} {p.Name};");
            }
            else
            {
                var arr = p.Value as JArray;
                if (arr.Count > 0)
                {
                    var arrValue = arr[0];
                    switch (arrValue.Type)
                    {
                        case JTokenType.Object:
                            var cmcmm = UpperFirst(p.Name);
                            sb.AppendLine($"    public List<{classHead}{cmcmm}> {p.Name};");
                            names.Add(cmcmm);
                            objs.Add(arrValue as JObject);
                            break;
                        case JTokenType.String:
                            sb.AppendLine($"    public List<string> {p.Name};");
                            break;
                        case JTokenType.Integer:
                            sb.AppendLine($"    public List<int> {p.Name};");
                            break;
                        case JTokenType.Float:
                            sb.AppendLine($"    public List<float> {p.Name};");
                            break;
                        case JTokenType.Boolean:
                            sb.AppendLine($"    public List<bool> {p.Name};");
                            break;

                    } 
                }
                else
                {
                    sb.AppendLine($"    public List<string> {p.Name};");
                    Debug.Log($"json数组:{p.Name}为空");
                }
            
            }

        }
        sb.AppendLine("}");
        for (int i = 0; i < objs.Count; i++)
        {
            Parse(objs[i], sb, names[i]);
        }
    }
    public static string UpperFirst(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return "";
        }
        var f = str.Substring(0, 1);
        f = f.ToUpper();
        return f + str.Substring(1, str.Length - 1);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147

用到了odin和NewtonJson

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/123487
推荐阅读
相关标签
  

闽ICP备14008679号