44using UnityEngine . UI ;
55
66namespace 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
0 commit comments