using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class RoundedRectangle : MonoBehaviour
{
    public Color color = new Color(1f, 1f, 1f,1f);
    public int r = 20;
    private bool isInit = false;
    private GameScenUserInfo gameScenUserInfo = null;
    void Start()
    {
        initMember();
    }
    
    public void initMember()
    {
        if(isInit == false)
        {
            isInit = true;
            RectTransform rectTransform = GetComponent<RectTransform>();
            Texture2D texture2D = new Texture2D((int)rectTransform.rect.width, (int)rectTransform.rect.height, TextureFormat.RGBA32, false);
            Image image = GetComponent<Image>();
            if (image == null)
            {
                image = gameObject.AddComponent<Image>();
            }
            
            Color[] allColor = new Color[texture2D.width * texture2D.height];
            for (int x = 0; x < texture2D.width; x++)
            {
                
                for (int y = 0; y < texture2D.height; y++)
                {
                    int offset = y * texture2D.width + x;
                    allColor[offset] = color;
                }
            }
            utilsTools.checkResetYuanJiao(allColor, texture2D.width, texture2D.height, r);
            texture2D.SetPixels(allColor);
            texture2D.Apply();
            image.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
        }
    }
    public void resetSize(Vector2 size)
    {
        isInit = false;
        Image image = GetComponent<Image>();
        Destroy(image.sprite);
        RectTransform rectTransform = GetComponent<RectTransform>();
        rectTransform.sizeDelta = new Vector2(size.x, size.y);
        initMember();
    }
    // Update is called once per frame
    void Update()
    {
        initMember();
    }
}
