首页 > 其他分享 >unity自定义头像

unity自定义头像

时间:2024-10-23 13:48:16浏览次数:1  
标签:自定义 width uvTemp Vector2 float height 头像 unity new

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;
   }
}

  

标签:自定义,width,uvTemp,Vector2,float,height,头像,unity,new
From: https://www.cnblogs.com/lichuangblog/p/18496207

相关文章

  • unity贴花实现
    Shader"Decal"{Properties{_MainTex("Texture",2D)="white"{} _MixedColor("MixedColor",Color)=(1,1,1,1)//颜色混合 _Alpha("Alpha",Range(0,1.0))=1//透明度}SubShader{......
  • Unity Shader-GodRay,体积光(BillBoard,Volume Shadow,Raidal Blur,Ray-Marching)
    前言好久没有更新博客了,经历了不少事情,好在最近回归了一点正轨,决定继续UnityShader的学习之路。作为回归的第一篇,来玩一个比较酷炫的效果(当然废话也比较多),一般称之为GodRay(圣光),也有人叫它云隙光,还有人叫它体积光(探照灯)。这几个名字对应几种类似的效果,但是实现方式相差甚远。先来......
  • 自定义组件-behaviors
      ......
  • uniapp微信小程序开发自定义相机
    自定义相机,调用手机原生相机,自定义拍照或录制键。原本以为是一个很复杂或者不能实现的功能,实际使用就是使用小程序的媒体组件<camera></camera>就可以了。<viewclass=""><cameraclass="camera"id="myCamera":device-position="devicePosition"@error="onCam......
  • Unity CharacterController基本属性
    CharacterController优点:已有的碰撞体最小移动距离限制:通常设置为0,如果角色试图移动到指示值以下,根本移动不了,目的是为了消除潜在的不需要的抖动皮肤宽度限制:避免卡在其他碰撞体中,在碰撞体进行碰撞检测拦截之前,皮肤宽度已经在这的基础上,提前做了一次碰撞拦截,但它会更柔和,允许......
  • 自定义组件-插槽
      ......
  • 【unity】 Loom实现多线程
    ​通常情况下,unity中在子线程中改变变量的值,但是子线程尚未结束时,主线程无法使用该变量。因此使用Loom作为中介,子线程调用并传值给Loom,Loom调用主线程的API。实现步骤创建Loom空物体,并挂载Loom脚本//Loom.csusingSystem;usingSystem.Collections;usingSystem.Collectio......
  • TriLib2:Unity运行时运行时加载模型
    概述本文基于此版本TriLib2是Unity的一款强大的3D模型运行时导入工具包,可以在游戏运行期间或编辑器中动态加载多种格式的模型,笔者也只是简单使用了从文件路径加载模型一种用法,旨在帮助你快速入门.它支持的格式包括FBX、OBJ、GLTF2、STL、PLY、3MF、DAE和ZIP等,......
  • ArcGIS002:软件自定义设置
    摘要:本文详细介绍安装arcgis10.2后软件自定义设置内容,包括工具条的启用、扩展模块的启用、如何加载项管理器、快捷键设置、样式管理器的使用以及软件常规设置。一、工具条的启用依次点击菜单栏【自定义】->【工具条】,根据工作需求勾选上常用的工具条。勾选后相应工具条自动......
  • 在 Avalonia 程序中添加自定义字体,并优化字体显示效果
    这里以MiSans为例,因为尝试了下阿里巴巴普惠体失败了。。。其它字体还未尝试1、添加字体需要将所需字体文件添加到项目中,并将其“生成操作”设置为“AvaloniaResource”,如图:2、在App.axaml中添加字体引用<Applicationxmlns="https://github.com/avaloniaui"......