Unity实现属性图

属性图脚本的UI脚本信息

效果图片
Unity实现属性图_第1张图片
脚本信息

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

namespace Framework.UI.Radar
{
    public class RadarProperty : Graphic
    {
        /// 
        /// 图像边的数量
        /// 
        public uint m_Sum = 6;
        /// 
        /// 是否显示背景
        /// 
        public bool m_IsBG { set; get; }
        /// 
        /// 图片的大小
        /// 
        public Vector2 m_Sizes { get; set; }
        /// 
        /// 线与属性的显示关系
        /// 
        public PropertyAndLine TypeProperty = PropertyAndLine.PropertyOnTop;

        /// 
        /// 是否显示属性信息
        /// 
        public bool m_IsProperty = false;
        /// 
        /// 属性图的颜色
        /// 
        public Color32 m_PropertyColor = Color.blue;

        public float m_MaxValues = 10;
        /// 
        /// 属性显示偏移大小
        /// 
        public float m_OffectValues = 0f;
        public List<float> m_CurrValue = new List<float>();
        /// 
        /// 是否描线
        /// 
        public bool m_IsDrawLine { set; get; }
        /// 
        /// 线的颜色
        /// 
        public Color32 m_LineColor = Color.black;
        /// 
        /// 线的宽度
        /// 
        public float m_LineSize { set; get; }
        /// 
        /// 线的数量
        /// 
        public uint m_LineSum { set; get; }


        protected override void OnPopulateMesh(VertexHelper vh)
        {
            if (!IsActive())
            {
                return;
            }
            if (m_Sum < 3)
            {
                m_Sum = 3;
            }
            vh.Clear();
            var vertexList = new List<UIVertex>();
            ApplyGradient(ref vertexList);
            vh.AddUIVertexTriangleStream(vertexList);
        }
        /// 
        /// 开始进行顶点描绘
        /// 
        /// 所有顶点信息
        private void ApplyGradient(ref List<UIVertex> vertices)
        {
            if (m_IsBG)
            {
                BGInfo(ref vertices);
            }
            if (TypeProperty == PropertyAndLine.PropertyOnTop)
            {
                if (m_IsDrawLine)
                {
                    LineInfo(ref vertices);
                }
                if (m_IsProperty)
                {
                    PropertyInfo(ref vertices);
                }
            }
            else if(TypeProperty == PropertyAndLine.LineOnTop)
            {
                if (m_IsProperty)
                {
                    PropertyInfo(ref vertices);
                }
                if (m_IsDrawLine)
                {
                    LineInfo(ref vertices);
                }
            }
        }
        /// 
        /// 获取满属性的顶点位置信息
        /// 
        /// 边的数量
        /// 圆的半径
        private List<Vector3> GetDrawUIVertex(uint count, Vector2 r)
        {
            List<Vector3> tempvector = new List<Vector3>();

            for (int i = 0; i < count; i++)
            {
                float temp_x = Mathf.Sin((360f * i / count) * Mathf.Deg2Rad) * r.x;//横坐标
                float temp_y = Mathf.Cos((360f * i / count) * Mathf.Deg2Rad) * r.y;//纵坐标
                tempvector.Add(new Vector3(temp_x, temp_y, 0));
            }
            return tempvector;
        }
        /// 
        /// 进行背景图的描绘
        /// 
        /// 
        private void BGInfo(ref List<UIVertex> vertices)
        {
            List<Vector3> tempvector = GetDrawUIVertex(m_Sum, m_Sizes);
            //bg
            for (int i = 0; i < m_Sum; i++)
            {
                for (int j = 0; j < 3; j++)
                {

                    UIVertex tempver = new UIVertex();
                    tempver.color = color;
                    if (j == 0)
                    {
                        tempver.position = tempvector[i];
                    }
                    else if (j == 1)
                    {
                        tempver.position = Vector3.zero;
                    }
                    else if (j == 2)
                    {
                        int index = (int)((i + 1) % m_Sum);
                        tempver.position = tempvector[index];
                    }
                    vertices.Add(tempver);
                }
            }
        }
        /// 
        /// 进行属性图的描绘
        /// 
        /// 
        /// 
        private void PropertyInfo(ref List<UIVertex> vertices)
        {
            List<Vector3> tempvector = GetDrawUIVertex(m_Sum, m_Sizes);
            //Property
            for (int i = 0; i < m_Sum; i++)
            {
                for (int j = 0; j < 3; j++)
                {

                    UIVertex tempver = new UIVertex();
                    tempver.color = m_PropertyColor;
                    if (j == 0)
                    {
                        tempver.position = tempvector[i] * (m_CurrValue[i] / m_MaxValues );
                    }
                    else if (j == 1)
                    {
                        tempver.position = Vector3.zero;
                    }
                    else if (j == 2)
                    {
                        int index = (int)((i + 1) % m_Sum);
                        tempver.position = tempvector[index] * (m_CurrValue[index] ) / (m_MaxValues ) ;
                    }
                    vertices.Add(tempver);
                }
            }
        }
        /// 
        /// 进行画线
        /// 
        /// 所有的顶点信息
        private void LineInfo(ref List<UIVertex> vertices)
        {
            Vector2 Every = new Vector2(m_Sizes.x / m_LineSum, m_Sizes.y / m_LineSum);
            if (m_LineSum <= 1)
            {
                DrawLine(ref vertices, m_Sizes);
            }
            else
            {
                for (int i = 1; i <= m_LineSum; i++)
                {
                    DrawLine(ref vertices, Every * i);
                }
            }
        }
        /// 
        /// 进行每一圈的单独的线画线
        /// 
        private void DrawLine(ref List<UIVertex> vertices,Vector2 varsize)
        {
            Vector2 temp_linesize = new Vector2(varsize.x - m_LineSize, varsize.y - m_LineSize);
            //线的外圈顶点
            List<Vector3> tempOtherVector = GetDrawUIVertex(m_Sum, varsize);
            //线的内圈顶点
            List<Vector3> tempInnerVector = GetDrawUIVertex(m_Sum, temp_linesize);

            for (int i = 0; i < m_Sum; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    UIVertex tempver = new UIVertex();
                    tempver.color = m_LineColor;
                    if (j == 0)
                    {
                        tempver.position = tempOtherVector[i];
                    }
                    else if (j == 1)
                    {
                        tempver.position = tempInnerVector[i];
                    }
                    else if (j == 2)
                    {
                        int index = (int)((i + 1) % m_Sum);
                        tempver.position = tempOtherVector[index];
                    }
                    else if (j == 3)
                    {
                        tempver.position = tempInnerVector[i];
                    }
                    else if (j == 4)
                    {
                        int index = (int)((i + 1) % m_Sum);
                        tempver.position = tempOtherVector[index];
                    }
                    else if (j == 5)
                    {
                        int index = (int)((i + 1) % m_Sum);
                        tempver.position = tempInnerVector[index];
                    }
                    vertices.Add(tempver);
                }
            }
        }
        /// 
        /// 线与属性的位置关系
        /// 
        public enum PropertyAndLine
        {
            /// 
            /// 线在上面
            /// 
            LineOnTop,
            /// 
            /// 属性在上面
            /// 
            PropertyOnTop,
        }
    }
}

属性图的Editor脚本

配合UI脚本使用,使Inspector界面更加简洁

using UnityEngine;
using System.Collections;
using UnityEditor;
using Framework.UI.Radar;
using System.Collections.Generic;

namespace Framework.Editor.UI
{
    [CustomEditor(typeof(RadarProperty))]
    public class RadarPropertyEditror : UnityEditor.Editor
    {
        private RadarProperty m_Class;


        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            m_Class = (RadarProperty)target;
            
            int index = EditorGUILayout.IntField("Sum", (int)m_Class.m_Sum);
            if (index <= 3)
            {
                m_Class.m_Sum = 3;
            }
            else
            {
                m_Class.m_Sum = (uint)index;
            }
            RectTransform rect = m_Class.GetComponent<RectTransform>();
            if (rect == null)
            {
                m_Class.m_Sizes = EditorGUILayout.Vector2Field("Size", m_Class.m_Sizes);
            }
            else
            {
                m_Class.m_Sizes = new Vector2(rect.sizeDelta.x, rect.sizeDelta.y);
                m_Class.m_Sizes /= 2.0f;
            }
            //背景信息
            m_Class.m_IsBG = EditorGUILayout.Toggle("BG", m_Class.m_IsBG);
            if (m_Class.m_IsBG)
            {
                BGInfo();
            }

            m_Class.TypeProperty = (RadarProperty.PropertyAndLine)EditorGUILayout.EnumPopup("TypeProperty", m_Class.TypeProperty);

            //属性信息
            m_Class.m_IsProperty = EditorGUILayout.Toggle("Property", m_Class.m_IsProperty);
            if (m_Class.m_IsProperty)
            {
                PropertyGUI();
            }
            //画线信息
            m_Class.m_IsDrawLine = EditorGUILayout.Toggle("Draw Line", m_Class.m_IsDrawLine);
            if (m_Class.m_IsDrawLine)
            {
                LineGUI();
            }

            //射线检测
            m_Class.raycastTarget = EditorGUILayout.Toggle("Raycast Target", m_Class.raycastTarget);

            m_Class.OnRebuildRequested();

            serializedObject.ApplyModifiedProperties();
        }

        private void BGInfo()
        {
            m_Class.color = EditorGUILayout.ColorField("BG Color", m_Class.color);
            m_Class.material = (Material)EditorGUILayout.ObjectField("Material", m_Class.material, typeof(Material), false);
        }

        private void PropertyGUI()
        {
            m_Class.m_PropertyColor = EditorGUILayout.ColorField("Property Color", m_Class.m_PropertyColor);
            m_Class.m_MaxValues = EditorGUILayout.FloatField("Max Property", m_Class.m_MaxValues);
            
            //当前值
            if (m_Class.m_CurrValue.Count < m_Class.m_Sum)
            {
                int tempindex = (int)m_Class.m_Sum - m_Class.m_CurrValue.Count;
                float value = 0;
                if (m_Class.m_CurrValue.Count != 0)
                {
                    value = m_Class.m_CurrValue[m_Class.m_CurrValue.Count - 1];
                }
                for (int i = 0; i < tempindex; i++)
                {
                    m_Class.m_CurrValue.Add(value);
                }
            }
            else if (m_Class.m_CurrValue.Count > m_Class.m_Sum)
            {
                int tempindex = m_Class.m_CurrValue.Count - (int)m_Class.m_Sum;
                m_Class.m_CurrValue.RemoveRange((int)m_Class.m_Sum, tempindex);
            }

            for (int i = 0; i < m_Class.m_CurrValue.Count; i++)
            {
                float tempvector = EditorGUILayout.FloatField("Property " + i.ToString(), m_Class.m_CurrValue[i]);
                if (tempvector <= 0)
                {
                    tempvector = 0;
                }
                else if (tempvector >= m_Class.m_MaxValues)
                {
                    tempvector =  m_Class.m_MaxValues;
                }
                m_Class.m_CurrValue[i] = tempvector;
            }
        }
        private void LineGUI()
        {
            m_Class.m_LineColor = EditorGUILayout.ColorField("Line Color", m_Class.m_LineColor);
            m_Class.m_LineSize = EditorGUILayout.FloatField("Line Size", m_Class.m_LineSize);
            if (m_Class.m_LineSize <= 0)
            {
                m_Class.m_LineSize = 0;
            }
            m_Class.m_LineSum = (uint)EditorGUILayout.IntField("Line Sum", (int)m_Class.m_LineSum);
            if (m_Class.m_LineSum <= 1)
            {
                m_Class.m_LineSum = 1;
            }
        }
    }
}

你可能感兴趣的:(Unity原生API)