UGUI之RawImage显示圆形或多边形贴图

  1. 创建canvas,挂载UICircle 脚本
  2. 添加RenderTexture
  3. 修改UICircle多边形数(36圆形)

脚本1:UICircle .cs

using System;

using UnityEngine;

using UnityEngine.UI;

 

public class UICircle : RawImage

{

const int FILL_PERCENT = 100;

float thickness = 5;

 

[SerializeField][Range(4,360)]

int _segments = 36;

 

public int segments {

get { return _segments;}

set {

if (_segments != value) {

_segments = value;

SetVerticesDirty ();

#if UNITY_EDITOR

UnityEditor.EditorUtility.SetDirty (transform);

#endif

}

}

}

 

 

protected override void OnRectTransformDimensionsChange ()

{

base.OnRectTransformDimensionsChange ();

this.thickness = (float)Mathf.Clamp (this.thickness, 0, rectTransform.rect.width / 2);

}

    

protected override void OnPopulateMesh(VertexHelper vh)

    {

        float outer = -rectTransform.pivot.x * rectTransform.rect.width;

        float inner = -rectTransform.pivot.x * rectTransform.rect.width + this.thickness;

 

        vh.Clear();

 

        Vector2 prevX = Vector2.zero;

        Vector2 prevY = Vector2.zero;

        Vector2 uv0 = new Vector2(0, 0);

        Vector2 uv1 = new Vector2(0, 1);

        Vector2 uv2 = new Vector2(1, 1);

        Vector2 uv3 = new Vector2(1, 0);

        Vector2 pos0;

        Vector2 pos1;

        Vector2 pos2;

        Vector2 pos3;

 

        float tw = rectTransform.rect.width;

        float th = rectTransform.rect.height;

 

        float angleByStep = (FILL_PERCENT / 100f * (Mathf.PI * 2f)) / segments;

        float currentAngle = 0f;

        for (int i = 0; i < segments + 1; i++)

        {

 

            float c = Mathf.Cos(currentAngle);

            float s = Mathf.Sin(currentAngle);

 

            StepThroughPointsAndFill(outer, inner, ref prevX, ref prevY, out pos0, out pos1, out pos2, out pos3, c, s);

 

            uv0 = new Vector2(pos0.x / tw + 0.5f, pos0.y / th + 0.5f);

            uv1 = new Vector2(pos1.x / tw + 0.5f, pos1.y / th + 0.5f);

            uv2 = new Vector2(pos2.x / tw + 0.5f, pos2.y / th + 0.5f);

            uv3 = new Vector2(pos3.x / tw + 0.5f, pos3.y / th + 0.5f);

 

            vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 }));

 

            currentAngle += angleByStep;

        }

    }

 

    private void StepThroughPointsAndFill(float outer, float inner, ref Vector2 prevX, ref Vector2 prevY, out Vector2 pos0, out Vector2 pos1, out Vector2 pos2, out Vector2 pos3, float c, float s)

    {

        pos0 = prevX;

        pos1 = new Vector2(outer * c, outer * s);

 

        pos2 = Vector2.zero;

        pos3 = Vector2.zero;

 

        prevX = pos1;

        prevY = pos2;

    }

 

protected UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs)

{

UIVertex[] vbo = new UIVertex[4];

for (int i = 0; i < vertices.Length; i++)

{

var vert = UIVertex.simpleVert;

vert.color = color;

vert.position = vertices[i];

vert.uv0 = uvs[i];

vbo[i] = vert;

}

return vbo;

}

}

 

脚本2:UICircleInspector.cs  放在Editor文件夹下

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEditor;

using UnityEditor.UI;

 

[CustomEditor(typeof(UICircle), true)]

[CanEditMultipleObjects]

public class UICircleInspector : RawImageEditor {

 

public override void OnInspectorGUI()

{

base.OnInspectorGUI ();

UICircle circle = target as UICircle;

circle.segments = Mathf.Clamp(EditorGUILayout.IntField ("UICircle多边形", circle.segments),4,360);

 

}

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值