回答編集履歴

7

例外処理の修正

2016/10/30 12:43

投稿

退会済みユーザー
test CHANGED
@@ -24,6 +24,8 @@
24
24
 
25
25
 
26
26
 
27
+ [RequireComponent(typeof(Camera))]
28
+
27
29
  public class MoveCanvas : MonoBehaviour {
28
30
 
29
31
  public Canvas canvas;
@@ -38,37 +40,43 @@
38
40
 
39
41
  public void MoveToCamera() {
40
42
 
41
- if (canvas != null && canvas.renderMode != RenderMode.WorldSpace) return;
43
+ if (canvas != null) {
42
44
 
43
- var _camera = GetComponent<Camera>();
45
+ if (canvas.renderMode == RenderMode.WorldSpace) {
44
46
 
45
- var _cameraRot = GetComponent<Transform>().localRotation;
47
+ var _camera = GetComponent<Camera>();
46
48
 
47
- var _canvasRectTf = canvas.GetComponent<RectTransform>();
49
+ var _cameraRot = GetComponent<Transform>().localRotation;
48
50
 
49
- var _pivot = _canvasRectTf.pivot;
51
+ var _canvasRectTf = canvas.GetComponent<RectTransform>();
50
52
 
51
- var _sizeDelta = _canvasRectTf.sizeDelta;
53
+ var _pivot = _canvasRectTf.pivot;
52
54
 
53
- var _pos = Vector3.zero;
55
+ var _sizeDelta = _canvasRectTf.sizeDelta;
54
56
 
55
- _pos.x = (_sizeDelta.x / -2f) + (_sizeDelta.x) * _pivot.x;
57
+ var _pos = Vector3.zero;
56
58
 
57
- _pos.y = (_sizeDelta.y / -2f) + (_sizeDelta.y) * _pivot.y;
59
+ _pos.x = (_sizeDelta.x / -2f) + (_sizeDelta.x) * _pivot.x;
58
60
 
59
- _pos.z = distance;
61
+ _pos.y = (_sizeDelta.y / -2f) + (_sizeDelta.y) * _pivot.y;
60
62
 
61
- prevCanvasPosition = _canvasRectTf.localPosition;
63
+ _pos.z = distance;
62
64
 
63
- prevCanvasRotation = _canvasRectTf.localRotation;
65
+ prevCanvasPosition = _canvasRectTf.localPosition;
64
66
 
65
- var _matrix = _camera.cameraToWorldMatrix;
67
+ prevCanvasRotation = _canvasRectTf.localRotation;
66
68
 
67
- var _point = _matrix.MultiplyPoint(_pos);
69
+ var _matrix = _camera.cameraToWorldMatrix;
68
70
 
69
- _canvasRectTf.localPosition = _point;
71
+ var _point = _matrix.MultiplyPoint(_pos);
70
72
 
73
+ _canvasRectTf.localPosition = _point;
74
+
71
- _canvasRectTf.localRotation = _cameraRot;
75
+ _canvasRectTf.localRotation = _cameraRot;
76
+
77
+ }
78
+
79
+ }
72
80
 
73
81
  }
74
82
 
@@ -76,13 +84,19 @@
76
84
 
77
85
  public void BackToPrevPosition() {
78
86
 
79
- if (canvas != null && canvas.renderMode != RenderMode.WorldSpace) return;
87
+ if (canvas != null) {
80
88
 
81
- var _canvasRectTf = canvas.GetComponent<RectTransform>();
89
+ if (canvas.renderMode == RenderMode.WorldSpace) {
82
90
 
83
- _canvasRectTf.localPosition = prevCanvasPosition;
91
+ var _canvasRectTf = canvas.GetComponent<RectTransform>();
84
92
 
93
+ _canvasRectTf.localPosition = prevCanvasPosition;
94
+
85
- _canvasRectTf.localRotation = prevCanvasRotation;
95
+ _canvasRectTf.localRotation = prevCanvasRotation;
96
+
97
+ }
98
+
99
+ }
86
100
 
87
101
  }
88
102
 

6

誤字の修正

2016/10/30 12:43

投稿

退会済みユーザー
test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
  var _sizeDelta = _canvasRectTf.sizeDelta;
52
52
 
53
- var _pos = Vector.zero;
53
+ var _pos = Vector3.zero;
54
54
 
55
55
  _pos.x = (_sizeDelta.x / -2f) + (_sizeDelta.x) * _pivot.x;
56
56
 

5

スクリプトの修正

2016/10/30 09:41

投稿

退会済みユーザー
test CHANGED
@@ -49,6 +49,8 @@
49
49
  var _pivot = _canvasRectTf.pivot;
50
50
 
51
51
  var _sizeDelta = _canvasRectTf.sizeDelta;
52
+
53
+ var _pos = Vector.zero;
52
54
 
53
55
  _pos.x = (_sizeDelta.x / -2f) + (_sizeDelta.x) * _pivot.x;
54
56
 

4

スクリプトの修正

2016/10/30 09:23

投稿

退会済みユーザー
test CHANGED
@@ -48,27 +48,11 @@
48
48
 
49
49
  var _pivot = _canvasRectTf.pivot;
50
50
 
51
- var _c = new Vector3[4];
51
+ var _sizeDelta = _canvasRectTf.sizeDelta;
52
52
 
53
- _canvasRectTf.GetWorldCorners(_c);
53
+ _pos.x = (_sizeDelta.x / -2f) + (_sizeDelta.x) * _pivot.x;
54
54
 
55
- var _pos = Vector3.zero;
56
-
57
- var _left = _c[0].x < _c[3].x ? _c[0].x : _c[3].x;
58
-
59
- var _right = _c[0].x < _c[3].x ? _c[3].x : _c[0].x;
60
-
61
- var _top = _c[0].y < _c[1].y ? _c[1].y : _c[0].y;
62
-
63
- var _bottom = _c[0].y < _c[1].y ? _c[0].y : _c[1].y;
64
-
65
- var _width = _right - _left;
66
-
67
- var _height = _top - _bottom;
68
-
69
- _pos.x = (_width / -2f) + (_width) * _pivot.x;
70
-
71
- _pos.y = (_height / -2f) + (_height) * _pivot.y;
55
+ _pos.y = (_sizeDelta.y / -2f) + (_sizeDelta.y) * _pivot.y;
72
56
 
73
57
  _pos.z = distance;
74
58
 

3

スクリプトの修正

2016/10/29 16:38

投稿

退会済みユーザー
test CHANGED
@@ -38,29 +38,51 @@
38
38
 
39
39
  public void MoveToCamera() {
40
40
 
41
- if (canvas != null){
41
+ if (canvas != null && canvas.renderMode != RenderMode.WorldSpace) return;
42
42
 
43
- if (canvas.renderMode != RenderMode.WorldSpace) return;
43
+ var _camera = GetComponent<Camera>();
44
44
 
45
- var _canvasRectTf = canvas.GetComponent<RectTransform>();
45
+ var _cameraRot = GetComponent<Transform>().localRotation;
46
46
 
47
- prevCanvasPosition = _canvasRectTf.localPosition;
47
+ var _canvasRectTf = canvas.GetComponent<RectTransform>();
48
48
 
49
- prevCanvasRotation = _canvasRectTf.localRotation;
49
+ var _pivot = _canvasRectTf.pivot;
50
50
 
51
- var _camera = GetComponent<Camera>();
51
+ var _c = new Vector3[4];
52
52
 
53
- var _cameraRot = GetComponent<Transform>().localRotation;
53
+ _canvasRectTf.GetWorldCorners(_c);
54
54
 
55
- var _matrix = _camera.cameraToWorldMatrix;
55
+ var _pos = Vector3.zero;
56
56
 
57
- var _point = _matrix.MultiplyPoint(new Vector3(-_canvasRectTf.sizeDelta.x / 2f, 0f, distance));
57
+ var _left = _c[0].x < _c[3].x ? _c[0].x : _c[3].x;
58
58
 
59
- _canvasRectTf.localPosition = _point;
59
+ var _right = _c[0].x < _c[3].x ? _c[3].x : _c[0].x;
60
60
 
61
- _canvasRectTf.localRotation = _cameraRot;
61
+ var _top = _c[0].y < _c[1].y ? _c[1].y : _c[0].y;
62
62
 
63
+ var _bottom = _c[0].y < _c[1].y ? _c[0].y : _c[1].y;
64
+
65
+ var _width = _right - _left;
66
+
67
+ var _height = _top - _bottom;
68
+
69
+ _pos.x = (_width / -2f) + (_width) * _pivot.x;
70
+
71
+ _pos.y = (_height / -2f) + (_height) * _pivot.y;
72
+
63
- }
73
+ _pos.z = distance;
74
+
75
+ prevCanvasPosition = _canvasRectTf.localPosition;
76
+
77
+ prevCanvasRotation = _canvasRectTf.localRotation;
78
+
79
+ var _matrix = _camera.cameraToWorldMatrix;
80
+
81
+ var _point = _matrix.MultiplyPoint(_pos);
82
+
83
+ _canvasRectTf.localPosition = _point;
84
+
85
+ _canvasRectTf.localRotation = _cameraRot;
64
86
 
65
87
  }
66
88
 
@@ -68,17 +90,13 @@
68
90
 
69
91
  public void BackToPrevPosition() {
70
92
 
71
- if (canvas != null){
93
+ if (canvas != null && canvas.renderMode != RenderMode.WorldSpace) return;
72
94
 
73
- if(canvas.renderMode != RenderMode.WorldSpace) return;
95
+ var _canvasRectTf = canvas.GetComponent<RectTransform>();
74
96
 
75
- var _canvasRectTf = canvas.GetComponent<RectTransform>();
97
+ _canvasRectTf.localPosition = prevCanvasPosition;
76
98
 
77
- _canvasRectTf.localPosition = prevCanvasPosition;
78
-
79
- _canvasRectTf.localRotation = prevCanvasRotation;
99
+ _canvasRectTf.localRotation = prevCanvasRotation;
80
-
81
- }
82
100
 
83
101
  }
84
102
 

2

スクリプトの修正

2016/10/29 14:55

投稿

退会済みユーザー
test CHANGED
@@ -38,25 +38,29 @@
38
38
 
39
39
  public void MoveToCamera() {
40
40
 
41
- if (canvas != null || canvas.renderMode != RenderMode.WorldSpace) return;
41
+ if (canvas != null){
42
42
 
43
- var _canvasRectTf = canvas.GetComponent<RectTransform>();
43
+ if (canvas.renderMode != RenderMode.WorldSpace) return;
44
44
 
45
- prevCanvasPosition = _canvasRectTf.localPosition;
45
+ var _canvasRectTf = canvas.GetComponent<RectTransform>();
46
46
 
47
- prevCanvasRotation = _canvasRectTf.localRotation;
47
+ prevCanvasPosition = _canvasRectTf.localPosition;
48
48
 
49
- var _camera = GetComponent<Camera>();
49
+ prevCanvasRotation = _canvasRectTf.localRotation;
50
50
 
51
- var _cameraRot = GetComponent<Transform>().localRotation;
51
+ var _camera = GetComponent<Camera>();
52
52
 
53
- var _matrix = _camera.cameraToWorldMatrix;
53
+ var _cameraRot = GetComponent<Transform>().localRotation;
54
54
 
55
- var _point = _matrix.MultiplyPoint(new Vector3(-_canvasRectTf.sizeDelta.x / 2f, 0f, distance));
55
+ var _matrix = _camera.cameraToWorldMatrix;
56
56
 
57
- _canvasRectTf.localPosition = _point;
57
+ var _point = _matrix.MultiplyPoint(new Vector3(-_canvasRectTf.sizeDelta.x / 2f, 0f, distance));
58
58
 
59
+ _canvasRectTf.localPosition = _point;
60
+
59
- _canvasRectTf.localRotation = _cameraRot;
61
+ _canvasRectTf.localRotation = _cameraRot;
62
+
63
+ }
60
64
 
61
65
  }
62
66
 
@@ -64,13 +68,17 @@
64
68
 
65
69
  public void BackToPrevPosition() {
66
70
 
67
- if (canvas != null || canvas.renderMode != RenderMode.WorldSpace) return;
71
+ if (canvas != null){
68
72
 
69
- var _canvasRectTf = canvas.GetComponent<RectTransform>();
73
+ if(canvas.renderMode != RenderMode.WorldSpace) return;
70
74
 
71
- _canvasRectTf.localPosition = prevCanvasPosition;
75
+ var _canvasRectTf = canvas.GetComponent<RectTransform>();
72
76
 
77
+ _canvasRectTf.localPosition = prevCanvasPosition;
78
+
73
- _canvasRectTf.localRotation = prevCanvasRotation;
79
+ _canvasRectTf.localRotation = prevCanvasRotation;
80
+
81
+ }
74
82
 
75
83
  }
76
84
 

1

情報の追加

2016/10/28 22:38

投稿

退会済みユーザー
test CHANGED
@@ -7,6 +7,8 @@
7
7
  `Assets/Editor`ディレクトリに置けばカメラのインスペクタにMoveとBackのボタンが表示されるので、
8
8
 
9
9
  `MoveCanvas`スクリプトの `canvas`に移動したいCanvasを設定して、Moveボタンでキャンバスをカメラの中央に移動、Backボタンで移動前の位置に戻ります。
10
+
11
+ `distance`はカメラからの距離になります。正ではく、負の値なので注意してください。
10
12
 
11
13
 
12
14