void FitCanvas() { RectTransform trans = GetComponent <RectTransform>(); mLastSetSize = trans.rect.size; if (FitAspectCanvas == false) { return; } ChartMagin margin = MarginLink; if (mFixPosition != null) { ChartCommon.SafeDestroy(mFixPosition); } GameObject fixPosition = new GameObject(); mFixPosition = fixPosition; ChartCommon.HideObject(fixPosition, hideHierarchy); // fixPosition.AddComponent<ChartItem>(); // var fixPositionRect = fixPosition.GetComponent<RectTransform>(); // fixPositionRect.anchorMin = new Vector2(1f, 1f); // fixPositionRect.anchorMax = new Vector2(1f, 1f); fixPosition.transform.position = transform.position; while (gameObject.transform.childCount > 0) { transform.GetChild(0).SetParent(fixPosition.transform, false); } fixPosition.transform.SetParent(transform, false); fixPosition.transform.localScale = new Vector3(1f, 1f, 1f); float totalWidth = TotalWidthLink; // + margin.Left + margin.Right; float totalHeight = TotalHeightLink; // + margin.Top + margin.Bottom; float widthScale = (trans.rect.size.x - margin.Left - margin.Right) / totalWidth; float heightScale = (trans.rect.size.y - margin.Top - margin.Bottom) / totalHeight; float uniformScale = Math.Min(widthScale, heightScale); fixPosition.transform.localScale = new Vector3(uniformScale, uniformScale, uniformScale); if (MaintainLabelSize) { TextController.SetInnerScale(1f / uniformScale); } else { TextController.SetInnerScale(1f); } Vector3 offs = CanvasFitOffset; fixPosition.transform.localPosition = new Vector3(-totalWidth * uniformScale * offs.x + Mathf.Lerp(margin.Left, -margin.Right, offs.x), -totalHeight * uniformScale * offs.y - Mathf.Lerp(margin.Top, -margin.Bottom, offs.y), 0f); }
void FitCanvas() { RectTransform trans = GetComponent <RectTransform>(); mLastSetSize = trans.rect.size; if (ShouldFitCanvas == false) { return; } // if (FitAspectCanvas == FitType.None) // return; ChartMagin margin = MarginLink; if (mFixPosition != null) { ChartCommon.SafeDestroy(mFixPosition); } GameObject fixPosition = new GameObject(); var fixRect = fixPosition.AddComponent <RectTransform>(); mFixPosition = fixPosition; ChartCommon.HideObject(fixPosition, hideHierarchy); fixPosition.AddComponent <ChartItem>(); float totalWidth = TotalWidthLink; // + margin.Left + margin.Right; float totalHeight = TotalHeightLink; // + margin.Top + margin.Bottom; fixRect.SetParent(transform, false); fixRect.localPosition = Vector3.zero; fixRect.anchorMin = new Vector2(0f, 0f); fixRect.anchorMax = new Vector2(0f, 0f); fixRect.pivot = new Vector2(0f, 0f); fixRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, totalWidth); fixRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, totalHeight); float xAnchor = 0.5f; float yAnchor = 0.5f; if (FitAlignCanvas == FitAlign.StartXCenterY || FitAlignCanvas == FitAlign.StartXStartY) { xAnchor = 0f; } if (FitAlignCanvas == FitAlign.CenterXStartY || FitAlignCanvas == FitAlign.StartXStartY) { yAnchor = 0f; } fixPosition.transform.localScale = new Vector3(1f, 1f, 1f); fixPosition.transform.SetSiblingIndex(0); toMove.Clear(); for (int i = 0; i < gameObject.transform.childCount; i++) { var child = trans.GetChild(i).gameObject; if (child == null) { continue; } if (child == fixPosition) { continue; } if (child.GetComponent <AnyChart>() != null) { continue; } if (child.GetComponent <ChartItem>() == null) { continue; } toMove.Add(child); } foreach (GameObject obj in toMove) { obj.transform.SetParent(fixPosition.transform, false); } toMove.Clear(); fixRect.anchorMin = new Vector2(xAnchor, yAnchor); fixRect.anchorMax = new Vector2(xAnchor, yAnchor); fixRect.pivot = new Vector2(xAnchor, yAnchor); if (totalWidth <= 0 || TotalHeight <= 0) { return; } float widthScale = (trans.rect.size.x - margin.Left - margin.Right) / totalWidth; float heightScale = (trans.rect.size.y - margin.Top - margin.Bottom) / totalHeight; float uniformScale = Math.Min(widthScale, heightScale); if (FitAspectCanvas == FitType.Height) { uniformScale = heightScale; } else if (FitAspectCanvas == FitType.Width) { uniformScale = widthScale; } else if (FitAspectCanvas == FitType.None) { uniformScale = 1f; } fixRect.localScale = new Vector3(uniformScale, uniformScale, 1f); if (MaintainLabelSize) { TextController.SetInnerScale(1f / uniformScale); } else { TextController.SetInnerScale(1f); } fixRect.anchoredPosition = new Vector3(Mathf.Lerp(margin.Left, -margin.Right, xAnchor), Mathf.Lerp(margin.Bottom, -margin.Top, yAnchor), 0f); }