public Vector3 GetGroupPosition(GameObject groupRoot, SpriteAlignment alignment) { Transform t = groupRoot.transform; // Find all the SpriteRenderers that are children of the layer group var spriteList = t.GetComponentsInChildren <SpriteRenderer>(); Vector3 min = new Vector3(float.MaxValue, float.MaxValue); Vector3 max = new Vector3(float.MinValue, float.MinValue); // From the SpriteRenderers, find the size of // the layer group by the bounds of the renderers foreach (var sprite in spriteList) { var bounds = sprite.bounds; min = Vector3.Min(min, bounds.min); max = Vector3.Max(max, bounds.max); } // Calculate the position for this layer group Vector2 pivot = PsdBuilder.GetPivot(alignment); Vector3 pos = Vector3.zero; pos.x = Mathf.Lerp(min.x, max.x, pivot.x); pos.y = Mathf.Lerp(min.y, max.y, pivot.y); return(pos); }
public static Vector3 CalculateGroupPosition(GameObject groupRoot, SpriteAlignment alignment) { Vector2 min = new Vector2(float.MaxValue, float.MaxValue); Vector2 max = new Vector2(float.MinValue, float.MinValue); #if NGUI var tList = groupRoot.GetComponentsInChildren <UIWidget>(); foreach (var rectTransform in tList) { if (rectTransform.gameObject == groupRoot) { continue; } var rectSize = new Vector2(rectTransform.width, rectTransform.height); var rectPivot = NGUIMath.GetPivotOffset(rectTransform.pivot); var calcMin = rectTransform.cachedTransform.position; calcMin.x -= rectSize.x * rectPivot.x; calcMin.y -= rectSize.y * rectPivot.y; var calcMax = calcMin + new Vector3(rectSize.x, rectSize.y); min = Vector2.Min(min, calcMin); max = Vector2.Max(max, calcMax); } #else var tList = groupRoot.GetComponentsInChildren <RectTransform>(); foreach (var rectTransform in tList) { if (rectTransform.gameObject == groupRoot) { continue; } var rectSize = rectTransform.sizeDelta; var rectPivot = rectTransform.pivot; var calcMin = rectTransform.position; calcMin.x -= rectSize.x * rectPivot.x; calcMin.y -= rectSize.y * rectPivot.y; var calcMax = calcMin + new Vector3(rectSize.x, rectSize.y); min = Vector2.Min(min, calcMin); max = Vector2.Max(max, calcMax); } #endif Vector2 pivot = PsdBuilder.GetPivot(alignment); Vector3 pos = Vector3.zero; pos.x = Mathf.Lerp(min.x, max.x, pivot.x); pos.y = Mathf.Lerp(min.y, max.y, pivot.y); return(pos); }
public void AddComponents(int layerIndex, GameObject imageObject, Sprite sprite, TextureImporterSettings settings) { #if NGUI var uiImg = imageObject.AddComponent <UISprite>(); string sprPath = PSDExporter.GetLayerFilename(this.layer); if (sprPath == null) { Debug.LogWarning("Cant find sprite path . layer name :" + this.layer.Name); } else { string spriteName = Path.GetFileNameWithoutExtension(sprPath); NAtlasHelper.UIAtlasData atlas = NAtlasHelper.FindSprite(spriteName); if (atlas != null) { uiImg.atlas = atlas.mainAtlas; UISpriteData spriteData = uiImg.atlas.GetSprite(spriteName); if (spriteData.borderLeft != 0 || spriteData.borderRight != 0 || spriteData.borderBottom != 0 || spriteData.borderTop != 0) { uiImg.type = UIBasicSprite.Type.Sliced; } } uiImg.spriteName = spriteName; } uiImg.depth = layerIndex; uiImg.SetDimensions((int)layer.Rect.width, (int)layer.Rect.height); uiImg.transform.SetAsFirstSibling(); // Vector2 sprPivot = PsdBuilder.GetPivot(settings); // uiImg.pivot = NGUIMath.GetPivot(sprPivot); #else var uiImg = imageObject.AddComponent <Image>(); uiImg.sprite = sprite; uiImg.SetNativeSize(); uiImg.rectTransform.SetAsFirstSibling(); Vector2 sprPivot = PsdBuilder.GetPivot(settings); uiImg.rectTransform.pivot = sprPivot; #endif }