unity自定义头像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public class CustomHeadImage : Image
{
   [SerializeField]
   private float showPercent = 1f;
   [SerializeField]
   private int segments = 100;
   protected override void OnPopulateMesh(VertexHelper vh)
   {
        vh.Clear();
        
       float width = rectTransform.rect.width;
       float height = rectTransform.rect.height;
       Vector2 uv = GetUV(overrideSprite);
       Vector2 convertRatio = GetConvetRatio(uv.x, uv.y, width, height);
       Vector2 uvCenter = GetUvCenter(uv.x, uv.y);
       Vector2 originPos = GetOriginPos(width, height);
       UIVertex origin = new UIVertex();
       //设置原点颜色渐变
       byte c = (byte)(showPercent * 255);
        origin.color = new Color32(c, c, c, 255);
        origin.position = originPos;
        origin.uv0 = new Vector2(Vector2.zero.x * convertRatio.x + uvCenter.x, Vector2.zero.y * convertRatio.y + uvCenter.y);
        vh.AddVert(origin);
 
 
       float radian = Mathf.PI * 2 / segments;
       float curRadian = 0;
       float radius = width * 0.5f;
       int realSegment = (int)(showPercent * segments);
       for (int i = 0; i < segments + 1; i++)
       {
           float x = Mathf.Cos(curRadian) * radius;
           float y = Mathf.Sin(curRadian) * radius;
            curRadian += radian;
           Vector2 xy = new Vector2(x, y);
           UIVertex uvTemp = new UIVertex();
           if (i < realSegment)
           {
                uvTemp.color = color;
           }
           else
           {
               if (realSegment == segments)
               {
                    uvTemp.color = color;
               }
               else
               {
                    uvTemp.color = new Color32(60, 60, 60, 255);
               }
           }
            uvTemp.position = xy + originPos;
            uvTemp.uv0 = new Vector2(xy.x * convertRatio.x + uvCenter.x, xy.y * convertRatio.y + uvCenter.y);
            vh.AddVert(uvTemp);
       }
 
 
       int id = 1;
       for (int i = 0; i < segments; i++)
       {
            vh.AddTriangle(id, 0, id + 1);
            id++;
       }
 
 
   }
   private Vector2 GetConvetRatio(float uvWidth, float uvHeight, float width, float height)
   {
 
 
       Vector2 convertRatio = new Vector2(uvWidth / width, uvHeight / height);
       return convertRatio;
   }
 
 
   private Vector2 GetUvCenter(float uvWidth, float uvHeight)
   {
       Vector2 center = new Vector2(uvWidth * 0.5f, uvHeight * 0.5f);
       return center;
   }
 
 
   private Vector2 GetOriginPos(float width, float height)
   {
       Vector2 originPos = new Vector2((0.5f - rectTransform.pivot.x) * width, ((0.5f - rectTransform.pivot.y) * height));
       return originPos;
   }
   private Vector2 GetUV(Sprite sprite)
   {
       Vector4 temp = sprite != null ? DataUtility.GetOuterUV(sprite) : Vector4.zero;
       float uvHeight = temp.w - temp.y;
       float uvWidth = temp.z - temp.x;
       Vector2 uv = new Vector2(uvWidth, uvHeight);
       return uv;
   }
}

  

posted @   游戏鼻祖  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2017-10-23 unity 同一标签里内容拥有不同颜色
点击右上角即可分享
微信分享提示