3.0 - unitypackage

This commit is contained in:
monitor1394
2022-01-05 21:40:48 +08:00
parent c160867765
commit 228a4b2840
846 changed files with 105 additions and 467693 deletions

1784
Runtime/XUGL/UGL.cs Normal file

File diff suppressed because it is too large Load Diff

11
Runtime/XUGL/UGL.cs.meta Normal file
View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 463dc57c2fc1849379941a7facf8dc84
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using UnityEngine;
using UnityEngine.UI;
namespace XUGL
{
[ExecuteInEditMode]
public class UGLExample : MaskableGraphic
{
private float m_Width = 800;
private float m_Height = 800;
private Vector3 m_Center = Vector3.zero;
private Vector3 m_LeftTopPos = Vector3.zero;
private Color32 m_BackgroundColor = new Color32(224, 224, 224, 255);
private Color32 m_DrawColor = new Color32(255, 132, 142, 255);
private float[] m_BorderRadius = new float[] { 5, 5, 10, 10 };
protected override void Awake()
{
base.Awake();
var rectTransform = GetComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(500, 500);
rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
rectTransform.pivot = new Vector2(0.5f, 0.5f);
m_Center = Vector3.zero;
m_LeftTopPos = new Vector3(-m_Width / 2, m_Height / 2);
}
protected override void OnPopulateMesh(VertexHelper vh)
{
Vector3 sp, cp, ep;
vh.Clear();
//背景边框
UGL.DrawSquare(vh, m_Center, m_Width / 2, m_BackgroundColor);
UGL.DrawBorder(vh, m_Center, m_Width, m_Height, 40, Color.green, Color.red, 0, m_BorderRadius,false,1);
//点
UGL.DrawCricle(vh, m_LeftTopPos + new Vector3(20, -20), 10, m_DrawColor);
//直线
sp = new Vector3(m_LeftTopPos.x + 50, m_LeftTopPos.y - 20);
ep = new Vector3(m_LeftTopPos.x + 250, m_LeftTopPos.y - 20);
UGL.DrawLine(vh, sp, ep, 3, m_DrawColor);
//3点确定的折线
sp = new Vector3(m_LeftTopPos.x + 20, m_LeftTopPos.y - 100);
cp = new Vector3(m_LeftTopPos.x + 200, m_LeftTopPos.y - 40);
ep = new Vector3(m_LeftTopPos.x + 250, m_LeftTopPos.y - 80);
UGL.DrawLine(vh, sp, cp, ep, 5, m_DrawColor);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8a87ea5df031473da3eb5fb8f57e20a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

330
Runtime/XUGL/UGLHelper.cs Normal file
View File

@@ -0,0 +1,330 @@
using System.Collections.Generic;
using UnityEngine;
namespace XUGL
{
public static class UGLHelper
{
public static bool IsValueEqualsColor(Color32 color1, Color32 color2)
{
return color1.a == color2.a
&& color1.b == color2.b
&& color1.g == color2.g
&& color1.r == color2.r;
}
public static bool IsValueEqualsColor(Color color1, Color color2)
{
return color1.a == color2.a
&& color1.b == color2.b
&& color1.g == color2.g
&& color1.r == color2.r;
}
public static bool IsValueEqualsString(string str1, string str2)
{
if (str1 == null && str2 == null)
return true;
else if (str1 != null && str2 != null)
return str1.Equals(str2);
else return false;
}
public static bool IsValueEqualsVector2(Vector2 v1, Vector2 v2)
{
return v1.x == v2.x
&& v1.y == v2.y;
}
public static bool IsValueEqualsVector3(Vector3 v1, Vector3 v2)
{
return v1.x == v2.x
&& v1.y == v2.y
&& v1.z == v2.z;
}
public static bool IsValueEqualsList<T>(List<T> list1, List<T> list2)
{
if (list1 == null || list2 == null)
return false;
if (list1.Count != list2.Count)
return false;
for (int i = 0; i < list1.Count; i++)
{
if (list1[i] == null && list2[i] == null)
{
}
else
{
if (list1[i] != null)
{
if (!list1[i].Equals(list2[i]))
return false;
}
else
{
if (!list2[i].Equals(list1[i]))
return false;
}
}
}
return true;
}
public static bool IsClearColor(Color32 color)
{
return color.a == 0
&& color.b == 0
&& color.g == 0
&& color.r == 0;
}
public static bool IsClearColor(Color color)
{
return color.a == 0
&& color.b == 0
&& color.g == 0
&& color.r == 0;
}
public static bool IsZeroVector(Vector3 pos)
{
return pos.x == 0
&& pos.y == 0
&& pos.z == 0;
}
public static Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
{
Vector3 point = Quaternion.AngleAxis(angle, axis) * (position - center);
Vector3 resultVec3 = center + point;
return resultVec3;
}
public static void GetBezierList(ref List<Vector3> posList, Vector3 sp, Vector3 ep,
Vector3 lsp, Vector3 nep, float smoothness = 2f, float k = 2.0f)
{
float dist = Mathf.Abs(sp.x - ep.x);
Vector3 cp1, cp2;
var dir = (ep - sp).normalized;
var diff = dist / k;
if (lsp == sp)
{
cp1 = sp + dist / k * dir * 1;
cp1.y = sp.y;
cp1 = sp;
}
else
{
cp1 = sp + (ep - lsp).normalized * diff;
}
if (nep == ep) cp2 = ep;
else cp2 = ep - (nep - sp).normalized * diff;
dist = Vector3.Distance(sp, ep);
int segment = (int)(dist / (smoothness <= 0 ? 2f : smoothness));
if (segment < 1) segment = (int)(dist / 0.5f);
if (segment < 4) segment = 4;
GetBezierList2(ref posList, sp, ep, segment, cp1, cp2);
if (posList.Count < 2)
{
posList.Clear();
posList.Add(sp);
posList.Add(ep);
}
}
public static void GetBezierListVertical(ref List<Vector3> posList, Vector3 sp, Vector3 ep,
float smoothness = 2f, float k = 2.0f)
{
Vector3 dir = (ep - sp).normalized;
float dist = Vector3.Distance(sp, ep);
Vector3 cp1 = sp + dist / k * dir * 1;
Vector3 cp2 = sp + dist / k * dir * (k - 1);
cp1.x = sp.x;
cp2.x = ep.x;
int segment = (int)(dist / (smoothness <= 0 ? 2f : smoothness));
GetBezierList2(ref posList, sp, ep, segment, cp1, cp2);
if (posList.Count < 2)
{
posList.Clear();
posList.Add(sp);
posList.Add(ep);
}
}
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, int segment, Vector3 cp)
{
List<Vector3> list = new List<Vector3>();
for (int i = 0; i < segment; i++)
{
list.Add(GetBezier(i / (float)segment, sp, cp, ep));
}
list.Add(ep);
return list;
}
public static void GetBezierList2(ref List<Vector3> posList, Vector3 sp, Vector3 ep,
int segment, Vector3 cp, Vector3 cp2)
{
posList.Clear();
if (posList.Capacity < segment + 1)
{
posList.Capacity = segment + 1;
}
for (int i = 0; i < segment; i++)
{
posList.Add((GetBezier2(i / (float)segment, sp, cp, cp2, ep)));
}
posList.Add(ep);
}
public static Vector3 GetBezier(float t, Vector3 sp, Vector3 cp, Vector3 ep)
{
Vector3 aa = sp + (cp - sp) * t;
Vector3 bb = cp + (ep - cp) * t;
return aa + (bb - aa) * t;
}
public static Vector3 GetBezier2(float t, Vector3 sp, Vector3 p1, Vector3 p2, Vector3 ep)
{
t = Mathf.Clamp01(t);
var oneMinusT = 1f - t;
return oneMinusT * oneMinusT * oneMinusT * sp +
3f * oneMinusT * oneMinusT * t * p1 +
3f * oneMinusT * t * t * p2 +
t * t * t * ep;
}
public static Vector3 GetDire(float angle, bool isDegree = false)
{
angle = isDegree ? angle * Mathf.Deg2Rad : angle;
return new Vector3(Mathf.Sin(angle), Mathf.Cos(angle));
}
public static Vector3 GetVertialDire(Vector3 dire)
{
if (dire.x == 0)
return new Vector3(-1, 0, 0);
if (dire.y == 0)
return new Vector3(0, -1, 0);
else
return new Vector3(-dire.y / dire.x, 1, 0).normalized;
}
/// <summary>
/// 获得0-360的角度12点钟方向为0度
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <returns></returns>
public static float GetAngle360(Vector2 from, Vector2 to)
{
float angle;
Vector3 cross = Vector3.Cross(from, to);
angle = Vector2.Angle(from, to);
angle = cross.z > 0 ? -angle : angle;
angle = (angle + 360) % 360;
return angle;
}
public static Vector3 GetPos(Vector3 center, float radius, float angle, bool isDegree = false)
{
angle = isDegree ? angle * Mathf.Deg2Rad : angle;
return new Vector3(center.x + radius * Mathf.Sin(angle),
center.y + radius * Mathf.Cos(angle));
}
/// <summary>
/// 获得两直线的交点
/// </summary>
/// <param name="p1">线段1起点</param>
/// <param name="p2">线段1终点</param>
/// <param name="p3">线段2起点</param>
/// <param name="p4">线段2终点</param>
/// <param name="intersection">相交点。当不想交时默认为 Vector3.zero </param>
/// <returns>相交则返回 true, 否则返回 false</returns>
public static bool GetIntersection(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, ref Vector3 intersection)
{
intersection = Vector3.zero;
var d = (p2.x - p1.x) * (p4.y - p3.y) - (p2.y - p1.y) * (p4.x - p3.x);
if (d == 0)
return false;
var u = ((p3.x - p1.x) * (p4.y - p3.y) - (p3.y - p1.y) * (p4.x - p3.x)) / d;
var v = ((p3.x - p1.x) * (p2.y - p1.y) - (p3.y - p1.y) * (p2.x - p1.x)) / d;
if (u < 0 || u > 1 || v < 0 || v > 1)
return false;
intersection.x = p1.x + u * (p2.x - p1.x);
intersection.y = p1.y + u * (p2.y - p1.y);
return true;
}
/// <summary>
/// 三个点画线段所需要的六个关键点
/// </summary>
/// <param name="lp">上一个点</param>
/// <param name="cp">当前点</param>
/// <param name="np">下一个点</param>
/// <param name="width">线段宽度</param>
/// <param name="ltp">上一个点的上角点</param>
/// <param name="lbp">上一个点的下角点</param>
/// <param name="ntp">下一个点的上角点</param>
/// <param name="nbp">下一个点的下角点</param>
/// <param name="itp">交汇点的上角点</param>
/// <param name="ibp">交汇点的下角点</param>
internal static void GetLinePoints(Vector3 lp, Vector3 cp, Vector3 np, float width,
ref Vector3 ltp, ref Vector3 lbp,
ref Vector3 ntp, ref Vector3 nbp,
ref Vector3 itp, ref Vector3 ibp,
ref Vector3 clp, ref Vector3 crp,
ref bool bitp, ref bool bibp, int debugIndex = 0
)
{
var dir1 = (cp - lp).normalized;
var dir2 = (cp - np).normalized;
var dir1v = Vector3.Cross(dir1, Vector3.forward).normalized;
var dir2v = Vector3.Cross(dir2, Vector3.back).normalized;
ltp = lp - dir1v * width;
lbp = lp + dir1v * width;
ntp = np - dir2v * width;
nbp = np + dir2v * width;
clp = cp - dir2v * width;
crp = cp + dir2v * width;
var ldist = (Vector3.Distance(cp, lp) + 1) * dir1;
var rdist = (Vector3.Distance(cp, np) + 1) * dir2;
bitp = true;
if (!UGLHelper.GetIntersection(ltp, ltp + ldist, ntp, ntp + rdist, ref itp))
{
itp = cp - dir1v * width;
clp = cp - dir1v * width;
crp = cp - dir2v * width;
bitp = false;
}
bibp = true;
if (!UGLHelper.GetIntersection(lbp, lbp + ldist, nbp, nbp + rdist, ref ibp))
{
ibp = cp + dir1v * width;
clp = cp + dir1v * width;
crp = cp + dir2v * width;
bibp = false;
}
if (bitp == false && bibp == false && cp == np)
{
ltp = cp - dir1v * width;
clp = cp + dir1v * width;
crp = cp + dir1v * width;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cc77f59a050d547caa3de82f4a9abd99
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: