C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.ComponentModel; 5using System.Runtime.CompilerServices; 6using System.Runtime.InteropServices; 7using System; 8using System.IO; 9using System.Linq; 10 11public class thumb : MonoBehaviour 12{ 13 14 Windows.Kinect.KinectSensor sensor; 15 Windows.Kinect.BodyFrameReader bodyReader; 16 Windows.Kinect.Body[] data = null; 17 18 List<Transform> tt = new List<Transform>(); 19 20 // Use this for initialization 21 void Start() 22 { 23 sensor = Windows.Kinect.KinectSensor.GetDefault(); 24 if (sensor != null) 25 { 26 bodyReader = sensor.BodyFrameSource.OpenReader(); 27 if (!sensor.IsOpen) 28 { 29 sensor.Open(); 30 } 31 32 } 33 } 34 35 static Vector3 GetVector3FromJoint(Windows.Kinect.Joint joint) 36 { 37 return new Vector3(joint.Position.X, joint.Position.Y, joint.Position.Z); 38 } 39 40 41 void RefreshBodyObject(Windows.Kinect.Body body) 42 { 43 bool isGen = false; 44 Debug.Log(tt.Count); 45 if (tt.Count != (int)Windows.Kinect.JointType.SpineMid) 46 { 47 tt.Clear(); 48 isGen = true; 49 print("ボーンを作成します."); 50 } 51 Windows.Kinect.JointType jt = Windows.Kinect.JointType.ThumbRight; 52 53 Windows.Kinect.Joint sourceJoint = body.Joints[jt]; 54 Vector3 tmp = GetVector3FromJoint(sourceJoint) * 20; 55 if (isGen == true) 56 { 57 var t = GameObject.CreatePrimitive(PrimitiveType.Capsule).transform; 58 t.name = jt.ToString(); 59 Debug.Log(jt); 60 t.position = tmp; 61 Debug.Log(tmp); 62 63 if (tmp.y > 0) 64 { 65 t.GetComponent<Renderer>().material.color = Color.blue; 66 } 67 else 68 { 69 t.GetComponent<Renderer>().material.color = Color.red; 70 } 71 72 t.parent = transform; 73 Destroy(t.GetComponent<BoxCollider>()); 74 tt.Add(t); 75 } 76 else 77 { 78 Debug.Log(jt); 79 Transform t = tt[(int)Windows.Kinect.JointType.SpineMid]; 80 Debug.Log(t); 81 if (sourceJoint.TrackingState == Windows.Kinect.TrackingState.Tracked) 82 { 83 t.position = tmp; 84 Debug.Log(tmp); 85 } 86 } 87 ShowList(tt); 88 } 89 90 public void ShowList<T>(List<T> list) 91 { 92 string log = ""; 93 foreach (var content in list.Select((val, idx) => new { val, idx })) 94 { 95 if (content.idx == list.Count - 1) 96 { 97 log += content.val.ToString(); 98 } 99 else 100 { 101 log += content.val.ToString() + ","; 102 } 103 } 104 Debug.Log(log); 105 } 106 107 108 // Update is called once per frame 109 void Update() 110 { 111 var frame = bodyReader.AcquireLatestFrame(); 112 if (frame == null) 113 { 114 return; 115 } 116 if (data == null) 117 { 118 data = new Windows.Kinect.Body[sensor.BodyFrameSource.BodyCount]; 119 } 120 frame.GetAndRefreshBodyData(data); 121 frame.Dispose(); 122 frame = null; 123 124 if (data == null) 125 { 126 return; 127 } 128 129 foreach (var body in data) 130 { 131 if (body == null) 132 { 133 continue; 134 } 135 if (body.IsTracked) 136 { 137 RefreshBodyObject(body); 138 break; 139 } 140 } 141 } 142 143 void OnApplicationQuit() 144 { 145 if (bodyReader != null) 146 { 147 bodyReader.Dispose(); 148 bodyReader = null; 149 } 150 if (sensor != null) 151 { 152 if (sensor.IsOpen) 153 { 154 sensor.Close(); 155 } 156 sensor = null; 157 } 158 } 159} 160 161
前提・実現したいこと
親指の追跡をしたい
発生している問題・エラーメッセージ
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.Transform].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) thumb.RefreshBodyObject (Windows.Kinect.Body body) (at Assets/Scenes/thumb.cs:79) thumb.Update () (at Assets/Scenes/thumb.cs:137)
試したこと
コンソールに掃き出して確認し、引数を合わせよう試みたのですがそれでも引数の範囲外です。というエラーメッセージが出てしまう。
補足情報(FW/ツールのバージョンなど)
unity2018 2.15f1 Personal(x64)
kinectV2
回答1件
あなたの回答
tips
プレビュー