protected override bool SvgInputAssetDrawImplementation(SVGBasicAtlas basicAtlas, SVGAssetInput svgAsset, int svgAssetIndex) { SVGUIAtlas uiAtlas = basicAtlas as SVGUIAtlas; bool isDirty = false; // scale adjustment for this SVG EditorGUILayout.LabelField(new GUIContent("Scale adjustment", "An additional scale factor used to adjust this SVG content only"), GUILayout.Width(105)); float offsetScale = EditorGUILayout.FloatField(svgAsset.Scale, GUILayout.Width(45)); EditorGUILayout.LabelField("", GUILayout.Width(5)); if (offsetScale != svgAsset.Scale) { uiAtlas.SvgAssetScaleAdjustmentSet(svgAsset, Math.Abs(offsetScale)); isDirty = true; } // 'explode groups' flag bool separateGroups = EditorGUILayout.Toggle("", svgAsset.SeparateGroups, GUILayout.Width(14)); EditorGUILayout.LabelField("Separate groups", GUILayout.Width(105)); // if group explosion flag has been changed, update it if (separateGroups != svgAsset.SeparateGroups) { uiAtlas.SvgAssetSeparateGroupsSet(svgAsset, separateGroups); isDirty = true; } // if 'Remove' button is clicked, remove the SVG entry if (GUILayout.Button("Remove", EditorStyles.miniButton, GUILayout.Width(70))) { uiAtlas.SvgAssetRemove(svgAssetIndex); isDirty = true; } return(isDirty); }
// return true if the atlas needs an update (i.e. a call to UpdateSprites), else false protected override string CalcAtlasHash() { int count = this.m_SvgList.Count; if (count > 0) { // we want the parameters string to come always in front (when sorted) string[] hashList = new string[count + 1]; // parameters string string paramsStr = "#*"; paramsStr += this.m_ReferenceWidth + "-"; paramsStr += this.m_ReferenceHeight + "-"; paramsStr += this.m_DeviceTestWidth + "-"; paramsStr += this.m_DeviceTestHeight + "-"; paramsStr += this.m_ScaleType + "-"; paramsStr += this.m_Match + "-"; paramsStr += this.m_OffsetScale + "-"; paramsStr += this.m_Pow2Textures + "-"; paramsStr += this.m_MaxTexturesDimension + "-"; paramsStr += this.m_SpritesBorder + "-"; paramsStr += this.m_ClearColor.ToString() + "-"; paramsStr += this.FullOutputFolder; hashList[0] = paramsStr; // for each input SVG row we define an "id string" for (int i = 0; i < count; ++i) { hashList [i + 1] = this.m_SvgList [i].Hash(); } // sort strings, so we can be SVG rows order independent Array.Sort(hashList); // return MD5 hash return(SVGBasicAtlas.MD5Calc(String.Join("-", hashList))); } return(""); }
protected bool SvgInputAssetsDraw(SVGBasicAtlas atlas, Event currentEvent, out Rect scollRect) { bool isDirty = false; // keep track of drawn rows if (currentEvent.type != EventType.Layout) { this.m_InputAssetsRects = new List <Rect>(); } Vector2 scrollPos = EditorGUILayout.BeginScrollView(this.m_SvgListScrollPos, GUILayout.ExpandWidth(true), GUILayout.MaxHeight(102), GUILayout.Height(102)); // perform backward loop because we could even remove entries without issues for (int i = 0; i < atlas.SvgAssetsCount(); ++i) { Rect rowRect; isDirty |= this.SvgInputAssetDraw(atlas, i, out rowRect); // keep track of row rectangle if (currentEvent.type != EventType.Layout) { this.m_InputAssetsRects.Add(rowRect); } } EditorGUILayout.EndScrollView(); // keep track of the scrollview area scollRect = GUILayoutUtility.GetLastRect(); if (this.m_SvgListScrollPos != scrollPos) { this.m_SvgListScrollPos = scrollPos; } return(isDirty); }
private static void ProcessAtlas(SVGBasicAtlas atlas) { Texture2D tmpTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false); List <SVGSpriteAssetFile> spritesAssets = atlas.SpriteAssets(); foreach (SVGSpriteAssetFile spriteAsset in spritesAssets) { SVGSpriteData spriteData = spriteAsset.SpriteData; Sprite original = spriteData.Sprite; // we must reference the original texture, because we want to keep the file reference (rd->texture.IsValid()) Sprite tmpSprite = Sprite.Create(original.texture, new Rect(0, 0, 1, 1), new Vector2(0, 0), SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT, 0, SpriteMeshType.FullRect); // now we change the (sprite) asset content: actually we have just reduced its rectangle to a 1x1 pixel EditorUtility.CopySerialized(tmpSprite, original); } for (int i = 0; i < atlas.TextureAssetsCount(); i++) { AssetFile file = atlas.TextureAsset(i); Texture2D original = file.Object as Texture2D; // copy the 1x1 texture inside the original texture EditorUtility.CopySerialized(tmpTexture, original); } }
static private List <SVGSpriteAssetFile> GetSpritesList(SVGBasicAtlas atlas) { List <SVGSpriteAssetFile> spritesAssets = atlas.SpriteAssets(); spritesAssets.Sort(delegate(SVGSpriteAssetFile spriteAsset1, SVGSpriteAssetFile spriteAsset2) { Sprite sprite1 = spriteAsset1.SpriteData.Sprite; Sprite sprite2 = spriteAsset2.SpriteData.Sprite; return(string.Compare(sprite1.name, sprite2.name, StringComparison.OrdinalIgnoreCase)); }); return(spritesAssets); }
// show the sprite selector static public void Show(SVGBasicAtlas atlas, string spriteName, OnSpriteSelectionCallback callback) { // close the current selector instance, if any SVGSpriteSelector.CloseAll(); if (atlas != null) { SVGSpriteSelector selector = ScriptableWizard.DisplayWizard <SVGSpriteSelector>("Select a sprite"); selector.m_Atlas = atlas; selector.m_SearchString = spriteName; selector.m_Callback = callback; selector.m_ScrollPos = Vector2.zero; selector.m_SpritesList = SVGSpriteSelector.GetSpritesList(atlas); } }
protected override bool SvgInputAssetDrawImplementation(SVGBasicAtlas basicAtlas, SVGAssetInput svgAsset, int svgAssetIndex) { SVGAtlas atlas = basicAtlas as SVGAtlas; bool isDirty = false; // scale adjustment for this SVG EditorGUILayout.LabelField(new GUIContent("Scale adjustment", "An additional scale factor used to adjust this SVG content only"), GUILayout.Width(105)); float offsetScale = EditorGUILayout.FloatField(svgAsset.Scale, GUILayout.Width(45)); EditorGUILayout.LabelField("", GUILayout.Width(5)); if (offsetScale != svgAsset.Scale) { atlas.SvgAssetScaleAdjustmentSet(svgAsset, Math.Abs(offsetScale)); isDirty = true; } // 'explode groups' flag bool separateGroups = EditorGUILayout.Toggle("", svgAsset.SeparateGroups, GUILayout.Width(14)); EditorGUILayout.LabelField("Separate groups", GUILayout.Width(105)); // if group explosion flag has been changed, update it if (separateGroups != svgAsset.SeparateGroups) { atlas.SvgAssetSeparateGroupsSet(svgAsset, separateGroups); isDirty = true; } // if 'Remove' button is clicked, remove the SVG entry if (GUILayout.Button("Remove", EditorStyles.miniButton, GUILayout.Width(70))) { atlas.SvgAssetRemove(svgAssetIndex); isDirty = true; } // instantiate all groups GUI.enabled = ((svgAsset.Instantiable && (!Application.isPlaying)) ? true : false); if (GUILayout.Button("Instantiate", EditorStyles.miniButton, GUILayout.Width(80))) { GameObject[] gameObjs = atlas.InstantiateGroups(svgAsset); // set the created instances as selected if (gameObjs != null) { Selection.objects = gameObjs; } } GUI.enabled = !Application.isPlaying; return(isDirty); }
protected bool UpdateButtonDraw(SVGBasicAtlas atlas) { bool pushed = false; // update button string updateStr = (atlas.NeedsUpdate()) ? "Update *" : "Update"; if (GUILayout.Button(updateStr)) { // close all modal popup editors SVGPivotEditor.CloseAll(); SVGSpriteSelector.CloseAll(); SVGAtlasSelector.CloseAll(); pushed = true; } return(pushed); }
private bool SvgInputAssetDraw(SVGBasicAtlas atlas, int index, out Rect rowRect) { bool isDirty; SVGAssetInput svgAsset = atlas.SvgAsset(index); bool highlight = (this.m_DragInfo.Dragging && this.m_DragInfo.DraggedObject == svgAsset) ? true : false; if (this.m_DragInfo.InsertIdx == index && this.m_DragInfo.InsertBefore) { // draw a separator before the row GUILayout.Box(GUIContent.none, this.m_BlueLine, GUILayout.ExpandWidth(true), GUILayout.Height(2)); } // if the SVG row is the dragged one, change colors if (highlight) { EditorGUILayout.BeginHorizontal(this.m_HighlightRow); // a row: asset name, separate groups checkbox, remove button, instantiate button EditorGUILayout.LabelField(svgAsset.TxtAsset.name, this.m_HighlightRow, GUILayout.MinWidth(10)); } else { EditorGUILayout.BeginHorizontal(); // a row: asset name, separate groups checkbox, remove button, instantiate button EditorGUILayout.LabelField(svgAsset.TxtAsset.name, GUILayout.MinWidth(10)); } isDirty = this.SvgInputAssetDrawImplementation(atlas, svgAsset, index); EditorGUILayout.EndHorizontal(); rowRect = GUILayoutUtility.GetLastRect(); if (this.m_DragInfo.InsertIdx == index && (!this.m_DragInfo.InsertBefore)) { // draw a separator after the row GUILayout.Box(GUIContent.none, this.m_BlueLine, GUILayout.ExpandWidth(true), GUILayout.Height(2)); } return(isDirty); }
protected void OutputFolderDraw(SVGBasicAtlas atlas) { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Output folder"); if (GUILayout.Button(atlas.OutputFolder)) { string absFolderPath = EditorUtility.OpenFolderPanel("Select output folder", atlas.OutputFolder, ""); if ((absFolderPath != "") && (absFolderPath.StartsWith(Application.dataPath))) { string relFolderPath = "Assets" + absFolderPath.Substring(Application.dataPath.Length); if (AssetDatabase.IsValidFolder(relFolderPath)) { atlas.OutputFolder = relFolderPath; } } } } EditorGUILayout.EndHorizontal(); // textures output subfolder EditorGUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Textures subfolder"); EditorGUILayout.LabelField(atlas.TexturesSubFolder); } EditorGUILayout.EndHorizontal(); // sprites output subfolder EditorGUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Sprites subfolder"); EditorGUILayout.LabelField(atlas.SpritesSubFolder); } EditorGUILayout.EndHorizontal(); GUILayout.Space(18); EditorGUILayout.LabelField("Drag & drop SVG assets here"); }
protected bool HandleDragEvents(SVGBasicAtlas atlas, Event currentEvent, Rect scollRect) { int i; bool isDirty = false; // events handler if (currentEvent.type != EventType.Layout) { bool needRepaint = false; // get mouse position relative to scollRect Vector2 mousePos = currentEvent.mousePosition - new Vector2(scollRect.xMin, scollRect.yMin); if (scollRect.Contains(currentEvent.mousePosition)) { bool separatorInserted = false; for (i = 0; i < atlas.SvgAssetsCount(); ++i) { // get the row rectangle relative to atlas.SvgList[i] Rect rowRect = this.m_InputAssetsRects[i]; // expand the rectangle height rowRect.yMin -= 3; rowRect.yMax += 3; if (rowRect.Contains(mousePos)) { // a mousedown on a row, will stop an already started drag operation if (currentEvent.type == EventType.MouseDown) { this.m_DragInfo.StopDrag(); } // check if we are already dragging an object if (this.m_DragInfo.Dragging) { if (!separatorInserted) { bool ok = true; bool dragBefore = (mousePos.y <= rowRect.yMin + rowRect.height / 2) ? true : false; // if we are dragging a text (asset) file, all positions are ok // if we are dragging an already present SVG row, we must perform additional checks if (!(this.m_DragInfo.DraggedObject is TextAsset)) { if (this.m_DragInfo.DraggedObject == atlas.SvgAsset(i)) { ok = false; } else { if (dragBefore) { if (i > 0 && this.m_DragInfo.DraggedObject == atlas.SvgAsset(i - 1)) { ok = false; } } else { if (i < (atlas.SvgAssetsCount() - 1) && this.m_DragInfo.DraggedObject == atlas.SvgAsset(i + 1)) { ok = false; } } } } if (ok) { if (dragBefore) { this.m_DragInfo.InsertIdx = i; this.m_DragInfo.InsertBefore = true; separatorInserted = true; } else { this.m_DragInfo.InsertIdx = i; this.m_DragInfo.InsertBefore = false; separatorInserted = true; } needRepaint = true; } } } else { // initialize the drag of an already present SVG document if (currentEvent.type == EventType.MouseDrag) { DragAndDrop.PrepareStartDrag(); DragAndDrop.StartDrag("Start drag"); this.m_DragInfo.StartDrag(atlas.SvgAsset(i)); needRepaint = true; } } } } // mouse is dragging inside the drop box, but not under an already present row; insertion point is inside the last element if (this.m_DragInfo.Dragging && !separatorInserted && atlas.SvgAssetsCount() > 0 && mousePos.y > this.m_InputAssetsRects[atlas.SvgAssetsCount() - 1].yMax) { bool ok = true; if (!(this.m_DragInfo.DraggedObject is TextAsset)) { if (this.m_DragInfo.DraggedObject == atlas.SvgAsset(atlas.SvgAssetsCount() - 1)) { ok = false; } } if (ok) { this.m_DragInfo.InsertIdx = atlas.SvgAssetsCount() - 1; this.m_DragInfo.InsertBefore = false; needRepaint = true; } } } else { this.m_DragInfo.InsertIdx = -1; } if (needRepaint) { Repaint(); } } if (currentEvent.type == EventType.DragExited) { this.m_DragInfo.StopDrag(); DragAndDrop.objectReferences = new UnityEngine.Object[0]; } else { switch (currentEvent.type) { case EventType.DragUpdated: case EventType.DragPerform: if (this.m_DragInfo.Dragging) { bool dragValid = true; if (scollRect.Contains(currentEvent.mousePosition) && dragValid) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; if (currentEvent.type == EventType.DragPerform) { int index; // accept drag&drop operation DragAndDrop.AcceptDrag(); // check if we are dropping a text asset if (this.m_DragInfo.DraggedObject is TextAsset) { // if a valid inter-position has not been selected, append the new asset at the end of list if (this.m_DragInfo.InsertIdx < 0) { index = atlas.SvgAssetsCount(); } else { index = (this.m_DragInfo.InsertBefore) ? this.m_DragInfo.InsertIdx : (this.m_DragInfo.InsertIdx + 1); } // add the text asset to the SVG list if (atlas.SvgAssetAdd(this.m_DragInfo.DraggedObject as TextAsset, index)) { isDirty = true; } } else { // we are dropping an already present SVG row index = (this.m_DragInfo.InsertBefore) ? this.m_DragInfo.InsertIdx : (this.m_DragInfo.InsertIdx + 1); if (atlas.SvgAssetMove(this.m_DragInfo.DraggedObject as SVGAssetInput, index)) { isDirty = true; } } // now we can close the drag operation this.m_DragInfo.StopDrag(); } } else { // if we are dragging outside of the allowed drop region, simply reject the drag&drop DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } } else { if (scollRect.Contains(currentEvent.mousePosition)) { if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length > 0) { UnityEngine.Object draggedObject = DragAndDrop.objectReferences[0]; // check object type, only TextAssets are allowed if (draggedObject is TextAsset) { this.m_DragInfo.StartDrag(DragAndDrop.objectReferences[0]); Repaint(); } else { // acceptance is not confirmed (e.g. we are dragging a binary file) DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } } } } break; default: break; } } return(isDirty); }
protected abstract bool SvgInputAssetDrawImplementation(SVGBasicAtlas atlas, SVGAssetInput svgAsset, int svgAssetIndex);