private int SortingOrderGenerate(SVGSpriteAssetFile spriteAsset) { if (spriteAsset != null) { SVGSpriteRef spriteRef = spriteAsset.SpriteRef; SVGSpriteData spriteData = spriteAsset.SpriteData; int svgIndex = this.SvgAssetIndexGet(spriteRef.TxtAsset); if (svgIndex >= 0) { SVGSpritesList spritesList; SVGAssetInput svgAsset = this.m_SvgList[svgIndex]; // if needed, advance in the instances group if (spriteData.InCurrentInstancesGroup) { // get the list of sprites (references) relative to the SVG input asset if (this.m_GeneratedSpritesLists.TryGetValue(svgAsset.TxtAsset.GetInstanceID(), out spritesList)) { // advance instances group, telling that we are going to instantiate one sprite only this.NextInstancesGroup(svgAsset, spritesList, 1); } } return(SVGAtlas.SortingOrderCalc(svgIndex, svgAsset.InstanceBaseIdx, spriteData.ZOrder)); } } return(-1); }
private void SortingOrdersCompact(SVGAssetInput svgAsset) { List <SpriteRenderer> spriteRenderers = new List <SpriteRenderer>(); // get the list of instantiated sprites relative to this atlas generator List <GameObject> spritesInstances = new List <GameObject>(); this.GetSpritesInstances(spritesInstances); foreach (GameObject gameObj in spritesInstances) { SVGSpriteLoaderBehaviour spriteLoader = (SVGSpriteLoaderBehaviour)gameObj.GetComponent <SVGSpriteLoaderBehaviour>(); SVGSpriteRef spriteRef = spriteLoader.SpriteReference; // if the sprite belongs to the specified SVG asset input, keep track of it if (spriteRef.TxtAsset == svgAsset.TxtAsset) { SpriteRenderer renderer = (SpriteRenderer)gameObj.GetComponent <SpriteRenderer>(); if (renderer != null) { spriteRenderers.Add(renderer); } } } if (spriteRenderers.Count > 0) { // order the list by current sorting order spriteRenderers.Sort(delegate(SpriteRenderer renderer1, SpriteRenderer renderer2) { if (renderer1.sortingOrder < renderer2.sortingOrder) { return(-1); } if (renderer1.sortingOrder > renderer2.sortingOrder) { return(1); } return(0); }); int j = spriteRenderers.Count; for (int i = 0; i < j; ++i) { SpriteRenderer renderer = spriteRenderers[i]; int currentOrder = renderer.sortingOrder; // isolate high part int svgIndex = currentOrder & SPRITES_SORTING_DOCUMENTS_MASK; // assign the new order renderer.sortingOrder = SVGAtlas.SortingOrderCalc(svgIndex, i); } svgAsset.InstanceBaseIdx = j; } else { // there are no sprite instances relative to the specified SVG, so we can start from 0 svgAsset.InstanceBaseIdx = 0; } }
private void SpritePreview(SVGAtlas atlas, SVGSpriteAssetFile spriteAsset) { Sprite sprite = spriteAsset.SpriteData.Sprite; Texture2D texture = sprite.texture; Rect spriteRect = sprite.textureRect; Rect uv = new Rect(spriteRect.x / texture.width, spriteRect.y / texture.height, spriteRect.width / texture.width, spriteRect.height / texture.height); GUILayoutOption[] spriteTextureOptions = new GUILayoutOption[2] { GUILayout.Width(atlas.SpritesPreviewSize), GUILayout.Height(atlas.SpritesPreviewSize) }; EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(atlas.SpritesPreviewSize + 5)); { EditorGUILayout.LabelField(sprite.name, GUILayout.MinWidth(10)); // reserve space for drawing sprite EditorGUILayout.LabelField("", spriteTextureOptions); Rect guiRect = GUILayoutUtility.GetLastRect(); float maxSpriteDim = Math.Max(spriteRect.width, spriteRect.height); float previewWidth = (spriteRect.width / maxSpriteDim) * atlas.SpritesPreviewSize; float previewHeight = (spriteRect.height / maxSpriteDim) * atlas.SpritesPreviewSize; float previewX = (atlas.SpritesPreviewSize - previewWidth) / 2; //float previewY = (SVGAtlasEditor.SPRITE_PREVIEW_DIMENSION - previewHeight) / 2; //float previewY = (previewWidth > previewHeight) ? 0 : ((SVGAtlasEditor.SPRITE_PREVIEW_DIMENSION - previewHeight) / 2); float previewY = 0; Rect previewRect = new Rect(guiRect.xMin + previewX, guiRect.yMin + previewY, previewWidth, previewHeight); GUI.DrawTextureWithTexCoords(previewRect, texture, uv, true); EditorGUILayout.Space(); // sprite dimensions EditorGUILayout.LabelField("[" + spriteRect.width + " x " + spriteRect.height + "]", GUILayout.MaxWidth(100)); EditorGUILayout.Space(); // current pivot EditorGUILayout.LabelField("Pivot [" + string.Format("{0:0.00}", spriteAsset.SpriteData.Pivot.x) + " , " + string.Format("{0:0.00}", spriteAsset.SpriteData.Pivot.y) + "]", GUILayout.Width(120)); EditorGUILayout.Space(); // edit pivot if (GUILayout.Button("Edit", EditorStyles.miniButton)) { // show pivot editor SVGPivotEditor.Show(spriteAsset, this.OnPivotEdited); } // instantiate if (GUILayout.Button("Instantiate", EditorStyles.miniButton, GUILayout.Width(80))) { GameObject gameObj = atlas.InstantiateSprite(spriteAsset.SpriteRef); // set the created instance as selected if (gameObj != null) { Selection.objects = new UnityEngine.Object[1] { gameObj as UnityEngine.Object }; } } EditorGUILayout.Space(); } EditorGUILayout.EndHorizontal(); }
// pivot editing callback private void OnPivotEdited(PivotEditingResult result, SVGSpriteAssetFile spriteAsset, Vector2 editedPivot, Vector4 editedBorder) { SVGAtlas atlas = target as SVGAtlas; if ((atlas != null) && (result == PivotEditingResult.Ok)) { // assign the new pivot atlas.UpdatePivot(spriteAsset, editedPivot); SVGUtils.MarkObjectDirty(atlas); } }
private void OnAtlasSelect(SVGAtlas atlas) { if (this.m_EditedLoader.Atlas != atlas) { this.m_EditedLoader.Atlas = atlas; this.m_EditedLoader.SpriteReference = null; SpriteRenderer renderer = this.m_EditedLoader.GetComponent <SpriteRenderer>(); if (renderer != null) { renderer.sprite = null; } } }
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); }
// Draw an atlas preview private static void AtlasPreview(SVGAtlas atlas, Rect clipRect, int textureIndex) { int idx = textureIndex % atlas.TextureAssetsCount(); AssetFile textureAsset = atlas.TextureAsset(idx); Texture2D atlasTexture = textureAsset.Object as Texture2D; if (atlasTexture != null) { Rect uv = new Rect(0, 0, 1, 1); float maxAtlasDim = Math.Max(atlasTexture.width, atlasTexture.height); float previewWidth = (atlasTexture.width / maxAtlasDim) * SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION; float previewHeight = (atlasTexture.height / maxAtlasDim) * SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION; float previewX = (SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION - previewWidth) / 2; float previewY = (SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION - previewHeight) / 2; Rect previewRect = new Rect(clipRect.xMin + previewX, clipRect.yMin + previewY, previewWidth, previewHeight); GUI.DrawTextureWithTexCoords(previewRect, atlasTexture, uv, true); } }
private static void UpdatePivotHierarchy(GameObject gameObj, Vector2 delta, uint depthLevel) { SVGSpriteLoaderBehaviour loader = gameObj.GetComponent <SVGSpriteLoaderBehaviour>(); if (loader != null) { Vector2 realDelta = (depthLevel > 0) ? (new Vector2(-delta.x, -delta.y)) : (new Vector2(delta.x * gameObj.transform.localScale.x, delta.y * gameObj.transform.localScale.y)); Vector2 newPos = new Vector2(gameObj.transform.localPosition.x + realDelta.x, gameObj.transform.localPosition.y + realDelta.y); // modify the current node gameObj.transform.localPosition = newPos; } // traverse children int j = gameObj.transform.childCount; for (int i = 0; i < j; ++i) { GameObject child = gameObj.transform.GetChild(i).gameObject; SVGAtlas.UpdatePivotHierarchy(child, delta, depthLevel + 1); } }
public override void OnInspectorGUI() { if (SVGAtlasEditor.m_Styles == null) { SVGAtlasEditor.m_Styles = new Styles(); } // get the target object SVGAtlas atlas = target as SVGAtlas; if (atlas != null) { base.OnInspectorGUI(); // we assign the Name property, so we can check if the user has renamed the atlas asset file atlas.Name = atlas.name; bool isDirty = this.DrawInspector(atlas); if (isDirty) { SVGUtils.MarkObjectDirty(atlas); SVGUtils.MarkSceneDirty(); } } }
// recalculate sorting orders of instantiated sprites: changing is due only to SVG index, so the lower part (group + zNatural) is left unchanged private void SortingOrdersUpdateSvgIndex() { // get the list of instantiated sprites relative to this atlas generator List <GameObject> spritesInstances = new List <GameObject>(); this.GetSpritesInstances(spritesInstances); foreach (GameObject gameObj in spritesInstances) { SVGSpriteLoaderBehaviour spriteLoader = (SVGSpriteLoaderBehaviour)gameObj.GetComponent <SVGSpriteLoaderBehaviour>(); SpriteRenderer renderer = (SpriteRenderer)gameObj.GetComponent <SpriteRenderer>(); if (renderer != null) { SVGSpriteRef spriteRef = spriteLoader.SpriteReference; int svgIndex = this.SvgAssetIndexGet(spriteRef.TxtAsset); if (svgIndex >= 0) { int instance = renderer.sortingOrder & SPRITES_SORTING_INSTANCES_MASK; renderer.sortingOrder = SVGAtlas.SortingOrderCalc(svgIndex, instance); } } } }
public void UpdatePivot(SVGSpriteAssetFile spriteAsset, Vector2 newPivot) { SVGSpriteRef spriteRef = spriteAsset.SpriteRef; SVGSpriteData spriteData = spriteAsset.SpriteData; Sprite oldSprite = spriteData.Sprite; // keep track of pivot movement Vector2 deltaPivot = newPivot - spriteData.Pivot; Vector2 deltaMovement = (new Vector2(deltaPivot.x * oldSprite.rect.width, deltaPivot.y * oldSprite.rect.height)) / SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT; // create a new sprite (same texture, same rectangle, different pivot) Sprite newSprite = Sprite.Create(oldSprite.texture, oldSprite.rect, newPivot, SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT, 0, SpriteMeshType.FullRect, spriteData.Border); GameObject[] allObjects = GameObject.FindObjectsOfType <GameObject>(); foreach (GameObject gameObj in allObjects) { if (gameObj.activeInHierarchy) { SVGSpriteLoaderBehaviour loader = gameObj.GetComponent <SVGSpriteLoaderBehaviour>(); // we must be sure that the loader component must refer to this atlas if (loader != null && loader.Atlas == this) { // check if the instance uses the specified sprite if (loader.SpriteReference.TxtAsset == spriteRef.TxtAsset && loader.SpriteReference.ElemIdx == spriteRef.ElemIdx) { SVGAtlas.UpdatePivotHierarchy(gameObj, deltaMovement, 0); } } } } spriteData.Pivot = newPivot; newSprite.name = oldSprite.name; EditorUtility.CopySerialized(newSprite, oldSprite); SVGUtils.MarkObjectDirty(oldSprite); // destroy the temporary sprite GameObject.DestroyImmediate(newSprite); }
static private List <SVGAtlas> GetAtlasesList() { string[] guids = AssetDatabase.FindAssets("t:SVGAtlas"); List <SVGAtlas> atlasesList = new List <SVGAtlas>(); foreach (string assetGUID in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(assetGUID); UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(SVGAtlas)); SVGAtlas atlas = asset as SVGAtlas; if (atlas != null) { atlasesList.Add(atlas); } } // sort atlases by name atlasesList.Sort(delegate(SVGAtlas atlas1, SVGAtlas atlas2) { return(string.Compare(atlas1.name, atlas2.name, StringComparison.OrdinalIgnoreCase)); }); return(atlasesList); }
private bool DrawInspector(SVGAtlas atlas) { Rect scollRect; float match = atlas.Match; bool isDirty = false; // get current event Event currentEvent = Event.current; // show current options int refWidth = EditorGUILayout.IntField(new GUIContent("Reference width", "The resolution the SVG files content is designed for. If the screen resolution is larger, SVG contents will be scaled up, and if it’s smaller, it will be scaled down."), atlas.ReferenceWidth); int refHeight = EditorGUILayout.IntField(new GUIContent("Reference height", "The resolution the SVG files content is designed for. If the screen resolution is larger, SVG contents will be scaled up, and if it’s smaller, it will be scaled down."), atlas.ReferenceHeight); int deviceTestWidth = EditorGUILayout.IntField("Device test width", atlas.DeviceTestWidth); int deviceTestHeight = EditorGUILayout.IntField("Device test height", atlas.DeviceTestHeight); SVGScalerMatchMode scaleType = (SVGScalerMatchMode)EditorGUILayout.EnumPopup(new GUIContent("Screen match mode", "A mode used to scale (i.e. generate) SVG sprites if the aspect ratio of the current resolution doesn’t fit the reference resolution."), atlas.ScaleType); if (scaleType == SVGScalerMatchMode.MatchWidthOrHeight) { Rect r = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight + 12); match = this.MatchSlider(r, atlas.Match); } float offsetScale = EditorGUILayout.FloatField(this.m_OffsetScaleContent, atlas.OffsetScale); bool pow2Textures = EditorGUILayout.Toggle(this.m_Pow2TexturesContent, atlas.Pow2Textures); int maxTexturesDimension = EditorGUILayout.IntField(this.m_MaxTexturesDimensionContent, atlas.MaxTexturesDimension); int border = EditorGUILayout.IntField(this.m_SpritesPaddingContent, atlas.SpritesBorder); Color clearColor = EditorGUILayout.ColorField(this.m_ClearColorContent, atlas.ClearColor); bool fastUpload = EditorGUILayout.Toggle(this.m_FastUploadContent, atlas.FastUpload); float spritesPreviewSize = (float)EditorGUILayout.IntField(this.m_SpritesPreviewSizeContent, (int)atlas.SpritesPreviewSize); // output folder this.OutputFolderDraw(atlas); // draw the list of input SVG files / assets isDirty |= this.SvgInputAssetsDraw(atlas, currentEvent, out scollRect); // update button if (this.UpdateButtonDraw(atlas)) { // regenerate/update sprites atlas.UpdateEditorSprites(true); isDirty = true; } GUILayout.Space(10); if (atlas.SvgAssetsCount() > 0) { // list of sprites, grouped by SVG document Vector2 spritesScrollPos = EditorGUILayout.BeginScrollView(this.m_SvgSpritesScrollPos, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); bool separatorNeeded = false; for (int i = 0; i < atlas.SvgAssetsCount(); ++i) { SVGAssetInput svgAsset = atlas.SvgAsset(i); List <SVGSpriteAssetFile> spritesAssets = atlas.GetGeneratedSpritesByDocument(svgAsset.TxtAsset); if (spritesAssets != null && spritesAssets.Count > 0) { // line separator if (separatorNeeded) { EditorGUILayout.Separator(); GUILayout.Box(GUIContent.none, this.m_GreyLine, GUILayout.ExpandWidth(true), GUILayout.Height(1)); EditorGUILayout.Separator(); } // display sprites list foreach (SVGSpriteAssetFile spriteAsset in spritesAssets) { this.SpritePreview(atlas, spriteAsset); } // we have displayed some sprites, next time a line separator is needed separatorNeeded = true; } } EditorGUILayout.EndScrollView(); if (this.m_SvgSpritesScrollPos != spritesScrollPos) { this.m_SvgSpritesScrollPos = spritesScrollPos; } } // events handler isDirty |= this.HandleDragEvents(atlas, currentEvent, scollRect); // negative values are not allowed for reference resolution refWidth = (refWidth <= 0) ? Screen.currentResolution.width : refWidth; refHeight = (refHeight <= 0) ? Screen.currentResolution.height : refHeight; deviceTestWidth = (deviceTestWidth <= 0) ? refWidth : deviceTestWidth; deviceTestHeight = (deviceTestHeight <= 0) ? refHeight : deviceTestHeight; // a negative value is not allowed for texture max dimension maxTexturesDimension = (maxTexturesDimension < 0) ? 1024 : maxTexturesDimension; // a negative value is not allowed for border border = (border < 0) ? 0 : border; // if reference resolution has been changed, update it if (atlas.ReferenceWidth != refWidth) { atlas.ReferenceWidth = refWidth; isDirty = true; } if (atlas.ReferenceHeight != refHeight) { atlas.ReferenceHeight = refHeight; isDirty = true; } // if device (test) resolution has been changed, update it if (atlas.DeviceTestWidth != deviceTestWidth) { atlas.DeviceTestWidth = deviceTestWidth; isDirty = true; } if (atlas.DeviceTestHeight != deviceTestHeight) { atlas.DeviceTestHeight = deviceTestHeight; isDirty = true; } // if scale adaption method has been changed, update it if (atlas.ScaleType != scaleType) { atlas.ScaleType = scaleType; isDirty = true; } if (atlas.Match != match) { atlas.Match = match; isDirty = true; } // if offset additional scale has been changed, update it if (atlas.OffsetScale != offsetScale) { atlas.OffsetScale = Math.Abs(offsetScale); isDirty = true; } // if power-of-two forcing flag has been changed, update it if (atlas.Pow2Textures != pow2Textures) { atlas.Pow2Textures = pow2Textures; isDirty = true; } // if desired maximum texture dimension has been changed, update it if (atlas.MaxTexturesDimension != maxTexturesDimension) { atlas.MaxTexturesDimension = maxTexturesDimension; isDirty = true; } // if border between each packed SVG has been changed, update it if (atlas.SpritesBorder != border) { atlas.SpritesBorder = border; isDirty = true; } // if surface clear color has been changed, update it if (atlas.ClearColor != clearColor) { atlas.ClearColor = clearColor; isDirty = true; } // if "fast upload" flag has been changed, update it if (atlas.FastUpload != fastUpload) { atlas.FastUpload = fastUpload; isDirty = true; } // if sprites preview size has been changed, update it if (atlas.SpritesPreviewSize != spritesPreviewSize) { atlas.SpritesPreviewSize = spritesPreviewSize; isDirty = true; } return(isDirty); }
private void OnAtlasSelect(SVGAtlas atlas) { if (this.m_EditedLoader.Atlas != atlas) { this.m_EditedLoader.Atlas = atlas; this.m_EditedLoader.SpriteReference = null; SpriteRenderer renderer = this.m_EditedLoader.GetComponent<SpriteRenderer>(); if (renderer != null) renderer.sprite = null; } }
private bool DrawGUI() { bool close = false; #if UNITY_EDITOR_WIN // take care of dpi scaling factor on Windows (Display Settings --> Advanced scaling settings) float dpi = Screen.dpi; // dpi 96 == 1.00 // dpi 120 == 1.25 // dpi 144 == 1.50 // dpi 168 == 1.75 // ... and so on float dpiAdjust = (((dpi - 96.0f) / 24.0f) * 0.25f) + 1.0f; #else float dpiAdjust = 1.0f; #endif int columnsPerRow = Math.Max(Mathf.FloorToInt((Screen.width / dpiAdjust) / SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION_PADDED), 1); int rowsCount = 1; int atlasIdx = 0; Rect rect = new Rect(SVGAtlasSelector.ATLAS_PREVIEW_BORDER, SVGAtlasSelector.ATLAS_PREVIEW_BORDER, SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION, SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION); // draw header, with the name of atlas and the "search by name" toolbox List <SVGAtlas> atlasesList = this.Header(); this.m_ScrollPos = GUILayout.BeginScrollView(this.m_ScrollPos); while (atlasIdx < atlasesList.Count) { // start a new row GUILayout.BeginHorizontal(); { int currentColumn = 0; rect.x = SVGAtlasSelector.ATLAS_PREVIEW_BORDER; while (atlasIdx < atlasesList.Count) { SVGAtlas atlas = atlasesList[atlasIdx]; // buttons are used to implement atlas selection (we use the atlas name as tooltip) if (GUI.Button(rect, new GUIContent("", atlas.name))) { // mouse left button click if (Event.current.button == 0) { if (this.m_Callback != null) { this.m_Callback(atlas); } close = true; } } // draw atlas preview if (Event.current.type == EventType.Repaint) { SVGAtlasSelector.AtlasPreview(atlas, rect, this.m_TextureIndex); } // draw atlas name SVGAtlasSelector.AtlasLabel(atlas.name, rect); // next atlas atlasIdx++; // next column rect.x += SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION_PADDED; if (++currentColumn >= columnsPerRow) { break; } } } GUILayout.EndHorizontal(); GUILayout.Space(SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION_PADDED); rect.y += SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION_PADDED + SVGAtlasSelector.ATLAS_NAME_HEIGHT; rowsCount++; } GUILayout.Space((rowsCount - 1) * SVGAtlasSelector.ATLAS_NAME_HEIGHT + SVGAtlasSelector.ATLAS_PREVIEW_BORDER); GUILayout.EndScrollView(); return(close); }
// Reset is called when the user hits the Reset button in the Inspector's context menu or when adding the component the first time. // This function is only called in editor mode. Reset is most commonly used to give good default values in the inspector. void Reset() { this.Camera = null; this.Background = null; this.Atlas = null; this.Cards = null; this.m_SelectedCard0 = null; this.m_SelectedCard1 = null; this.m_Animating = false; }
// Draw an atlas preview private static void AtlasPreview(SVGAtlas atlas, Rect clipRect, int textureIndex) { List<AssetFile> texturesAssets = atlas.GeneratedTexturesFiles; int idx = textureIndex % texturesAssets.Count; Texture2D atlasTexture = texturesAssets[idx].Object as Texture2D; if (atlasTexture != null) { Rect uv = new Rect(0, 0, 1, 1); float maxAtlasDim = Math.Max(atlasTexture.width, atlasTexture.height); float previewWidth = (atlasTexture.width / maxAtlasDim) * SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION; float previewHeight = (atlasTexture.height / maxAtlasDim) * SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION; float previewX = (SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION - previewWidth) / 2; float previewY = (SVGAtlasSelector.ATLAS_PREVIEW_DIMENSION - previewHeight) / 2; Rect previewRect = new Rect(clipRect.xMin + previewX, clipRect.yMin + previewY, previewWidth, previewHeight); GUI.DrawTextureWithTexCoords(previewRect, atlasTexture, uv, true); } }
// show the sprite selector public static void Show(SVGAtlas 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); //selector.m_SelectedSprite = null; } }
private static int SortingOrderCalc(int svgIndex, int instanceBaseIdx, int zOrder) { return(SVGAtlas.SortingOrderCalc(svgIndex, instanceBaseIdx + zOrder)); }
public GameObject[] InstantiateGroups(SVGAssetInput svgAsset) { SVGSpritesList spritesList; if (svgAsset != null && this.m_GeneratedSpritesLists.TryGetValue(svgAsset.TxtAsset.GetInstanceID(), out spritesList)) { int spritesCount = spritesList.Sprites.Count; int svgIndex = this.SvgAssetIndexGet(svgAsset.TxtAsset); if (svgIndex >= 0 && spritesCount > 0) { // list of sprite assets (file) relative to the specified SVG; in this case we can set the right list capacity List <SVGSpriteAssetFile> spriteAssets = new List <SVGSpriteAssetFile>(spritesCount); bool advanceInstancesGroup = false; // now we are sure that at least one valid sprite box exists float xMin = float.MaxValue; float yMin = float.MaxValue; float xMax = float.MinValue; float yMax = float.MinValue; foreach (SVGSpriteRef spriteRef in spritesList.Sprites) { SVGSpriteAssetFile spriteAsset; if (this.m_GeneratedSpritesFiles.TryGetValue(spriteRef, out spriteAsset)) { SVGSpriteData spriteData = spriteAsset.SpriteData; Sprite sprite = spriteData.Sprite; //float scl = 1 / spriteData.Scale; float scl = 1; float ox = (float)spriteData.OriginalX; float oy = (float)spriteData.OriginalY; float spriteMinX = ox * scl; float spriteMinY = oy * scl; float spriteMaxX = (ox + sprite.rect.width) * scl; float spriteMaxY = (oy + sprite.rect.height) * scl; // update min corner if (spriteMinX < xMin) { xMin = spriteMinX; } if (spriteMinY < yMin) { yMin = spriteMinY; } // update max corner if (spriteMaxX > xMax) { xMax = spriteMaxX; } if (spriteMaxY > yMax) { yMax = spriteMaxY; } // if there is a single sprite already instantiated in the current group, we have to advance in the next instances group if (spriteData.InCurrentInstancesGroup) { advanceInstancesGroup = true; } // keep track of this sprite asset spriteAssets.Add(spriteAsset); } } if (spriteAssets.Count > 0) { // because at least one valid sprite box exists, now we are sure that a valid "global" box has been calculated float centerX = (xMin + xMax) / 2; float centerY = (yMin + yMax) / 2; float boxHeight = yMax - yMin; List <GameObject> instances = new List <GameObject>(); if (advanceInstancesGroup) { // advance in the instances group, telling that we are going to instantiate N sprites this.NextInstancesGroup(svgAsset, spritesList, spriteAssets.Count); } foreach (SVGSpriteAssetFile spriteAsset in spriteAssets) { SVGSpriteData spriteData = spriteAsset.SpriteData; Sprite sprite = spriteData.Sprite; Vector2 pivot = spriteData.Pivot; //float scl = 1 / spriteData.Scale; float scl = 1; float px = (sprite.rect.width * pivot.x + (float)spriteData.OriginalX) * scl - centerX; float py = boxHeight - (sprite.rect.height * (1 - pivot.y) + (float)spriteData.OriginalY) * scl - centerY; Vector2 worldPos = new Vector2(px / SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT, py / SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT); // instantiate the object int sortingOrder = SVGAtlas.SortingOrderCalc(svgIndex, svgAsset.InstanceBaseIdx, spriteData.ZOrder); //instances.Add(this.InstantiateSprite(spriteAsset, worldPos, sortingOrder)); GameObject newObj = this.InstantiateSprite(spriteAsset, worldPos, sortingOrder); newObj.transform.localScale = new Vector3(scl, scl, 1); spriteData.InCurrentInstancesGroup = true; } // return the created instances return(instances.ToArray()); } } } return(null); }
private static List<SVGSpriteAssetFile> GetSpritesList(SVGAtlas atlas) { List<SVGSpriteAssetFile> spritesList = atlas.GeneratedSpritesFiles.Values(); spritesList.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 spritesList; }