Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
GameObjectCloner.cs
Go to the documentation of this file.
1 using UnityEngine;
2 
3 namespace droid.Runtime.Utilities.Misc.Procedural {
7  public class GameObjectCloner : MonoBehaviour {
8  [SerializeField] GameObject[] _clones = null;
9  [SerializeField] Vector3 _initial_offset = new Vector3(0, 0, 0);
10  [SerializeField] [Range(0, 99)] int _num_clones = 0;
11  [SerializeField] Vector3 _offset = new Vector3(20, 0, 20);
12  [SerializeField] GameObject _prefab = null;
13 
14  void Start() { this.InstantiateClones(); }
15 
16  void InstantiateClones() {
17  if (this._clones.Length > 0) {
18  this.ClearClones();
19  }
20 
21  var clone_id = 0;
22  this._clones = new GameObject[this._num_clones];
23  if (this._prefab) {
24  var clone_coords = NeodroidUtilities.SnakeSpaceFillingGenerator(this._num_clones);
25  foreach (var c in clone_coords) {
26  var go = Instantiate(this._prefab,
27  this._initial_offset + Vector3.Scale(this._offset, c),
28  Quaternion.identity,
29  this.transform);
30  go.name = $"{go.name}{clone_id}";
31  this._clones[clone_id] = go;
32  clone_id++;
33  }
34  }
35  }
36 
37  void ClearClones() {
38  foreach (var clone in this._clones) {
39  Destroy(clone);
40  }
41  }
42 
43  void Update() {
44  if (this._num_clones != this._clones.Length) {
45  this.InstantiateClones();
46  }
47  }
48  }
49 }