-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b036ff
commit 29f8cfa
Showing
5 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters