public GameObject Unprefabricate(GameObject patternObject) { var groupPrefab = Resources.Load <GameObject>(Constants.GROUP_PREFAB); var prefabGo = Instantiate(groupPrefab, Vector3.zero, Quaternion.identity); var group = prefabGo.GetComponent <Group>(); if (group.IsNotNull()) { var patternGroup = patternObject.GetComponent <Group>(); var pixelsInPatternGroup = patternGroup.pixels; foreach (var pixel in pixelsInPatternGroup) { // PixelManager.instance.AddPixel(pixel); var pixelTransform = pixel.transform; var instancePixel = Instantiate(pixel, pixelTransform.localPosition, pixelTransform.rotation); instancePixel.name = pixel.name; group.AddPixel(instancePixel); pixelManager.AddPixel(instancePixel); } var groups = patternGroup.groupChildren; group.CloneGroup(groups, true); // Compute pivot of group var pixelsInGroup = group.GetPixelsInChildren(); var selectedPoints = pixelsInGroup.Select(x => x.transform.position).ToArray(); var centerPoint = TransformUtility.ComputeCenterPoint(selectedPoints); // var groupPosition = centerPoint.ToVector2().Snap2(); var pivot = group.GetComponentInChildren <GroupPivot>(); pivot.transform.position = new Vector3(centerPoint.x, centerPoint.y, pivot.transform.position.z); } group.SetEnabledPivot(false); return(prefabGo); }
public static Group Create(IEnumerable <Pixel> pixels) { var selectedPoints = pixels.Select(x => x.transform.position).ToArray(); var centerPoint = TransformUtility.ComputeCenterPoint(selectedPoints); var groupPosition = centerPoint.ToVector2().Snap2(); var groupPrefab = Resources.Load <Group>(Constants.GROUP_PREFAB); var group = Instantiate <Group>(groupPrefab, groupPosition, Quaternion.identity); var pivot = group.pivot; pivot.transform.position = new Vector3(centerPoint.x, centerPoint.y, pivot.transform.position.z); group.SetEnabledPivot(false); // set parent for each selected pixel those are without any group var selectedPixelsWithoutGroup = pixels.Where(x => x.group.IsNull()).ToList(); foreach (var pixel in selectedPixelsWithoutGroup) { group.AddPixel(pixel); } // set parent for each group what has any selected pixel var selectedGroups = GetManyGroups(pixels); foreach (var selectedGroup in selectedGroups) { if (selectedGroup.id == group.id) { continue; } // disabling pivot of selected group before set parent for it var pivotOfSelectedGroup = selectedGroup.pivot; pivotOfSelectedGroup.GetComponent <MeshRenderer>().enabled = false; group.AddChildGroup(selectedGroup); } group.SetEnabledPivot(false); selectedGroups = null; selectedPoints = null; groupPrefab = null; return(group); }