今天无意间在网上发现了一个公式,叫做超椭圆方程,效果就是圆角矩形:
原文地址:
http://blog.youkuaiyun.com/tom_221x/article/details/50924154
原文中是使用shader来实现,这里我使用这个公式来尝试直接读取图片文件生成sprite。
步骤
1.读取texture文件
Texture2D tex = (Texture2D)Resources.Load(filepath) as Texture2D;
2.遍历所有像素点,设置像素的透明度。
先计算出像素点的位置百分比
float wrate = (float)w / (float)tex.width - 0.5f;
float hrate = (float)h / (float)tex.height - 0.5f;
这里减去0.5是为了让图片的中心在(0,0)点的位置
获取当前像素点的颜色
Color source = tex.GetPixel(w, h);
</