├── DuplicateSpecial.cs ├── README.md └── duplicate_special_demo.gif /DuplicateSpecial.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | 4 | [ExecuteInEditMode] 5 | public class DuplicateSpecial : EditorWindow 6 | { 7 | int count; 8 | Vector3 relOffset; 9 | Vector3 conOffset; 10 | 11 | // object to duplicate 12 | GameObject target; 13 | Renderer rend; 14 | 15 | // wrapper object for duplicates 16 | GameObject wrapper; 17 | 18 | [MenuItem("Plugins/Duplicate special")] 19 | static void Init() 20 | { 21 | DuplicateSpecial window = (DuplicateSpecial)GetWindow(typeof(DuplicateSpecial), true, "Duplicate special"); 22 | window.Show(); 23 | } 24 | 25 | void OnGUI() 26 | { 27 | target = Selection.activeGameObject; 28 | if (target == null) 29 | { 30 | ErrorMsg("Please select an object to duplicate from Hierarchy View"); 31 | return; 32 | } 33 | 34 | rend = target.GetComponentInChildren(); 35 | 36 | EditorGUI.BeginChangeCheck(); 37 | 38 | count = EditorGUILayout.IntField("Count", count); 39 | relOffset = EditorGUILayout.Vector3Field("Relative offset", relOffset); 40 | conOffset = EditorGUILayout.Vector3Field("Constant offset", conOffset); 41 | 42 | if (EditorGUI.EndChangeCheck()) 43 | { 44 | Rollback(); // destroy wrapper from previous changes check tick before creating a new one 45 | Apply(); 46 | } 47 | 48 | if (GUILayout.Button("Ok")) Close(); 49 | if (GUILayout.Button("Cancel")) 50 | { 51 | Rollback(); 52 | Close(); 53 | } 54 | } 55 | 56 | private void Apply() 57 | { 58 | wrapper = new GameObject(target.name + "_duplicates_" + count); 59 | 60 | for (int i = 1; i <= count; i++) 61 | { 62 | GameObject clone = Instantiate(target, target.transform.position, target.transform.rotation) as GameObject; 63 | 64 | Vector3 offset = new Vector3(rend.bounds.size.x * relOffset.x + conOffset.x, 65 | rend.bounds.size.y * relOffset.y + conOffset.y, 66 | rend.bounds.size.z * relOffset.z + conOffset.z) 67 | * i; 68 | clone.transform.Translate(offset); 69 | clone.transform.SetParent(wrapper.transform); 70 | } 71 | } 72 | 73 | private void Rollback() 74 | { 75 | DestroyImmediate(wrapper); 76 | } 77 | 78 | private void ErrorMsg(string msg) 79 | { 80 | EditorGUILayout.LabelField(msg); 81 | } 82 | 83 | void OnDestroy() 84 | { 85 | if (count == 0) Rollback(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Duplicate special for Unity 2 | 3 | A tiny Unity Editor script which duplicates objects in a fashion similar to Blender's Array Modifier. 4 | 5 | Place it in your Unity project under Assets/Editor. 6 | 7 | ![My image](https://github.com/marcin-se/unity_duplicate_special/blob/master/duplicate_special_demo.gif) 8 | 9 | -------------------------------------------------------------------------------- /duplicate_special_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riperjack/unity_duplicate_special/661c5368cfe588852d01f53f5799d644462c6c28/duplicate_special_demo.gif --------------------------------------------------------------------------------