using DG.Tweening; using System; using UnityEngine; using UnityEngine.UI; [ExecuteInEditMode] public class ImageWithRoundedCorners : MonoBehaviour { private static readonly int Props = Shader.PropertyToID("_WidthHeightRadius"); private Material material; public float radius; private void Start() { Refresh(); } void OnRectTransformDimensionsChange() { Refresh(); } private void OnValidate() { Refresh(); } private void Refresh(){ if (material == null) { material = Instantiate(Resources.Load("UI/Material/RoundedCornersTextureMaterial")); } var rect = ((RectTransform)transform).rect; material.SetVector(Props, new Vector4(rect.width, rect.height, radius * 2, 0)); if (gameObject.GetComponent() != null) { gameObject.GetComponent().material = material; //gameObject.GetComponent().sprite = null; } else if (gameObject.GetComponent() != null) { gameObject.GetComponent().material = material; } } public void Refresh(Vector2 r) { material.SetVector(Props, new Vector4(r.x, r.y, radius * 2, 0)); if (gameObject.GetComponent() != null) { gameObject.GetComponent().material = material; gameObject.GetComponent().sprite = null; } else if (gameObject.GetComponent() != null) { gameObject.GetComponent().material = material; } } }