private void DrawCreateExtensions() { if (constructorExtensions.Length == 0) { return; } GUI.enabled = selectedGroup != null; bool doCreate; using (new EditorGUILayout.HorizontalScope(GUILayout.Height(30f))) { extIndex = EditorGUILayout.Popup(extLabel, extIndex, constructorNames); bool canBuild = false; GUIContent createBtn = GUIContent.none; if (extIndex > -1 && extIndex < constructorExtensions.Length) { createBtn = constructorNames[extIndex]; canBuild = constructorExtensions[extIndex].CanBuild(Selection.activeGameObject); } GUI.enabled = canBuild && selectedGroup != null; doCreate = GUILayout.Button(createBtn, GUILayout.ExpandHeight(true)); } if (doCreate) { IPsdConstructor constructor = constructorExtensions[extIndex]; PsdBuilder.BuildPsd(Selection.activeGameObject, selectedGroup, settings, fileInfo, createAlign, constructor); } GUI.enabled = true; }
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 Vector3 GetLayerPosition(Rect layerSize, Vector2 layerPivot, float pixelsToUnitSize) { Vector3 layerPos = PsdBuilder.CalculateLayerPosition(layerSize, layerPivot); // Because this constructor is working with Unity sprites, // scale the layer position by the pixels to unit size layerPos /= pixelsToUnitSize; return(layerPos); }
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 }
private void HandleHierarchyItem(int instanceId, Rect selectionRect) { if (!isDragging || selectedGroup == null) { return; } if (selectionRect.Contains(Event.current.mousePosition)) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; if (Event.current.type == EventType.DragPerform) { var obj = EditorUtility.InstanceIDToObject(instanceId); GameObject targetObject = (GameObject)obj; if (targetObject == null) { return; } DragAndDrop.AcceptDrag(); isDragging = false; var canvas = targetObject.GetComponentInParent <Canvas>(); bool isUi = (canvas != null); if (isUi) { PsdBuilder.BuildUiImages(targetObject, selectedGroup, settings, fileInfo, createAlign); } else { PsdBuilder.BuildSprites(targetObject, selectedGroup, settings, fileInfo, createAlign); } } } }
public Vector3 GetLayerPosition(Rect layerSize, Vector2 layerPivot, float pixelsToUnitSize) { return(PsdBuilder.CalculateLayerPosition(layerSize, layerPivot)); }
public Vector3 GetGroupPosition(GameObject groupRoot, SpriteAlignment alignment) { return(PsdBuilder.CalculateGroupPosition(groupRoot, alignment)); }
private void DrawCreateEntry() { showCreateSprites = EditorGUILayout.Foldout(showCreateSprites, "Sprite Creation", styleBoldFoldout); if (!showCreateSprites || !imageLoaded) { return; } float labelWidth = EditorGUIUtility.labelWidth; using (new EditorGUILayout.HorizontalScope(GUILayout.Height(30f))) { MessageType boxType = MessageType.Warning; string boxMsg = "Select a Layer Group"; if (selectedGroup != null) { boxMsg = string.Format("Creating: {0}", selectedGroup.name); boxType = MessageType.Info; } EditorGUILayout.HelpBox(boxMsg, boxType); using (new EditorGUILayout.VerticalScope(GUILayout.Width(165f))) { GUILayout.FlexibleSpace(); EditorGUIUtility.labelWidth = 65f; createAlign = (SpriteAlignment)EditorGUILayout.EnumPopup("Alignment", createAlign); GUILayout.FlexibleSpace(); } } GUILayout.Space(5f); bool createSprites = false; bool createUiImgs = false; bool createAllUI = false; // EditorGUIUtility.labelWidth = labelWidth; // using (new EditorGUILayout.HorizontalScope()) // { // if (selectedGroup == null) // GUI.enabled = false; // // const float buttonHeight = 50f; // // using (new EditorGUILayout.VerticalScope()) // { // GUILayout.Label("2D Sprites", styleHeader); // createSprites = GUILayout.Button("Create 2D Sprites", // GUILayout.Height(buttonHeight), // GUILayout.ExpandHeight(false)); // } // // using (new EditorGUILayout.VerticalScope()) // { // bool selectionOk = Selection.activeGameObject != null; // string btnText = "Select UI Root"; // if (selectionOk) // { // selectionOk = uiConstructor.CanBuild(Selection.activeGameObject); // if (selectionOk) // btnText = "Create UI Images"; // } // // GUI.enabled = selectionOk && selectedGroup != null; // // GUILayout.Label("UI Images", styleHeader); // createUiImgs = GUILayout.Button(btnText, // GUILayout.Height(buttonHeight), // GUILayout.ExpandHeight(false)); // } // GUI.enabled = true; // } using (new EditorGUILayout.VerticalScope()) { string btnText = "Create UI Panel"; createAllUI = GUILayout.Button(btnText, GUILayout.Height(30), GUILayout.ExpandHeight(false)); GUILayout.Space(5); } if (createSprites) { PsdBuilder.BuildPsd(Selection.activeGameObject, selectedGroup, settings, fileInfo, createAlign, spriteConstructor); } if (createUiImgs) { PsdBuilder.BuildPsd(Selection.activeGameObject, selectedGroup, settings, fileInfo, createAlign, uiConstructor); } if (createAllUI) { GameObject gObj = PsdBuilder.FindUIRoot(); GameObject root = PsdBuilder.BuildPsdRoot(gObj, settings, fileInfo, createAlign, uiConstructor); Selection.activeGameObject = root; } // DrawCreateExtensions(); }