1
This commit is contained in:
@@ -3,7 +3,6 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MeshCombineStudio;
|
||||
using NGS.AdvancedCullingSystem.Dynamic;
|
||||
using UnityEditor.Build;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
#if UNITY_EDITOR
|
||||
|
8
Src/UnityPluginsSupport/SkiaSharp.meta
Normal file
8
Src/UnityPluginsSupport/SkiaSharp.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed9859b679a5a034dac17a0e578a7606
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "BITKit.Extensions.SkiaSharp",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:d525ad6bd40672747bde77962f1c401e",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b",
|
||||
"GUID:6de01b04fa4e14662b03fa46366da151",
|
||||
"GUID:f19bbd83e3c264a5680926bf75d7e494"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"_SkiaSharp"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c2aa13aa706ffc49a1a0044cad55436
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
121
Src/UnityPluginsSupport/SkiaSharp/SkiaExtensions.cs
Normal file
121
Src/UnityPluginsSupport/SkiaSharp/SkiaExtensions.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SkiaSharp;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public static class SkiaExtensions
|
||||
{
|
||||
public static Texture2D ToTexture2D(this SKImageInfo info,SKSurface surface)
|
||||
{
|
||||
// Okay, we're finished drawing. Now we create a Unity texture.
|
||||
TextureFormat format = (info.ColorType == SKColorType.Rgba8888) ? TextureFormat.RGBA32 : TextureFormat.BGRA32;
|
||||
var texture = new Texture2D(info.Width, info.Height, format, false, true);
|
||||
texture.wrapMode = TextureWrapMode.Clamp;
|
||||
|
||||
// Pull a Skia image object out of the canvas...
|
||||
var pixmap = surface.PeekPixels();
|
||||
// Copy it to the Unity texture...
|
||||
texture.LoadRawTextureData(pixmap.GetPixels(), pixmap.RowBytes * pixmap.Height);
|
||||
texture.Apply(false, true);
|
||||
// And drop it into the RawImage object.
|
||||
|
||||
return texture;
|
||||
}
|
||||
public static SKColor ToSKColor(this Color color,byte? alpha=null)
|
||||
{
|
||||
return new SKColor((byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), alpha??(byte)(color.a * 255));
|
||||
}
|
||||
public static SKColor ToSKColor(this Color32 color)
|
||||
{
|
||||
return new SKColor(color.r, color.g, color.b, color.a);
|
||||
}
|
||||
public static byte[] GetBytes(this SKSurface self)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
using var image = self.Snapshot();
|
||||
|
||||
|
||||
|
||||
using var data = image.Encode(SKEncodedImageFormat.Png, 40);
|
||||
|
||||
//using var stream = File.OpenWrite(exportPath.Value);
|
||||
var ms = new MemoryStream();
|
||||
// save the data to a stream
|
||||
data.SaveTo(ms);
|
||||
|
||||
var bytes = ms.ToArray();
|
||||
|
||||
var bitmap = OnFlipHorizontalClicked(bytes);
|
||||
|
||||
using var rotatedData = bitmap.Encode(SKEncodedImageFormat.Png, 40);
|
||||
|
||||
using var newMs = new MemoryStream();
|
||||
rotatedData.SaveTo(newMs);
|
||||
|
||||
return newMs.ToArray();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
StringBuilder exceptionBuilder = new();
|
||||
exceptionBuilder.AppendLine($"Surface:{self is not null}");
|
||||
BIT4Log.LogException( new InGameException(exceptionBuilder.ToString(),e));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public static string GetBase64(this SKSurface self)
|
||||
{
|
||||
try
|
||||
{
|
||||
var base64 = Convert.ToBase64String(self.GetBytes());
|
||||
|
||||
return "data:image/png;base64," + base64;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
StringBuilder exceptionBuilder = new();
|
||||
exceptionBuilder.AppendLine($"Surface:{self is not null}");
|
||||
BIT4Log.LogException( new InGameException(exceptionBuilder.ToString(),e));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public static SKBitmap OnFlipHorizontalClicked(byte[] bytes)
|
||||
{
|
||||
var bitmap = SKBitmap.Decode(bytes);
|
||||
var flippedBitmap = SKBitmap.Decode(bytes);
|
||||
|
||||
using var canvas = new SKCanvas(flippedBitmap);
|
||||
canvas.Clear();
|
||||
canvas.Scale(1, -1, 0, bitmap.Height / 2);
|
||||
canvas.DrawBitmap(bitmap, new SKPoint());
|
||||
return flippedBitmap;
|
||||
}
|
||||
public static SKBitmap Rotate(SKBitmap bitmap, double angle)
|
||||
{
|
||||
double radians = Math.PI * angle / 180;
|
||||
float sine = (float)Math.Abs(Math.Sin(radians));
|
||||
float cosine = (float)Math.Abs(Math.Cos(radians));
|
||||
int originalWidth = bitmap.Width;
|
||||
int originalHeight = bitmap.Height;
|
||||
int rotatedWidth = (int)(cosine * originalWidth + sine * originalHeight);
|
||||
int rotatedHeight = (int)(cosine * originalHeight + sine * originalWidth);
|
||||
|
||||
var rotatedBitmap = new SKBitmap(rotatedWidth, rotatedHeight);
|
||||
|
||||
using (var surface = new SKCanvas(rotatedBitmap))
|
||||
{
|
||||
surface.Translate(rotatedWidth / 2, rotatedHeight / 2);
|
||||
surface.RotateDegrees((float)angle);
|
||||
surface.Translate(-originalWidth / 2, -originalHeight / 2);
|
||||
surface.DrawBitmap(bitmap, new SKPoint());
|
||||
}
|
||||
return rotatedBitmap;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7e8feb080df7bc41ad23eb3faa350df
|
||||
guid: c23b988a0ca3904468edef1bd026f977
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
@@ -11,6 +11,11 @@ namespace BITKit.UX
|
||||
public static RenderTexture BlurredScreen;
|
||||
[SerializeField] private TranslucentImageSource source;
|
||||
[SerializeField] private RenderTexture blurredScreen;
|
||||
private void LateUpdate()
|
||||
{
|
||||
source.Request();
|
||||
BlurredScreen = blurredScreen = source.BlurredScreen;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -12,12 +12,23 @@ namespace BITKit.UX
|
||||
public TranslucentVisualElement()
|
||||
{
|
||||
RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
|
||||
generateVisualContent += DrawCanvas;
|
||||
}
|
||||
|
||||
private void DrawCanvas(MeshGenerationContext obj)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void OnGeometryChanged(GeometryChangedEvent evt)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (BITAppForUnity.IsPlaying is false) return;
|
||||
#endif
|
||||
if (style.display.value is not DisplayStyle.Flex) return;
|
||||
style.backgroundImage = new StyleBackground(Background.FromRenderTexture(TranslucentService.BlurredScreen));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.iOS;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace BITKit.UX
|
||||
@@ -25,12 +24,13 @@ namespace BITKit.UX
|
||||
image.enabled = obj;
|
||||
if (obj) alpha = 0;
|
||||
}
|
||||
private void Update()
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (BITAppForUnity.AllowCursor.Allow && alpha is not 1)
|
||||
{
|
||||
alpha = Mathf.Clamp01(alpha + Time.deltaTime*5);
|
||||
image.color = new Color(0, 0, 0, alpha);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,7 @@
|
||||
{
|
||||
"name": "BITKit.Extension.ZXing",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:4d2db42e8a830b54fa78ebb94e912288",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23"
|
||||
],
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
|
@@ -1,63 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using ZXing;
|
||||
using ZXing.QrCode;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ZXing.Common;
|
||||
namespace BITKit.UX.Components
|
||||
{
|
||||
[System.Serializable]
|
||||
public class ZXingQRBuilder : StringComponent
|
||||
{
|
||||
public TranslateSO so;
|
||||
public UXImage image;
|
||||
protected override async void OnSet(string value)
|
||||
{
|
||||
var url = so.Get(value, false);
|
||||
await UniTask.SwitchToMainThread();
|
||||
var tex = GenerateQRImageWithColor(url, 256, 256, Color.black, out var bitMatrix);
|
||||
image.Set(tex);
|
||||
}
|
||||
|
||||
/// <param name="height"></param>
|
||||
Texture2D GenerateQRImageWithColor(string content, int width, int height, Color color, out BitMatrix bitMatrix)
|
||||
{
|
||||
// 编码成color32
|
||||
MultiFormatWriter writer = new MultiFormatWriter();
|
||||
Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
|
||||
//设置字符串转换格式,确保字符串信息保持正确
|
||||
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
|
||||
// 设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)
|
||||
hints.Add(EncodeHintType.MARGIN, 1);
|
||||
hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.M);
|
||||
//实例化字符串绘制二维码工具
|
||||
bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
|
||||
|
||||
// 转成texture2d
|
||||
int w = bitMatrix.Width;
|
||||
int h = bitMatrix.Height;
|
||||
Texture2D texture = new Texture2D(w, h);
|
||||
for (int x = 0; x < h; x++)
|
||||
{
|
||||
for (int y = 0; y < w; y++)
|
||||
{
|
||||
if (bitMatrix[x, y])
|
||||
{
|
||||
texture.SetPixel(y, x, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
texture.SetPixel(y, x, Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
texture.Apply();
|
||||
|
||||
//byte[] bytes = texture.EncodeToPNG();
|
||||
//string path = System.IO.Path.Combine(Application.dataPath, "qr.png");
|
||||
//System.IO.File.WriteAllBytes(path, bytes);
|
||||
return texture;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user