|
| 1 | +using LevelEditorActions; |
| 2 | +using Serializers; |
| 3 | +using System.Collections.Generic; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace EditorExpanded.Editor.Tools |
| 7 | +{ |
| 8 | + class MergeAction : SimplerAction |
| 9 | + { |
| 10 | + private ReferenceMap.Handle<GameObject> originalObjectHandle_; |
| 11 | + private ReferenceMap.Handle<GameObject> newObjectHandle_; |
| 12 | + private byte[] deletedObjectBytes_; |
| 13 | + private GroupAction groupAction_; |
| 14 | + |
| 15 | + private bool nothingtodo = false; |
| 16 | + |
| 17 | + public MergeAction(GameObject[] gameObjects) |
| 18 | + { |
| 19 | + ReferenceMap referenceMap = G.Sys.LevelEditor_.ReferenceMap_; |
| 20 | + |
| 21 | + if (gameObjects.Length > 0) |
| 22 | + { |
| 23 | + //Group.InitData data = new Group.InitData(Vector3.zero, Quaternion.identity, Vector3.one, new Bounds()); |
| 24 | + //this.groupAction_ = new GroupAction(gameObjects.ToArray(), data); |
| 25 | + this.groupAction_ = Group.CreateGroupAction(gameObjects.ToArray(), gameObjects.ToArray()[0]); |
| 26 | + Group group = this.groupAction_.GroupObjects(); |
| 27 | + this.originalObjectHandle_ = referenceMap.GetHandle<GameObject>(group.gameObject); |
| 28 | + this.deletedObjectBytes_ = BinarySerializer.SaveGameObjectToBytes(group.gameObject, Resource.LoadPrefab("Group"), true); |
| 29 | + this.groupAction_.Undo(); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + nothingtodo = true; |
| 34 | + } |
| 35 | + |
| 36 | + newObjectHandle_ = new ReferenceMap.Handle<GameObject>(); |
| 37 | + |
| 38 | + } |
| 39 | + public override string Description_ => "Merges objects."; |
| 40 | + |
| 41 | + public override void Undo() => this.UnMergeObjects(); |
| 42 | + |
| 43 | + public override void Redo() => this.MergeObjects(); |
| 44 | + |
| 45 | + public void MergeObjects() |
| 46 | + { |
| 47 | + if (!nothingtodo) |
| 48 | + { |
| 49 | + |
| 50 | + Group objectGroup = groupAction_.GroupObjects(); |
| 51 | + GameObject groupObject = objectGroup.gameObject; |
| 52 | + |
| 53 | + Vector3 groupPosition = groupObject.GetComponent<Transform>().localPosition; |
| 54 | + Quaternion groupRotation = groupObject.GetComponent<Transform>().localRotation; |
| 55 | + Vector3 groupSize = objectGroup.localBounds_.size; |
| 56 | + |
| 57 | + GameObject[] originalobjs = groupAction_.UngroupObjects(); |
| 58 | + LevelEditor levelEditor = G.Sys.LevelEditor_; |
| 59 | + |
| 60 | + groupAction_.GroupObjects(); |
| 61 | + levelEditor.DeleteGameObject(this.originalObjectHandle_.Get()); |
| 62 | + List<LevelLayer> layersOfObjects = new List<LevelLayer>(); |
| 63 | + LevelLayer origobjlayer = levelEditor.WorkingLevel_.GetLayerOfObject(originalobjs[0]); |
| 64 | + GameObject mergeObject = DuplicateObjectsAction.Duplicate(originalobjs[0]); |
| 65 | + levelEditor.AddGameObject(ref newObjectHandle_, mergeObject, origobjlayer); |
| 66 | + Vector3 mergeObjectPos = mergeObject.GetComponent<Transform>().position; |
| 67 | + Quaternion mergeObjectRot = mergeObject.GetComponent<Transform>().rotation; |
| 68 | + Vector3 mergeObjectScl = mergeObject.GetComponent<Transform>().localScale; |
| 69 | + //mergeObject.GetComponent<Transform>().rotation = Quaternion.identity; |
| 70 | + GroupAction groupAction_2 = Group.CreateGroupAction(new GameObject[] { mergeObject }, mergeObject); |
| 71 | + Group group = groupAction_2.GroupObjects(); |
| 72 | + GameObject mergeObjectGroup = group.gameObject; |
| 73 | + mergeObjectGroup.GetComponent<Transform>().localRotation = mergeObjectRot; |
| 74 | + mergeObjectGroup.GetChild(0).GetComponent<Transform>().position = mergeObjectPos; |
| 75 | + mergeObjectGroup.GetChild(0).GetComponent<Transform>().rotation = mergeObjectRot; |
| 76 | + group.localBounds_ = Group.CalculateBoundsFromImmediateChildren(group); |
| 77 | + //Vector3 objsize = group.localBounds_.size; |
| 78 | + |
| 79 | + Vector3 mergeObjectSize = group.localBounds_.size; |
| 80 | + |
| 81 | + Vector3 newMergeObjectScale = new Vector3(groupSize.x / mergeObjectSize.x, groupSize.y / mergeObjectSize.y, groupSize.z / mergeObjectSize.z); |
| 82 | + |
| 83 | + mergeObjectGroup.GetComponent<Transform>().localPosition = groupPosition; |
| 84 | + mergeObjectGroup.GetComponent<Transform>().localRotation = groupRotation; |
| 85 | + mergeObjectGroup.GetComponent<Transform>().localScale = newMergeObjectScale; |
| 86 | + |
| 87 | + groupAction_2.UngroupObjects(); |
| 88 | + } |
| 89 | + } |
| 90 | + public void UnMergeObjects() |
| 91 | + { |
| 92 | + if (!nothingtodo) |
| 93 | + { |
| 94 | + G.Sys.LevelEditor_.DeleteGameObject(this.newObjectHandle_.Get()); |
| 95 | + |
| 96 | + LevelEditor levelEditor = G.Sys.LevelEditor_; |
| 97 | + |
| 98 | + GameObject newObj = Deserializer.LoadGameObjectFromBytes<BinaryDeserializer>(this.deletedObjectBytes_, levelEditor.ReferenceMap_.GetIDToObjectMap()); |
| 99 | + if ((UnityEngine.Object)newObj == (UnityEngine.Object)null) |
| 100 | + { |
| 101 | + Debug.LogError((object)"Error loading deleted object"); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + levelEditor.AddGameObject(ref this.originalObjectHandle_, newObj, (LevelLayer)null); |
| 106 | + G.Sys.LevelEditor_.SelectObject(newObj); |
| 107 | + if (this.groupAction_ == null) |
| 108 | + return; |
| 109 | + this.groupAction_.groupObjectHandle_ = this.originalObjectHandle_; |
| 110 | + this.groupAction_.UngroupObjects(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | +} |
0 commit comments