45 lines
1.0 KiB
C#
45 lines
1.0 KiB
C#
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));
|
|
if (gameObject.GetComponent<Image>() != null)
|
|
{
|
|
gameObject.GetComponent<Image>().material = material;
|
|
gameObject.GetComponent<Image>().sprite = null;
|
|
}
|
|
else if (gameObject.GetComponent<RawImage>()!=null)
|
|
{
|
|
gameObject.GetComponent<RawImage>().material = material;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
material = Instantiate(Resources.Load<Material>("UI/Material/RoundedCornersTextureMaterial"));
|
|
}
|
|
}
|
|
}
|