前提・実現したいこと
MediaPipeUnityPluginというライブラリで受け取ったJSONと思われるデータ「value.poselandmarks」を読み込みたいです。
ちなみにvalue.poselandmarks[0]とすると Cannot apply indexing with [] to an expression of type'NormalizedLandmarkList となります。
発生している問題・エラーメッセージ
Assets/Mediapipe/Samples/Scenes/Holistic/HolisticTrackingSolution.cs(143,30): error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<Mediapipe.Landmark>' to 'System.Collections.Generic.List<Landmark>' Assets/Mediapipe/Samples/Scenes/Holistic/HolisticTrackingSolution.cs(144,37): error CS0411: The type arguments for method 'JsonUtility.FromJson<T>(string)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
該当のソースコード
C#
1[Serializable] 2public class Wrapper 3{ 4 public List<Landmark> landmark; 5} 6 7 8[Serializable] 9public class Landmark 10{ 11 public double x; 12 public double y; 13 public double z; 14 public double visibility; 15 public double presence; 16}
C#
1Wrapper wrapper = new Wrapper(); 2wrapper.landmark = new List<Landmark>(); 3 string json = JsonUtility.FromJson(wrapper);
JSON
1{ 2 "landmark": [ 3 { 4 "x": 0.2629511, 5 "y": 0.2393726, 6 "z": -1.63657415, 7 "visibility": 0.9658212, 8 "presence": 0.9954764 9 }, 10 { 11 "x": 0.3084781, 12 "y": 0.103633992, 13 "z": -1.70021892, 14 "visibility": 0.962113857, 15 "presence": 0.994487941 16 }, 17 { 18 "x": 0.335506, 19 "y": 0.09454892, 20 "z": -1.69986653, 21 "visibility": 0.9644682, 22 "presence": 0.9949823 23 }, 24 { 25 "x": 0.369098246, 26 "y": 0.08761243, 27 "z": -1.70031965, 28 "visibility": 0.961571336, 29 "presence": 0.9940049 30 }, 31 { 32 "x": 0.2230778, 33 "y": 0.1299035, 34 "z": -1.5992856, 35 "visibility": 0.9581684, 36 "presence": 0.9923149 37 }, 38 { 39 "x": 0.193480074, 40 "y": 0.133466959, 41 "z": -1.600757, 42 "visibility": 0.9560138, 43 "presence": 0.991605163 44 }, 45 { 46 "x": 0.171469077, 47 "y": 0.136153191, 48 "z": -1.60131109, 49 "visibility": 0.957673132, 50 "presence": 0.989012241 51 }, 52 { 53 "x": 0.4373785, 54 "y": 0.08000301, 55 "z": -1.50350976, 56 "visibility": 0.9793107, 57 "presence": 0.9952773 58 }, 59 { 60 "x": 0.212699383, 61 "y": 0.139021873, 62 "z": -1.05882573, 63 "visibility": 0.973896444, 64 "presence": 0.9913624 65 }, 66 { 67 "x": 0.347007751, 68 "y": 0.28568995, 69 "z": -1.51577044, 70 "visibility": 0.973242939, 71 "presence": 0.9958645 72 }, 73 { 74 "x": 0.263258874, 75 "y": 0.342737257, 76 "z": -1.38710141, 77 "visibility": 0.967380345, 78 "presence": 0.9933749 79 }, 80 { 81 "x": 0.775076747, 82 "y": 0.4852151, 83 "z": -1.18634164, 84 "visibility": 0.9782643, 85 "presence": 0.9797115 86 }, 87 { 88 "x": 0.163272172, 89 "y": 0.5029171, 90 "z": -0.47282052, 91 "visibility": 0.97782445, 92 "presence": 0.987057865 93 }, 94 { 95 "x": 0.793088734, 96 "y": 1.024656, 97 "z": -1.80261481, 98 "visibility": 0.134435534, 99 "presence": 0.0235567279 100 }, 101 { 102 "x": 0.0483283177, 103 "y": 0.980344236, 104 "z": -0.133183047, 105 "visibility": 0.1623759, 106 "presence": 0.07004051 107 }, 108 { 109 "x": 0.48451978, 110 "y": 0.5688188, 111 "z": -2.36961722, 112 "visibility": 0.54387033, 113 "presence": 0.6797247 114 }, 115 { 116 "x": 0.103113785, 117 "y": 1.2875098, 118 "z": -0.3646039, 119 "visibility": 0.552444041, 120 "presence": 0.1036061 121 }, 122 { 123 "x": 0.470187962, 124 "y": 0.44396466, 125 "z": -2.488359, 126 "visibility": 0.620796144, 127 "presence": 0.8008424 128 }, 129 { 130 "x": 0.09318472, 131 "y": 1.38178229, 132 "z": -0.392183483, 133 "visibility": 0.6260255, 134 "presence": 0.1431908 135 }, 136 { 137 "x": 0.4824732, 138 "y": 0.324523479, 139 "z": -2.33891535, 140 "visibility": 0.6467453, 141 "presence": 0.894325435 142 }, 143 { 144 "x": 0.09838327, 145 "y": 1.30869842, 146 "z": -0.425549358, 147 "visibility": 0.665143549, 148 "presence": 0.221739963 149 }, 150 { 151 "x": 0.478813261, 152 "y": 0.395430446, 153 "z": -2.30541086, 154 "visibility": 0.6197953, 155 "presence": 0.8808902 156 }, 157 { 158 "x": 0.122083664, 159 "y": 1.27752328, 160 "z": -0.3995623, 161 "visibility": 0.6609248, 162 "presence": 0.189134523 163 }, 164 { 165 "x": 0.660822451, 166 "y": 1.64620316, 167 "z": -0.2525036, 168 "visibility": 0.001579855, 169 "presence": 0.000254729151 170 }, 171 { 172 "x": 0.2482064, 173 "y": 1.63240278, 174 "z": 0.256049871, 175 "visibility": 0.00164249644, 176 "presence": 0.000183555851 177 }, 178 { 179 "x": 0.627968967, 180 "y": 2.51957822, 181 "z": 0.227060512, 182 "visibility": 0.00255248672, 183 "presence": 0.0000972254857 184 }, 185 { 186 "x": 0.238794729, 187 "y": 2.50018477, 188 "z": 0.713123441, 189 "visibility": 0.00345917279, 190 "presence": 0.0000796253153 191 }, 192 { 193 "x": 0.6412408, 194 "y": 3.25620317, 195 "z": 1.463341, 196 "visibility": 0.000202769661, 197 "presence": 0.0000034801692 198 }, 199 { 200 "x": 0.277073175, 201 "y": 3.26306319, 202 "z": 1.62666261, 203 "visibility": 0.000215640757, 204 "presence": 0.000006367081 205 }, 206 { 207 "x": 0.666566551, 208 "y": 3.36892033, 209 "z": 1.58773875, 210 "visibility": 0.000216238885, 211 "presence": 0.00000421156847 212 }, 213 { 214 "x": 0.296536863, 215 "y": 3.380107, 216 "z": 1.72133565, 217 "visibility": 0.0004053733, 218 "presence": 0.000008458709 219 }, 220 { 221 "x": 0.536378562, 222 "y": 3.55772543, 223 "z": 1.13668084, 224 "visibility": 0.00027576054, 225 "presence": 0.00000514463363 226 }, 227 { 228 "x": 0.292625755, 229 "y": 3.555444, 230 "z": 1.15551078, 231 "visibility": 0.000393349124, 232 "presence": 0.0000112516 233 } 234 ] 235}
試したこと
1. Serializable属性
UnityでJsonオブジェクトの配列読み込みができない のように読み込み先のクラスにSerializable属性をつけた
2. Wrapperクラス
【Unity】JsonUtilityを使ったJson化の ルートがList型のデータをJSON化する場合は他のクラスでラップする のようにWrapperクラスを作った
- 型の指定を追加
string json = JsonUtility.FromJson<List>(wrapper);
補足情報(FW/ツールのバージョンなど)
macOS Big Sur 11.6
unity 2020.3.8f1