miepzerino
2025-03-29 ad79d9ca49274cc660fc2030a071b24314f0f210
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Collections.Generic;
using UnityEngine;
 
namespace Flexalon
{
    /// <summary>
    /// Sometimes, it's useful to generate child objects instead of defining them statically.
    /// The Flexalon Cloner can generate objects from a set of prefabs iteratively or randomly,
    /// and can optionally bind to a data source.
    /// </summary>
    [AddComponentMenu("Flexalon/Flexalon Cloner"), HelpURL("https://www.flexalon.com/docs/cloner")]
    public class FlexalonCloner : MonoBehaviour
    {
        [SerializeField]
        private List<GameObject> _objects;
        /// <summary> Prefabs which should be cloned as children. </summary>
        public List<GameObject> Objects
        {
            get => _objects;
            set { _objects = value; MarkDirty(); }
        }
 
        /// <summary> In which order should prefabs be cloned. </summary>
        public enum CloneTypes
        {
            /// <summary> Clone prefabs in the order they are assigned. </summary>
            Iterative,
 
            /// <summary> Clone prefabs in a random order. </summary>
            Random
        }
 
        [SerializeField]
        private CloneTypes _cloneType = CloneTypes.Iterative;
        /// <summary> In which order should prefabs be cloned. </summary>
        public CloneTypes CloneType
        {
            get => _cloneType;
            set { _cloneType = value; MarkDirty(); }
        }
 
        [SerializeField]
        private uint _count;
        /// <summary> How many clones should be generated. </summary>
        public uint Count
        {
            get => _count;
            set { _count = value; MarkDirty(); }
        }
 
        [SerializeField]
        private int _randomSeed;
        /// <summary> Seed used for the Random clone type, to ensure results remain consistent. </summary>
        public int RandomSeed
        {
                get => _randomSeed;
                set { _randomSeed = value; MarkDirty(); }
        }
 
        [SerializeField]
        private GameObject _dataSource = null;
        /// <summary> Can be an gameObject with a component that implements FlexalonDataSource.
        /// The number of objects cloned is set to the number of items in the Data property. </summary>
        public GameObject DataSource
        {
            get => _dataSource;
            set
            {
                UnhookDataSource();
                _dataSource = value;
                HookDataSource();
                MarkDirty();
            }
        }
 
        [SerializeField, HideInInspector]
        private List<GameObject> _clones = new List<GameObject>();
 
        void OnEnable()
        {
            HookDataSource();
            MarkDirty();
        }
 
        private void HookDataSource()
        {
            if (isActiveAndEnabled && _dataSource != null && _dataSource)
            {
                if (_dataSource.TryGetComponent<DataSource>(out var component))
                {
                    component.DataChanged += MarkDirty;
                }
            }
        }
 
        private void UnhookDataSource()
        {
            if (_dataSource != null && _dataSource)
            {
                if (_dataSource.TryGetComponent<DataSource>(out var component))
                {
                    component.DataChanged -= MarkDirty;
                }
            }
        }
 
        void OnDisable()
        {
            UnhookDataSource();
            MarkDirty();
        }
 
        /// <summary> Forces the cloner to regenerate its clones. </summary>
        public void MarkDirty()
        {
            foreach(var clone in _clones)
            {
                if (Application.isPlaying)
                {
                    Destroy(clone);
                }
                else
                {
                    DestroyImmediate(clone);
                }
            }
 
            _clones.Clear();
 
            if (isActiveAndEnabled && _objects != null && _objects.Count > 0)
            {
                switch (_cloneType)
                {
                    case CloneTypes.Iterative:
                        GenerateIterativeClones();
                        break;
                    case CloneTypes.Random:
                        GenerateRandomClones();
                        break;
                }
            }
        }
 
        private IReadOnlyList<object> GetData()
        {
            if (_dataSource != null && _dataSource)
            {
                return _dataSource.GetComponent<DataSource>()?.Data;
            }
 
            return null;
        }
 
        private void GenerateIterativeClones()
        {
            int i = 0;
            var data = GetData();
            var count = data?.Count ?? (int)_count;
            while (_clones.Count < count)
            {
                GenerateClone(i, data);
                i = (i + 1) % _objects.Count;
            }
        }
 
        private void GenerateRandomClones()
        {
            var random = new System.Random(_randomSeed);
            var data = GetData();
            var count = data?.Count ?? (int)_count;
            while (_clones.Count < count)
            {
                GenerateClone(random.Next(_objects.Count), data);
            }
        }
 
        private void GenerateClone(int index, IReadOnlyList<object> data)
        {
            var clone = Instantiate(_objects[index], Vector3.zero, Quaternion.identity, transform);
            _clones.Add(clone);
 
            if (data != null && clone.TryGetComponent<DataBinding>(out var dataBinding))
            {
                dataBinding.SetData(data[_clones.Count - 1]);
            }
        }
    }
}