teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

編集

2020/09/13 16:37

投稿

tsuki01
tsuki01

スコア1751

answer CHANGED
@@ -1,3 +1,54 @@
1
+ 実装方法は複数パターンあるかもしれませんが、以下の様な方法はいかがでしょうか。
2
+
3
+ **処理概要**
4
+ 1、子オブジェクトのpositionを格納するためのListを宣言
5
+ 2、Start()関数内で、子オブジェクトの初期位置をListに格納
6
+ 3、AllPosiReset()関数実行時に、Listで保持しているpositionを子オブジェクトに再設定
7
+
8
+ **気になる点**
9
+ foreachで子オブジェクトを取得する際、Start()関数とAllPosiReset()関数で必ず同じ順番で取得できるかは私も分かっておりません。。
10
+ もし順番が異なることがある場合、別の子オブジェクトのpositionが適用されてしまうことがあるかもです。
11
+ なので、もしそうなった場合はコメント頂ければと思います。
12
+
13
+
14
+ ```C#
15
+ using System.Collections;
16
+ using System.Collections.Generic;
17
+ using UnityEngine;
18
+
19
+ public class ResetSample : MonoBehaviour
20
+ {
21
+ public Transform ParentModel; // 親オブジェクト格納用
22
+ private List<Vector3> originalPosList; // 元のPosition格納用
23
+
24
+ void Start()
25
+ {
26
+ // リスト生成
27
+ originalPosList = new List<Vector3>();
28
+
29
+ // 子オブジェクトの初期位置をリストに格納
30
+ foreach (Transform child in ParentModel)
31
+ {
32
+ originalPosList.Add(child.transform.localPosition);
33
+ }
34
+ }
35
+
36
+
37
+ public void AllPosiReset()
38
+ {
39
+ // 子オブジェクトの位置をリセット
40
+ int i = 0;
41
+ foreach (Transform child in ParentModel)
42
+ {
43
+ child.transform.localPosition = originalPosList[i];
44
+ i++;
45
+ }
46
+ }
47
+ }
48
+ ```
49
+
50
+ ---
51
+
1
52
  ### 2020.09.14追記
2
53
  階層構造になっているGameObjectのリセット方法はいくつかありますが、今回は [こちら](https://stackoverflow.com/questions/38898035/unity-gameobjects-with-tag-reset-to-start-location-position) を利用した方法を共有させて頂きます。
3
54
 
@@ -111,55 +162,4 @@
111
162
  }
112
163
  }
113
164
 
114
- ```
115
-
116
- ---
117
-
118
- 実装方法は複数パターンあるかもしれませんが、以下の様な方法はいかがでしょうか。
119
-
120
- **処理概要**
121
- 1、子オブジェクトのpositionを格納するためのListを宣言
122
- 2、Start()関数内で、子オブジェクトの初期位置をListに格納
123
- 3、AllPosiReset()関数実行時に、Listで保持しているpositionを子オブジェクトに再設定
124
-
125
- **気になる点**
126
- foreachで子オブジェクトを取得する際、Start()関数とAllPosiReset()関数で必ず同じ順番で取得できるかは私も分かっておりません。。
127
- もし順番が異なることがある場合、別の子オブジェクトのpositionが適用されてしまうことがあるかもです。
128
- なので、もしそうなった場合はコメント頂ければと思います。
129
-
130
-
131
- ```C#
132
- using System.Collections;
133
- using System.Collections.Generic;
134
- using UnityEngine;
135
-
136
- public class ResetSample : MonoBehaviour
137
- {
138
- public Transform ParentModel; // 親オブジェクト格納用
139
- private List<Vector3> originalPosList; // 元のPosition格納用
140
-
141
- void Start()
142
- {
143
- // リスト生成
144
- originalPosList = new List<Vector3>();
145
-
146
- // 子オブジェクトの初期位置をリストに格納
147
- foreach (Transform child in ParentModel)
148
- {
149
- originalPosList.Add(child.transform.localPosition);
150
- }
151
- }
152
-
153
-
154
- public void AllPosiReset()
155
- {
156
- // 子オブジェクトの位置をリセット
157
- int i = 0;
158
- foreach (Transform child in ParentModel)
159
- {
160
- child.transform.localPosition = originalPosList[i];
161
- i++;
162
- }
163
- }
164
- }
165
165
  ```

2

追記

2020/09/13 16:36

投稿

tsuki01
tsuki01

スコア1751

answer CHANGED
@@ -1,3 +1,120 @@
1
+ ### 2020.09.14追記
2
+ 階層構造になっているGameObjectのリセット方法はいくつかありますが、今回は [こちら](https://stackoverflow.com/questions/38898035/unity-gameobjects-with-tag-reset-to-start-location-position) を利用した方法を共有させて頂きます。
3
+
4
+ **大まかな手順**
5
+ 1、全てのGameObjcectを表示状態にする
6
+ 2、対象のGameObjectに対し、独自のタグを設定する(インスペクターからあらかじめ独自のタグを追加しておく必要あり)
7
+ 3、アプリ開始直後のTransformを保持する
8
+ 4、右クリックメニュー内などのイベント内で(任意のイベントで)、Transformを元に戻す
9
+
10
+ **以下サンプル**
11
+ ```C#
12
+ using System.Collections;
13
+ using System.Collections.Generic;
14
+ using UnityEngine;
15
+
16
+ public class StateChangeSample : MonoBehaviour
17
+ {
18
+ // ルートとなるGameObject格納用
19
+ public GameObject rootObj;
20
+
21
+ // GameObjectのTransform類格納用(リセット処理にて利用する)
22
+ private Vector3[] defaultPos;
23
+ private Vector3[] defaultScale;
24
+ private Quaternion[] defaultRot;
25
+ private Transform[] models;
26
+
27
+
28
+ void Start()
29
+ {
30
+ // 全オブジェクトを表示する
31
+ SetActiveRecursively(rootObj, true);
32
+
33
+ // GameObject検索用に、GameObjectに独自のタグを設定する
34
+ // ※インスペクターから、タグを追加しておく必要あり
35
+ SetOriginalTag(rootObj, "Parts");
36
+
37
+ // 開始時のTransformを保持する
38
+ BackUpTransform();
39
+ }
40
+
41
+
42
+ // リセットボタン押下時処理
43
+ // ※こちらではボタン押下時のイベントとして設定
44
+ // 質問者さんの方では、右クリックメニューのイベントなどで「ResetTransform」メソッドを呼んでください
45
+ public void ResetBtnClick()
46
+ {
47
+ // Transformをリセットする
48
+ ResetTransform();
49
+ }
50
+
51
+
52
+ // オブジェクトの表示・非表示切り替え処理
53
+ // ※再帰処理にて、全オブジェクトを対象とする
54
+ public void SetActiveRecursively(GameObject obj, bool state)
55
+ {
56
+ obj.SetActive(state);
57
+ foreach (Transform child in obj.transform)
58
+ {
59
+ SetActiveRecursively(child.gameObject, state);
60
+ }
61
+ }
62
+
63
+
64
+ // タグの追加処理
65
+ void SetOriginalTag(GameObject obj, string tagStr) {
66
+ obj.tag = tagStr;
67
+ foreach (Transform child in obj.transform)
68
+ {
69
+ SetOriginalTag(child.gameObject, tagStr);
70
+ }
71
+ }
72
+
73
+
74
+ // バックアップ処理
75
+ // ※開始直後のTransformを保持する
76
+ void BackUpTransform()
77
+ {
78
+ //Find GameObjects with Model tag
79
+ GameObject[] tempModels = GameObject.FindGameObjectsWithTag("Parts");
80
+
81
+ //Create pos, scale and rot, Transform array size based on sixe of Objects found
82
+ defaultPos = new Vector3[tempModels.Length];
83
+ defaultScale = new Vector3[tempModels.Length];
84
+ defaultRot = new Quaternion[tempModels.Length];
85
+
86
+ models = new Transform[tempModels.Length];
87
+
88
+ //Get original the pos, scale and rot of each Object on the transform
89
+ for (int i = 0; i < tempModels.Length; i++)
90
+ {
91
+ models[i] = tempModels[i].GetComponent<Transform>();
92
+
93
+ defaultPos[i] = models[i].position;
94
+ defaultScale[i] = models[i].localScale;
95
+ defaultRot[i] = models[i].rotation;
96
+ }
97
+ }
98
+
99
+
100
+ // リセット処理
101
+ // ※Transformを、開始直後の状態にリセットする
102
+ void ResetTransform()
103
+ {
104
+ //Restore the all the original pos, scale and rot of each GameOBject
105
+ for (int i = 0; i < models.Length; i++)
106
+ {
107
+ models[i].position = defaultPos[i];
108
+ models[i].localScale = defaultScale[i];
109
+ models[i].rotation = defaultRot[i];
110
+ }
111
+ }
112
+ }
113
+
114
+ ```
115
+
116
+ ---
117
+
1
118
  実装方法は複数パターンあるかもしれませんが、以下の様な方法はいかがでしょうか。
2
119
 
3
120
  **処理概要**

1

修正

2020/09/13 16:32

投稿

tsuki01
tsuki01

スコア1751

answer CHANGED
@@ -29,7 +29,7 @@
29
29
  // 子オブジェクトの初期位置をリストに格納
30
30
  foreach (Transform child in ParentModel)
31
31
  {
32
- originalPosList.Add(child.transform.position);
32
+ originalPosList.Add(child.transform.localPosition);
33
33
  }
34
34
  }
35
35
 
@@ -40,7 +40,7 @@
40
40
  int i = 0;
41
41
  foreach (Transform child in ParentModel)
42
42
  {
43
- child.transform.position = originalPosList[i];
43
+ child.transform.localPosition = originalPosList[i];
44
44
  i++;
45
45
  }
46
46
  }