powerfun-unity/Assets/Cyp/Circle/ImageWithRoundedCorners.cs
2021-03-29 09:19:48 +08:00

37 lines
822 B
C#

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, 0));
gameObject.GetComponent<Image>().material = material;
gameObject.GetComponent<Image>().sprite = null;
}
else
{
material = Instantiate(Resources.Load<Material>("UI/Material/RoundedCornersTextureMaterial"));
}
}
}