Skip to content

Commit 97d6b59

Browse files
authored
Merge pull request #48 from xiabob/master
Support SpriteAtlas
2 parents 8862398 + dec85de commit 97d6b59

4 files changed

Lines changed: 74 additions & 52 deletions

File tree

UiRoundedCorners/ImageWithIndependentRoundedCorners.cs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
using UnityEngine.UI;
55

66
namespace Nobi.UiRoundedCorners {
7-
[ExecuteInEditMode] //Required to do validation with OnEnable()
8-
[DisallowMultipleComponent] //You can only have one of these in every object
9-
[RequireComponent(typeof(RectTransform))]
7+
[ExecuteInEditMode] //Required to do validation with OnEnable()
8+
[DisallowMultipleComponent] //You can only have one of these in every object
9+
[RequireComponent(typeof(RectTransform))]
1010
public class ImageWithIndependentRoundedCorners : MonoBehaviour {
1111
private static readonly int prop_halfSize = Shader.PropertyToID("_halfSize");
1212
private static readonly int prop_radiuses = Shader.PropertyToID("_r");
1313
private static readonly int prop_rect2props = Shader.PropertyToID("_rect2props");
14+
private static readonly int prop_OuterUV = Shader.PropertyToID("_OuterUV");
1415

1516
// Vector2.right rotated clockwise by 45 degrees
1617
private static readonly Vector2 wNorm = new Vector2(.7071068f, -.7071068f);
1718
// Vector2.right rotated counter-clockwise by 45 degrees
1819
private static readonly Vector2 hNorm = new Vector2(.7071068f, .7071068f);
1920

20-
public Vector4 r = new Vector4(40f, 40f, 40f, 40f);
21-
private Material material;
21+
public Vector4 r = new Vector4(40f, 40f, 40f, 40f);
22+
private Material material;
23+
private Vector4 outerUV = new Vector4(0, 0, 1, 1);
2224

2325
// xy - position,
2426
// zw - halfSize
@@ -31,16 +33,15 @@ private void OnValidate() {
3133
}
3234

3335
private void OnEnable() {
34-
//You can only add either ImageWithRoundedCorners or ImageWithIndependentRoundedCorners
36+
//You can only add either ImageWithRoundedCorners or ImageWithIndependentRoundedCorners
3537
//It will replace the other component when added into the object.
36-
var other = GetComponent<ImageWithRoundedCorners>();
37-
if (other != null)
38-
{
39-
r = Vector4.one * other.radius; //When it does, transfer the radius value to this script
40-
DestroyHelper.Destroy(other);
41-
}
42-
43-
Validate();
38+
var other = GetComponent<ImageWithRoundedCorners>();
39+
if (other != null) {
40+
r = Vector4.one * other.radius; //When it does, transfer the radius value to this script
41+
DestroyHelper.Destroy(other);
42+
}
43+
44+
Validate();
4445
Refresh();
4546
}
4647

@@ -51,9 +52,9 @@ private void OnRectTransformDimensionsChange() {
5152
}
5253

5354
private void OnDestroy() {
54-
image.material = null; //This makes so that when the component is removed, the UI material returns to null
55+
image.material = null; //This makes so that when the component is removed, the UI material returns to null
5556

56-
DestroyHelper.Destroy(material);
57+
DestroyHelper.Destroy(material);
5758
image = null;
5859
material = null;
5960
}
@@ -70,6 +71,10 @@ public void Validate() {
7071
if (image != null) {
7172
image.material = material;
7273
}
74+
75+
if (image is Image uiImage && uiImage.sprite != null) {
76+
outerUV = UnityEngine.Sprites.DataUtility.GetOuterUV(uiImage.sprite);
77+
}
7378
}
7479

7580
public void Refresh() {
@@ -78,6 +83,7 @@ public void Refresh() {
7883
material.SetVector(prop_rect2props, rect2props);
7984
material.SetVector(prop_halfSize, rect.size * .5f);
8085
material.SetVector(prop_radiuses, r);
86+
material.SetVector(prop_OuterUV, outerUV);
8187
}
8288

8389
private void RecalculateProps(Vector2 size) {
@@ -120,24 +126,22 @@ private void RecalculateProps(Vector2 size) {
120126
/// Display Vector4 as 4 separate fields for each corners.
121127
/// It's way easier to use than w,x,y,z in Vector4.
122128
/// </summary>
123-
#if UNITY_EDITOR
129+
#if UNITY_EDITOR
124130
[CustomEditor(typeof(ImageWithIndependentRoundedCorners))]
125-
public class Vector4Editor : Editor
126-
{
127-
public override void OnInspectorGUI()
128-
{
129-
//DrawDefaultInspector();
131+
public class Vector4Editor : Editor {
132+
public override void OnInspectorGUI() {
133+
//DrawDefaultInspector();
130134

131-
serializedObject.Update();
135+
serializedObject.Update();
132136

133-
SerializedProperty vector4Prop = serializedObject.FindProperty("r");
137+
SerializedProperty vector4Prop = serializedObject.FindProperty("r");
134138

135-
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("x"), new GUIContent("Top Left Corner"));
136-
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("y"), new GUIContent("Top Right Corner"));
137-
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("w"), new GUIContent("Bottom Left Corner"));
138-
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("z"), new GUIContent("Bottom Right Corner"));
139+
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("x"), new GUIContent("Top Left Corner"));
140+
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("y"), new GUIContent("Top Right Corner"));
141+
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("w"), new GUIContent("Bottom Left Corner"));
142+
EditorGUILayout.PropertyField(vector4Prop.FindPropertyRelative("z"), new GUIContent("Bottom Right Corner"));
139143

140-
serializedObject.ApplyModifiedProperties();
141-
}
144+
serializedObject.ApplyModifiedProperties();
145+
}
142146
}
143147
#endif

UiRoundedCorners/ImageWithRoundedCorners.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
using UnityEngine.UI;
33

44
namespace Nobi.UiRoundedCorners {
5-
[ExecuteInEditMode] //Required to check the OnEnable function
6-
[DisallowMultipleComponent] //You can only have one of these in every object.
7-
[RequireComponent(typeof(RectTransform))]
5+
[ExecuteInEditMode] //Required to check the OnEnable function
6+
[DisallowMultipleComponent] //You can only have one of these in every object.
7+
[RequireComponent(typeof(RectTransform))]
88
public class ImageWithRoundedCorners : MonoBehaviour {
99
private static readonly int Props = Shader.PropertyToID("_WidthHeightRadius");
10+
private static readonly int prop_OuterUV = Shader.PropertyToID("_OuterUV");
1011

11-
public float radius = 40f;
12-
private Material material;
12+
public float radius = 40f;
13+
private Material material;
14+
private Vector4 outerUV = new Vector4(0, 0, 1, 1);
1315

1416
[HideInInspector, SerializeField] private MaskableGraphic image;
1517

@@ -19,24 +21,23 @@ private void OnValidate() {
1921
}
2022

2123
private void OnDestroy() {
22-
image.material = null; //This makes so that when the component is removed, the UI material returns to null
24+
image.material = null; //This makes so that when the component is removed, the UI material returns to null
2325

24-
DestroyHelper.Destroy(material);
26+
DestroyHelper.Destroy(material);
2527
image = null;
2628
material = null;
2729
}
2830

2931
private void OnEnable() {
30-
//You can only add either ImageWithRoundedCorners or ImageWithIndependentRoundedCorners
31-
//It will replace the other component when added into the object.
32-
var other = GetComponent<ImageWithIndependentRoundedCorners>();
33-
if (other != null)
34-
{
35-
radius = other.r.x; //When it does, transfer the radius value to this script
36-
DestroyHelper.Destroy(other);
37-
}
32+
//You can only add either ImageWithRoundedCorners or ImageWithIndependentRoundedCorners
33+
//It will replace the other component when added into the object.
34+
var other = GetComponent<ImageWithIndependentRoundedCorners>();
35+
if (other != null) {
36+
radius = other.r.x; //When it does, transfer the radius value to this script
37+
DestroyHelper.Destroy(other);
38+
}
3839

39-
Validate();
40+
Validate();
4041
Refresh();
4142
}
4243

@@ -58,14 +59,19 @@ public void Validate() {
5859
if (image != null) {
5960
image.material = material;
6061
}
62+
63+
if (image is Image uiImage && uiImage.sprite != null) {
64+
outerUV = UnityEngine.Sprites.DataUtility.GetOuterUV(uiImage.sprite);
65+
}
6166
}
6267

6368
public void Refresh() {
6469
var rect = ((RectTransform)transform).rect;
6570

66-
//Multiply radius value by 2 to make the radius value appear consistent with ImageWithIndependentRoundedCorners script.
67-
//Right now, the ImageWithIndependentRoundedCorners appears to have double the radius than this.
68-
material.SetVector(Props, new Vector4(rect.width, rect.height, radius * 2, 0));
69-
}
71+
//Multiply radius value by 2 to make the radius value appear consistent with ImageWithIndependentRoundedCorners script.
72+
//Right now, the ImageWithIndependentRoundedCorners appears to have double the radius than this.
73+
material.SetVector(Props, new Vector4(rect.width, rect.height, radius * 2, 0));
74+
material.SetVector(prop_OuterUV, outerUV);
75+
}
7076
}
7177
}

UiRoundedCorners/IndependentRoundedCorners.shader

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
_r ("r", Vector) = (0,0,0,0)
1616
_halfSize ("halfSize", Vector) = (0,0,0,0)
1717
_rect2props ("rect2props", Vector) = (0,0,0,0)
18+
_OuterUV ("image outer uv", Vector) = (0, 0, 1, 1)
1819
// ---
1920
}
2021

@@ -58,11 +59,16 @@
5859
float4 _r;
5960
float4 _halfSize;
6061
float4 _rect2props;
62+
float4 _OuterUV;
6163
sampler2D _MainTex;
6264
float4 _ClipRect;
6365
fixed4 _TextureSampleAdd;
6466

6567
fixed4 frag (v2f i) : SV_Target {
68+
float2 uvSample = i.uv;
69+
uvSample.x = (uvSample.x - _OuterUV.x) / (_OuterUV.z - _OuterUV.x);
70+
uvSample.y = (uvSample.y - _OuterUV.y) / (_OuterUV.w - _OuterUV.y);
71+
6672
half4 color = (tex2D(_MainTex, i.uv) + _TextureSampleAdd) * i.color;
6773

6874
#ifdef UNITY_UI_CLIP_RECT
@@ -77,7 +83,7 @@
7783
return color;
7884
}
7985

80-
float alpha = CalcAlphaForIndependentCorners(i.uv, _halfSize.xy, _rect2props, _r);
86+
float alpha = CalcAlphaForIndependentCorners(uvSample, _halfSize.xy, _rect2props, _r);
8187

8288
#ifdef UNITY_UI_ALPHACLIP
8389
clip(alpha - 0.001);

UiRoundedCorners/RoundedCorners.shader

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Shader "UI/RoundedCorners/RoundedCorners" {
1313

1414
// Definition in Properties section is required to Mask works properly
1515
_WidthHeightRadius ("WidthHeightRadius", Vector) = (0,0,0,0)
16+
_OuterUV ("image outer uv", Vector) = (0, 0, 1, 1)
1617
// ---
1718
}
1819

@@ -54,11 +55,16 @@ Shader "UI/RoundedCorners/RoundedCorners" {
5455
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
5556

5657
float4 _WidthHeightRadius;
58+
float4 _OuterUV;
5759
sampler2D _MainTex;
5860
fixed4 _TextureSampleAdd;
5961
float4 _ClipRect;
6062

6163
fixed4 frag (v2f i) : SV_Target {
64+
float2 uvSample = i.uv;
65+
uvSample.x = (uvSample.x - _OuterUV.x) / (_OuterUV.z - _OuterUV.x);
66+
uvSample.y = (uvSample.y - _OuterUV.y) / (_OuterUV.w - _OuterUV.y);
67+
6268
half4 color = (tex2D(_MainTex, i.uv) + _TextureSampleAdd) * i.color;
6369

6470
#ifdef UNITY_UI_CLIP_RECT
@@ -73,7 +79,7 @@ Shader "UI/RoundedCorners/RoundedCorners" {
7379
return color;
7480
}
7581

76-
float alpha = CalcAlpha(i.uv, _WidthHeightRadius.xy, _WidthHeightRadius.z);
82+
float alpha = CalcAlpha(uvSample, _WidthHeightRadius.xy, _WidthHeightRadius.z);
7783

7884
#ifdef UNITY_UI_ALPHACLIP
7985
clip(alpha - 0.001);

0 commit comments

Comments
 (0)