// border editing callback private void OnSpriteEdited(PivotEditingResult result, SVGSpriteAssetFile spriteAsset, Vector2 editedPivot, Vector4 editedBorder) { SVGCanvasBehaviour svgCanvas = target as SVGCanvasBehaviour; if ((svgCanvas != null) && (result == PivotEditingResult.Ok)) { SVGUIAtlas uiAtlas = svgCanvas.UIAtlas; if (uiAtlas != null) { // assign the new border uiAtlas.UpdateBorder(spriteAsset, editedBorder); SVGUtils.MarkObjectDirty(uiAtlas); SVGUtils.MarkSceneDirty(); // get the list of instantiated SVG sprites that reference the edited one List <GameObject> spritesInstances = new List <GameObject>(); uiAtlas.GetSpritesInstances(spritesInstances, spriteAsset.SpriteRef); foreach (GameObject gameObj in spritesInstances) { Image uiImage = (Image)gameObj.GetComponent <Image>(); if (uiImage != null) { // the Image component won't recognize the change of sprite border, so in order to refresh // instantiated objects we have to unset-set the sprite property uiImage.sprite = null; uiImage.sprite = spriteAsset.SpriteData.Sprite; } } } } }
// 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); } }
public void UpdateBorder(SVGSpriteAssetFile spriteAsset, Vector4 newBorder) { SVGSpriteData spriteData = spriteAsset.SpriteData; Sprite oldSprite = spriteData.Sprite; // create a new sprite (same texture, same rectangle, same pivot, different border) Sprite newSprite = Sprite.Create(oldSprite.texture, oldSprite.rect, spriteData.Pivot, SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT, 0, SpriteMeshType.FullRect, newBorder); spriteData.Border = newBorder; newSprite.name = oldSprite.name; // serialized copy from the new sprite (i.e. the sprite with updated border) to the old sprite (i.e. the sprite to be updated) // NB: we use this technique because there isn't an explicit sprite method that allows the change of border property EditorUtility.CopySerialized(newSprite, oldSprite); SVGUtils.MarkObjectDirty(oldSprite); // destroy the temporary sprite GameObject.DestroyImmediate(newSprite); }
private void DrawInspector() { bool resizeOnStart = EditorGUILayout.Toggle("Resize on Start()", this.m_EditedLoader.ResizeOnStart); bool updateTransform = EditorGUILayout.Toggle("Update transform", this.m_EditedLoader.UpdateTransform); string atlasName = (this.m_EditedLoader.Atlas != null) ? this.m_EditedLoader.Atlas.name : "<select>"; EditorGUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Atlas"); if (GUILayout.Button(atlasName, "DropDown")) { SVGAtlasSelector.Show("", this.OnAtlasSelect); } } EditorGUILayout.EndHorizontal(); if (this.m_EditedLoader.Atlas != null && this.m_EditedLoader.SpriteReference != null) { SVGSpriteAssetFile spriteAsset = this.m_EditedLoader.Atlas.GetGeneratedSprite(this.m_EditedLoader.SpriteReference); string buttonText = (spriteAsset != null) ? spriteAsset.SpriteData.Sprite.name : "<select>"; EditorGUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Sprite"); if (GUILayout.Button(buttonText, "DropDown")) { SVGSpriteSelector.Show(this.m_EditedLoader.Atlas, "", this.OnSpriteSelect); } } EditorGUILayout.EndHorizontal(); } if (this.m_EditedLoader.ResizeOnStart != resizeOnStart) { this.m_EditedLoader.ResizeOnStart = resizeOnStart; SVGUtils.MarkObjectDirty(this.m_EditedLoader); } if (this.m_EditedLoader.UpdateTransform != updateTransform) { this.m_EditedLoader.UpdateTransform = updateTransform; SVGUtils.MarkObjectDirty(this.m_EditedLoader); } }
private void DrawInspector() { bool resizeOnStart; EditorGUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Resize on Start()"); resizeOnStart = EditorGUILayout.Toggle(this.m_EditedLoader.ResizeOnStart); } EditorGUILayout.EndHorizontal(); SVGUIAtlas uiAtlas = this.m_EditedLoader.UIAtlas; if ((uiAtlas != null) && (this.m_EditedLoader.SpriteReference != null)) { SVGSpriteAssetFile spriteAsset = uiAtlas.GetGeneratedSprite(this.m_EditedLoader.SpriteReference); string buttonText = (spriteAsset != null) ? spriteAsset.SpriteData.Sprite.name : "<select>"; EditorGUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Sprite"); if (GUILayout.Button(buttonText, "DropDown")) { SVGSpriteSelector.Show(uiAtlas, "", this.OnSpriteSelect); } if (GUILayout.Button("Edit", GUILayout.Width(80))) { // show pivot editor SVGPivotEditor.Show(spriteAsset, this.OnSpriteEdited); } } EditorGUILayout.EndHorizontal(); } if (this.m_EditedLoader.ResizeOnStart != resizeOnStart) { this.m_EditedLoader.ResizeOnStart = resizeOnStart; SVGUtils.MarkObjectDirty(this.m_EditedLoader); } }
public override void OnInspectorGUI() { // get the target object SVGCanvasBehaviour canvasBehaviour = target as SVGCanvasBehaviour; if ((canvasBehaviour != null) && (canvasBehaviour.UIAtlas != null)) { Canvas canvas = canvasBehaviour.GetComponent <Canvas>(); if (canvas != null) { base.OnInspectorGUI(); // maintain the canvas scale factor synched between Canvas an SVGUIAtlas! canvasBehaviour.EnsureCanvasAssigned(); bool isDirty = this.DrawInspector(canvasBehaviour.UIAtlas, canvas); if (isDirty) { SVGUtils.MarkObjectDirty(canvasBehaviour.UIAtlas); SVGUtils.MarkSceneDirty(); } } } }
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(); } } }
// border editing callback private void OnSpriteEdited(PivotEditingResult result, SVGSpriteAssetFile spriteAsset, Vector2 editedPivot, Vector4 editedBorder) { SVGUISpriteLoaderBehaviour spriteLoader = target as SVGUISpriteLoaderBehaviour; if ((spriteLoader != null) && (result == PivotEditingResult.Ok)) { SVGUIAtlas uiAtlas = spriteLoader.UIAtlas; if (uiAtlas != null) { // assign the new border uiAtlas.UpdateBorder(spriteAsset, editedPivot, editedBorder); SVGUtils.MarkObjectDirty(uiAtlas); Image uiImage = spriteLoader.GetComponent <Image>(); if (uiImage != null) { // the Image component does not recognize the change of sprite border, so in order to refresh // instantiated objects we have to unset-set the sprite property uiImage.sprite = null; uiImage.sprite = spriteAsset.SpriteData.Sprite; } } } }
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); }