Skip to content

Commit

Permalink
Added SVG editor (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammariqais committed Mar 13, 2024
1 parent 4b036ff commit 29f8cfa
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 12 deletions.
44 changes: 44 additions & 0 deletions SkiaUnity/Assets/SkiaSharp/Editor/SVGEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#if UNITY_EDITOR

using SkiaSharp.Unity;
using SkiaSharp.Unity.HB;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using TextAlignment = Topten.RichTextKit.TextAlignment;


[CustomEditor(typeof(SVGLoader))]
public class SVGEditor : Editor {
private GUIStyle selectedStyle;
private SerializedProperty svgFile;
bool showHaloSettings = false;
bool showMoreSettings = false;

private void OnEnable(){
selectedStyle = new GUIStyle();
selectedStyle.normal.background = EditorGUIUtility.Load("builtin skins/darkskin/images/pre background@2x.png") as Texture2D;
selectedStyle.normal.textColor = Color.white;
selectedStyle.alignment = TextAnchor.MiddleCenter;
selectedStyle.fontSize = 14;

// Find the serialized property for fontSize
svgFile = serializedObject.FindProperty("svgFile");
}
public override void OnInspectorGUI(){
SVGLoader script = (SVGLoader)target;
GUIStyle largeLabelStyle = new GUIStyle(GUI.skin.label);
largeLabelStyle.fontSize = 12; // Adjust the font size as needed
largeLabelStyle.fontStyle = FontStyle.Bold;
//script.text = EditorGUILayout.TextArea(script.text, GUILayout.Height(100));
//textProperty.stringValue = script.text;
EditorGUILayout.PropertyField(svgFile);


serializedObject.ApplyModifiedProperties();
if (!EditorApplication.isPlayingOrWillChangePlaymode) {
script.RenderSVG();
}
}
}
#endif
11 changes: 11 additions & 0 deletions SkiaUnity/Assets/SkiaSharp/Editor/SVGEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 56 additions & 8 deletions SkiaUnity/Assets/SkiaSharp/Runtime/SVGLoader.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,83 @@
using System;
using UnityEngine;
using Svg.Skia;
using UnityEngine.UI;

namespace SkiaSharp.Unity {
public class SVGLoader : MonoBehaviour {
public class SVGLoader : MonoBehaviour, ILayoutElement {
[SerializeField]
private TextAsset svgFile;
public TextAsset svgFile;

private SKBitmap bmp;
private SKSurface surface;
private Texture2D texture;
private RawImage rawImage;
private int currentWidth, currentHeight;

void Start() {
rawImage = GetComponent<RawImage>();

RenderSVG();
}

public void RenderSVG() {
if (svgFile == null) {
return;
}
if (rawImage == null) {
rawImage = GetComponent<RawImage>();
}

if (texture != null) {
#if !UNITY_EDITOR
DestroyImmediate(texture);
#else
DestroyImmediate(texture);
#endif
}

using (var svg = new SKSvg())
{
if (svg.FromSvg(svgFile.text) is { }) {
bmp = svg.Picture.ToBitmap(SKColor.Empty, rawImage.GetComponent<RectTransform>().rect.width/svg.Picture.CullRect.Width, rawImage.GetComponent<RectTransform>().rect.height/ svg.Picture.CullRect.Height, SKColorType.Rgba8888, SKAlphaType.Opaque, null);
if (svg.FromSvg(svgFile.text) is { }) {
if (rawImage.GetComponent<RectTransform>().rect.width == 0 ||
rawImage.GetComponent<RectTransform>().rect.height == 0) {
return;
}
int roundedWidth = Mathf.CeilToInt(rawImage.GetComponent<RectTransform>().rect.width / 4f) * 4;
int roundedHeight = Mathf.CeilToInt(rawImage.GetComponent<RectTransform>().rect.height / 4f) * 4;
currentWidth = roundedWidth;
currentHeight = roundedHeight;
bmp = svg.Picture.ToBitmap(SKColor.Empty, currentWidth/svg.Picture.CullRect.Width, currentHeight/ svg.Picture.CullRect.Height, SKColorType.Rgba8888, SKAlphaType.Opaque, null);
surface = SKSurface.Create(bmp.Info);
SKCanvas canvas = new SKCanvas(bmp);
TextureFormat format = (bmp.Info.ColorType == SKColorType.Rgba8888) ? TextureFormat.RGBA32 : TextureFormat.BGRA32;
texture = new Texture2D(bmp.Info.Width, bmp.Info.Height, format, false);
texture = new Texture2D(roundedWidth, roundedHeight, format, false);
texture.wrapMode = TextureWrapMode.Repeat;
var pixmap = surface.PeekPixels();
texture.LoadRawTextureData(bmp.GetPixels(), pixmap.RowBytes * pixmap.Height);
texture.name = "SVG";
texture.Apply();
rawImage.texture = texture;
}
}
}

private void Update() {
int roundedWidthNew = Mathf.CeilToInt(rawImage.GetComponent<RectTransform>().rect.width / 4f) * 4;
int roundedHeightNew = Mathf.CeilToInt(rawImage.GetComponent<RectTransform>().rect.height / 4f) * 4;
if (currentWidth != roundedWidthNew ||
currentHeight != roundedHeightNew) {
RenderSVG();
}
}

public void CalculateLayoutInputHorizontal() { }

public void CalculateLayoutInputVertical() { }

public float minWidth { get; }
public float preferredWidth => rawImage.GetComponent<RectTransform>().rect.width;
public float flexibleWidth { get; }
public float minHeight { get; }
public float preferredHeight => rawImage.GetComponent<RectTransform>().rect.height;
public float flexibleHeight { get; }
public int layoutPriority { get; }
}
}
8 changes: 5 additions & 3 deletions SkiaUnity/Assets/SkiaSharp/Runtime/SkottiePlayerV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public class SkottiePlayerV2 : MonoBehaviour {
[SerializeField]
private string stateName;
[SerializeField]
private bool resetAfterFinished = false;
public bool resetAfterFinished = false;
[SerializeField]
private bool autoPlay = false;
public bool autoPlay = false;
[SerializeField]
private bool loop = false;
public bool loop = false;

public UnityAction<string> OnAnimationFinished;
public UnityAction OnAnimationInit;

private Animation currentAnimation;
private SKCanvas canvas;
Expand Down Expand Up @@ -106,6 +107,7 @@ void LoadTexture() {
} else {
spriteRenderer.sprite = Sprite.Create(texture,new Rect(0,0,texture.width,texture.height),Vector2.one*0.5f,100f,0);
}
OnAnimationInit?.Invoke();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SkiaUnity/Assets/SkiaSharp/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.ammariqais.skiaforunity",
"displayName": "Skia For Unity",
"version": "3.0.0-pre.6",
"version": "3.0.1",
"unity": "2019.3",
"description": "Skia For Unity With Harfbuzz texts, Skottie Animations, lottie",
"author": {
Expand Down

0 comments on commit 29f8cfa

Please sign in to comment.