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()
    {

    }
    void OnRectTransformDimensionsChange(){
		Refresh();
	}
	
	private void OnValidate(){
		Refresh();
	}

	private void Refresh(){
		if (material != null)
		{
			var rect = ((RectTransform)transform).rect;
			material.SetVector(Props, new Vector4(rect.width, rect.height, radius*2, 0));
			gameObject.GetComponent<Image>().material = material;
			gameObject.GetComponent<Image>().sprite = null;
		}
		else 
		{
			material = Instantiate(Resources.Load<Material>("UI/Material/RoundedCornersTextureMaterial"));
		}
	}
}
