質問編集履歴

2

コードの変更

2016/12/12 13:08

投稿

harry0707
harry0707

スコア12

test CHANGED
File without changes
test CHANGED
@@ -16,41 +16,13 @@
16
16
 
17
17
  ```C#
18
18
 
19
-
20
-
21
19
  using UnityEngine;
22
20
 
23
21
  using System.Collections;
24
22
 
25
- using System.IO;
26
-
27
- using UnityEngine.UI;
28
23
 
29
24
 
30
-
31
-
32
-
33
- public class PPinch : MonoBehaviour {
25
+ public class Pinch : MonoBehaviour {
34
-
35
-
36
-
37
- public System.Collections.Generic.List<Sprite> sprites = new System.Collections.Generic.List<Sprite> ();
38
-
39
-
40
-
41
- public Image image_main;
42
-
43
-
44
-
45
- Touch touch;
46
-
47
-
48
-
49
- public Vector2 firstPos, secondPos;
50
-
51
-
52
-
53
- float MoveDis=0, BeganDis=0;
54
26
 
55
27
 
56
28
 
@@ -72,143 +44,95 @@
72
44
 
73
45
  void Start () {
74
46
 
75
- }
76
47
 
48
+
49
+ }
50
+
51
+
52
+
77
- // Update is called once per frame
53
+ // Update is called once per frame
78
54
 
79
55
  void Update () {
80
56
 
57
+ if(Input.touchCount >= 2){
58
+
59
+ Touch t1 = Input.GetTouch(0);
60
+
61
+ Touch t2 = Input.GetTouch(1);
62
+
63
+ // 2点タッチ開始時の距離を記憶.
64
+
65
+ if(t2.phase == TouchPhase.Began){
66
+
67
+ backDist = Vector2.Distance(t1.position, t2.position);
68
+
69
+ }
70
+
71
+ // ピンチイン・アウト.
72
+
73
+ else if(t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved){
74
+
75
+ float newDist = Vector2.Distance(t1.position, t2.position);
76
+
77
+ float transScale = (backDist - newDist) / 500.0f;
78
+
79
+ if(transScale != 0){
81
80
 
82
81
 
83
- if (Input.touchCount > 0)
84
82
 
85
- {
83
+ transScale = 1.0f - transScale;
86
84
 
87
- this.touch = Input.touches[0];
88
85
 
89
-
90
86
 
91
- if (this.touch.phase == TouchPhase.Began)
87
+ // スケール変化
92
88
 
93
- {
89
+ Vector3 newScale = transform.localScale;
94
90
 
95
- Debug.Log("Input.touchCount == " + Input.touchCount);
91
+ newScale = newScale * transScale;
96
92
 
97
- switch (Input.touchCount)
98
93
 
99
- {
100
94
 
101
- case 1: // 1本指でタッチ
95
+ // 位置修正
102
96
 
103
- Debug.Log ("Single Touch");
97
+ Vector3 newPosition = transform.localPosition;
104
98
 
105
- firstPos = touch.position;
99
+ newPosition.y = newPosition.y * transScale;
106
100
 
107
- break;
101
+ newPosition.z = newPosition.z * transScale;
108
102
 
109
-
110
103
 
111
- case 2: // 2本指でタッチ
112
104
 
113
- Debug.Log ("Double Touch");
105
+ if(ScaleMin <= newScale.z && newScale.z <= ScaleMax){
114
106
 
115
- secondPos = touch.position;
107
+ transform.localScale = newScale;
116
108
 
117
-
109
+ transform.localPosition -= new Vector3(0, newPosition.y, newPosition.z);
118
110
 
119
- BeganDis = Vector2.Distance (firstPos, secondPos);//距離
111
+ backDist = newDist;
120
-
121
-
122
-
123
- break;
124
-
125
-
126
-
127
- }
128
112
 
129
113
  }
130
114
 
131
-
115
+ }
132
116
 
133
- if (this.touch.phase == TouchPhase.Moved) {
134
-
135
-
136
-
137
- switch (Input.touchCount)
138
-
139
- {
140
-
141
- case 1: // 1本指でタッチ
142
-
143
- Debug.Log ("Single Touch");
144
-
145
- firstPos = touch.position;
146
-
147
- break;
148
-
149
-
150
-
151
- case 2: // 2本指でタッチ
152
-
153
- Debug.Log ("Double Touch");
154
-
155
- secondPos = touch.position;
156
-
157
-
158
-
159
- MoveDis = Vector2.Distance (firstPos, secondPos);//距離
160
-
161
-
162
-
163
- float scale = MoveDis - BeganDis / 100;
164
-
165
-
166
-
167
- RectTransform rt = image_main.gameObject.GetComponent<RectTransform> ();
168
-
169
- rt.localScale = new Vector2 (rt.localScale.x + scale, rt.localScale.y + scale);//拡大縮小
170
-
171
-
172
-
173
- if (scale < 0) {//縮小
174
-
175
-
176
-
177
- rt.localScale = new Vector2 (rt.localScale.x + scale, rt.localScale.y + scale);
178
-
179
-
180
-
181
- } else {
182
-
183
-
184
-
185
-
186
-
187
- }
117
+ }
188
-
189
-
190
-
191
- break;
192
118
 
193
119
  }
194
120
 
195
- }
121
+ }
196
122
 
197
- if (this.touch.phase == TouchPhase.Ended) {
198
123
 
199
-
200
124
 
201
- MoveDis = 0;
202
125
 
203
- BeganDis = 0;
204
126
 
205
- }
127
+
128
+
129
+
130
+
131
+
206
132
 
207
133
  }
208
134
 
209
- }
210
135
 
211
- }
212
136
 
213
137
  ```
214
138
 
@@ -220,15 +144,13 @@
220
144
 
221
145
 
222
146
 
223
- これでデバックをすると、一本指でキャラクターを触ったときは、普通に動くのですが、
224
-
225
- 二本指触り、一動かすと画面におさまりれなくなくらいなってしいます。。。
147
+ これある程ピンチイン・ピンチアウトはできるすが、不安定で、オブジェクトがががくと揺れます。
226
148
 
227
149
 
228
150
 
229
151
 
230
152
 
231
- どのようにコードを書けばいのか、アドバイスをいただければ幸いです
153
+ どのようにコードを書けばきれに拡大・縮小されるのか、アドバイスをいただければ幸いです
232
154
 
233
155
 
234
156
 

1

聞きたいことをより詳細に記入

2016/12/12 13:08

投稿

harry0707
harry0707

スコア12

test CHANGED
File without changes
test CHANGED
@@ -10,8 +10,226 @@
10
10
 
11
11
 
12
12
 
13
+ いまスクリプトにはこのようにかいています
14
+
15
+
16
+
17
+ ```C#
18
+
19
+
20
+
21
+ using UnityEngine;
22
+
23
+ using System.Collections;
24
+
25
+ using System.IO;
26
+
27
+ using UnityEngine.UI;
28
+
29
+
30
+
31
+
32
+
33
+ public class PPinch : MonoBehaviour {
34
+
35
+
36
+
37
+ public System.Collections.Generic.List<Sprite> sprites = new System.Collections.Generic.List<Sprite> ();
38
+
39
+
40
+
41
+ public Image image_main;
42
+
43
+
44
+
45
+ Touch touch;
46
+
47
+
48
+
49
+ public Vector2 firstPos, secondPos;
50
+
51
+
52
+
53
+ float MoveDis=0, BeganDis=0;
54
+
55
+
56
+
57
+ //zoomの許容範囲.
58
+
59
+ public float ScaleMin = 0.2f;
60
+
61
+ public float ScaleMax = 5.0f;
62
+
63
+
64
+
65
+ //直前の2点間の距離.
66
+
67
+ private float backDist = 0.0f;
68
+
69
+
70
+
71
+ // Use this for initialization
72
+
73
+ void Start () {
74
+
75
+ }
76
+
77
+ // Update is called once per frame
78
+
79
+ void Update () {
80
+
81
+
82
+
83
+ if (Input.touchCount > 0)
84
+
85
+ {
86
+
87
+ this.touch = Input.touches[0];
88
+
89
+
90
+
91
+ if (this.touch.phase == TouchPhase.Began)
92
+
93
+ {
94
+
95
+ Debug.Log("Input.touchCount == " + Input.touchCount);
96
+
97
+ switch (Input.touchCount)
98
+
99
+ {
100
+
101
+ case 1: // 1本指でタッチ
102
+
103
+ Debug.Log ("Single Touch");
104
+
105
+ firstPos = touch.position;
106
+
107
+ break;
108
+
109
+
110
+
111
+ case 2: // 2本指でタッチ
112
+
113
+ Debug.Log ("Double Touch");
114
+
115
+ secondPos = touch.position;
116
+
117
+
118
+
119
+ BeganDis = Vector2.Distance (firstPos, secondPos);//距離
120
+
121
+
122
+
123
+ break;
124
+
125
+
126
+
127
+ }
128
+
129
+ }
130
+
131
+
132
+
133
+ if (this.touch.phase == TouchPhase.Moved) {
134
+
135
+
136
+
137
+ switch (Input.touchCount)
138
+
139
+ {
140
+
141
+ case 1: // 1本指でタッチ
142
+
143
+ Debug.Log ("Single Touch");
144
+
145
+ firstPos = touch.position;
146
+
147
+ break;
148
+
149
+
150
+
151
+ case 2: // 2本指でタッチ
152
+
153
+ Debug.Log ("Double Touch");
154
+
155
+ secondPos = touch.position;
156
+
157
+
158
+
159
+ MoveDis = Vector2.Distance (firstPos, secondPos);//距離
160
+
161
+
162
+
163
+ float scale = MoveDis - BeganDis / 100;
164
+
165
+
166
+
167
+ RectTransform rt = image_main.gameObject.GetComponent<RectTransform> ();
168
+
169
+ rt.localScale = new Vector2 (rt.localScale.x + scale, rt.localScale.y + scale);//拡大縮小
170
+
171
+
172
+
173
+ if (scale < 0) {//縮小
174
+
175
+
176
+
177
+ rt.localScale = new Vector2 (rt.localScale.x + scale, rt.localScale.y + scale);
178
+
179
+
180
+
181
+ } else {
182
+
183
+
184
+
185
+
186
+
187
+ }
188
+
189
+
190
+
191
+ break;
192
+
193
+ }
194
+
195
+ }
196
+
197
+ if (this.touch.phase == TouchPhase.Ended) {
198
+
199
+
200
+
201
+ MoveDis = 0;
202
+
203
+ BeganDis = 0;
204
+
205
+ }
206
+
207
+ }
208
+
209
+ }
210
+
211
+ }
212
+
213
+ ```
214
+
215
+
216
+
217
+ みにくいコードで申し訳ないです(;;)
218
+
219
+ いろんなサイトから寄せ集めてきたコードです。。
220
+
221
+
222
+
223
+ これでデバックをすると、一本指でキャラクターを触ったときは、普通に動くのですが、
224
+
225
+ 二本指で触り、一度動かすと、画面におさまりきれなくなるくらいでかくなってしまいます。。。
226
+
227
+
228
+
229
+
230
+
13
- アドバイスをいただければ幸いです
231
+ どのようにコードを書けばいいのか、アドバイスをいただければ幸いです
14
-
15
-
16
-
232
+
233
+
234
+
17
- よろしく願いします
235
+ よろしく願いします