基础内容
必要插件安装 缓动曲线和动画基础 ElementFolder,Track与其次级模块,PathNode重构
This commit is contained in:
195
Assets/Modern UI Pack/Scripts/Charts/PieChart.cs
Normal file
195
Assets/Modern UI Pack/Scripts/Charts/PieChart.cs
Normal file
@@ -0,0 +1,195 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
public class PieChart : MaskableGraphic
|
||||
{
|
||||
// Chart Items
|
||||
[SerializeField] public List<PieChartDataNode> chartData = new List<PieChartDataNode>();
|
||||
|
||||
// Settings
|
||||
[Range(-75, 150)] public float borderThickness = 5;
|
||||
[SerializeField] private Color borderColor = new Color32(255, 255, 255, 255);
|
||||
public Transform indicatorParent;
|
||||
public string valuePrefix = "(";
|
||||
public string valueSuffix = ")";
|
||||
public bool addValueToIndicator = true;
|
||||
public bool enableBorderColor;
|
||||
|
||||
private float fillAmount = 1f;
|
||||
private int segments = 720;
|
||||
|
||||
[System.Serializable]
|
||||
public class PieChartDataNode
|
||||
{
|
||||
public string name = "Chart Item";
|
||||
public float value = 10;
|
||||
public Color32 color = new Color32(255, 255, 255, 255);
|
||||
public Image indicatorImage;
|
||||
public TextMeshProUGUI indicatorText;
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
UpdateIndicators();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
this.borderThickness = (float)Mathf.Clamp(this.borderThickness, -75, rectTransform.rect.width / 3.333f);
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
if (chartData.Count == 0)
|
||||
return;
|
||||
|
||||
float outer = -rectTransform.pivot.x * rectTransform.rect.width;
|
||||
float inner = -rectTransform.pivot.x * rectTransform.rect.width + this.borderThickness;
|
||||
|
||||
var outer1 = -rectTransform.pivot.x * rectTransform.rect.width * 0.6f;
|
||||
var inner1 = -rectTransform.pivot.x * rectTransform.rect.width * 0.6f + this.borderThickness;
|
||||
|
||||
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 f = fillAmount;
|
||||
float degrees = 360f / segments;
|
||||
int fa = (int)((segments + 1) * f);
|
||||
|
||||
var dataIndex = 0;
|
||||
var total = 0f;
|
||||
var currentValue = chartData[0].value;
|
||||
chartData.ForEach(s => total += s.value);
|
||||
var fillColor = chartData[0].color;
|
||||
|
||||
for (int i = 0; i < fa; i++)
|
||||
{
|
||||
float rad = Mathf.Deg2Rad * (i * degrees);
|
||||
float c = Mathf.Cos(rad);
|
||||
float s = Mathf.Sin(rad);
|
||||
|
||||
uv0 = new Vector2(0, 1);
|
||||
uv1 = new Vector2(1, 1);
|
||||
uv2 = new Vector2(1, 0);
|
||||
uv3 = new Vector2(0, 0);
|
||||
|
||||
pos0 = prevX;
|
||||
pos1 = new Vector2(outer * c, outer * s);
|
||||
pos2 = new Vector2(inner * c, inner * s);
|
||||
pos3 = prevY;
|
||||
|
||||
if (i > currentValue / total * segments)
|
||||
{
|
||||
if (dataIndex < chartData.Count - 1)
|
||||
{
|
||||
dataIndex += 1;
|
||||
currentValue += chartData[dataIndex].value;
|
||||
fillColor = chartData[dataIndex].color;
|
||||
}
|
||||
}
|
||||
|
||||
vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2 * inner1 / inner, pos3 * inner1 / inner }, new[] { uv0, uv1, uv2, uv3 }, fillColor));
|
||||
|
||||
if (enableBorderColor == true)
|
||||
{
|
||||
vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 }, borderColor));
|
||||
vh.AddUIVertexQuad(SetVbo(new[] { pos0 * outer1 / outer, pos1 * outer1 / outer, pos2 * inner1 / inner, pos3 * inner1 / inner }, new[] { uv0, uv1, uv2, uv3 }, borderColor));
|
||||
}
|
||||
|
||||
prevX = pos1;
|
||||
prevY = pos2;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetData(List<PieChartDataNode> data)
|
||||
{
|
||||
chartData = data;
|
||||
SetVerticesDirty();
|
||||
}
|
||||
|
||||
protected UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs, Color32 color)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public void UpdateIndicators()
|
||||
{
|
||||
for (int i = 0; i < chartData.Count; ++i)
|
||||
{
|
||||
if (chartData[i].indicatorImage != null)
|
||||
chartData[i].indicatorImage.color = chartData[i].color;
|
||||
|
||||
if (chartData[i].indicatorText != null && addValueToIndicator == true)
|
||||
chartData[i].indicatorText.text = chartData[i].name + valuePrefix + chartData[i].value.ToString() + valueSuffix;
|
||||
else if (chartData[i].indicatorText != null && addValueToIndicator == false)
|
||||
chartData[i].indicatorText.text = chartData[i].name;
|
||||
}
|
||||
|
||||
if (indicatorParent != null)
|
||||
StartCoroutine("UpdateIndicatorLayout");
|
||||
}
|
||||
|
||||
public void ChangeValue(int itemIndex, float itemValue)
|
||||
{
|
||||
chartData[itemIndex].value = itemValue;
|
||||
|
||||
this.enabled = false;
|
||||
this.enabled = true;
|
||||
}
|
||||
|
||||
public void AddNewItem()
|
||||
{
|
||||
PieChartDataNode item = new PieChartDataNode();
|
||||
|
||||
if (indicatorParent.childCount != 0)
|
||||
{
|
||||
int tempIndex = indicatorParent.childCount - 1;
|
||||
|
||||
GameObject tempIndicator = indicatorParent.GetChild(tempIndex).gameObject;
|
||||
GameObject newIndicator = Instantiate(tempIndicator, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
|
||||
|
||||
newIndicator.transform.SetParent(indicatorParent, false);
|
||||
newIndicator.gameObject.name = "Item " + tempIndex.ToString() + " Indicator";
|
||||
|
||||
item.indicatorImage = newIndicator.GetComponentInChildren<Image>();
|
||||
item.indicatorText = newIndicator.GetComponentInChildren<TextMeshProUGUI>();
|
||||
item.name = "Chart Item " + tempIndex.ToString();
|
||||
}
|
||||
|
||||
chartData.Add(item);
|
||||
}
|
||||
|
||||
IEnumerator UpdateIndicatorLayout()
|
||||
{
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(indicatorParent.GetComponentInParent<RectTransform>());
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/Modern UI Pack/Scripts/Charts/PieChart.cs.meta
Normal file
18
Assets/Modern UI Pack/Scripts/Charts/PieChart.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5e4a4384d3c229846b0d84dc8ec948dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 3ae7671fb73a5df4ebec7a6b32ae8e8c, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Charts/PieChart.cs
|
||||
uploadId: 628721
|
||||
120
Assets/Modern UI Pack/Scripts/Charts/PieChartEditor.cs
Normal file
120
Assets/Modern UI Pack/Scripts/Charts/PieChartEditor.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[CustomEditor(typeof(PieChart))]
|
||||
public class PieChartEditor : Editor
|
||||
{
|
||||
private GUISkin customSkin;
|
||||
private PieChart pieTarget;
|
||||
private UIManagerPieChart tempUIM;
|
||||
private int currentTab;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
pieTarget = (PieChart)target;
|
||||
|
||||
try { tempUIM = pieTarget.GetComponent<UIManagerPieChart>(); }
|
||||
catch { }
|
||||
|
||||
if (EditorGUIUtility.isProSkin == true) { customSkin = MUIPEditorHandler.GetDarkEditor(customSkin); }
|
||||
else { customSkin = MUIPEditorHandler.GetLightEditor(customSkin); }
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
MUIPEditorHandler.DrawComponentHeader(customSkin, "PC Top Header");
|
||||
|
||||
GUIContent[] toolbarTabs = new GUIContent[2];
|
||||
toolbarTabs[0] = new GUIContent("Content");
|
||||
toolbarTabs[1] = new GUIContent("Settings");
|
||||
|
||||
currentTab = MUIPEditorHandler.DrawTabs(currentTab, toolbarTabs, customSkin);
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Content", "Content"), customSkin.FindStyle("Tab Content")))
|
||||
currentTab = 0;
|
||||
if (GUILayout.Button(new GUIContent("Settings", "Settings"), customSkin.FindStyle("Tab Settings")))
|
||||
currentTab = 1;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
var chartData = serializedObject.FindProperty("chartData");
|
||||
var borderThickness = serializedObject.FindProperty("borderThickness");
|
||||
var borderColor = serializedObject.FindProperty("borderColor");
|
||||
var enableBorderColor = serializedObject.FindProperty("enableBorderColor");
|
||||
var addValueToIndicator = serializedObject.FindProperty("addValueToIndicator");
|
||||
var indicatorParent = serializedObject.FindProperty("indicatorParent");
|
||||
var valuePrefix = serializedObject.FindProperty("valuePrefix");
|
||||
var valueSuffix = serializedObject.FindProperty("valueSuffix");
|
||||
|
||||
switch (currentTab)
|
||||
{
|
||||
case 0:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Content Header", 6);
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
EditorGUI.indentLevel = 1;
|
||||
|
||||
EditorGUILayout.PropertyField(chartData, new GUIContent("Chart Items"));
|
||||
chartData.isExpanded = true;
|
||||
|
||||
if (GUILayout.Button("+ Add a new item", customSkin.button))
|
||||
pieTarget.AddNewItem();
|
||||
|
||||
EditorGUI.indentLevel = 0;
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
if (pieTarget.gameObject.activeInHierarchy == true)
|
||||
pieTarget.UpdateIndicators();
|
||||
|
||||
break;
|
||||
|
||||
case 1:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Customization Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(indicatorParent, customSkin, "Indicator Parent");
|
||||
MUIPEditorHandler.DrawProperty(borderThickness, customSkin, "Border Thickness");
|
||||
addValueToIndicator.boolValue = MUIPEditorHandler.DrawToggle(addValueToIndicator.boolValue, customSkin, "Add Value To Indicator");
|
||||
|
||||
if (addValueToIndicator.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawPropertyCW(valuePrefix, customSkin, "Value Prefix:", 75);
|
||||
MUIPEditorHandler.DrawPropertyCW(valueSuffix, customSkin, "Value Suffix:", 75);
|
||||
}
|
||||
|
||||
enableBorderColor.boolValue = MUIPEditorHandler.DrawToggle(enableBorderColor.boolValue, customSkin, "Enable Border Color (Experimental)");
|
||||
|
||||
if (enableBorderColor.boolValue == true)
|
||||
MUIPEditorHandler.DrawProperty(borderColor, customSkin, "Border Color");
|
||||
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "UIM Header", 10);
|
||||
|
||||
if (tempUIM != null)
|
||||
{
|
||||
MUIPEditorHandler.DrawUIManagerConnectedHeader();
|
||||
|
||||
if (GUILayout.Button("Open UI Manager", customSkin.button))
|
||||
EditorApplication.ExecuteMenuItem("Tools/Modern UI Pack/Show UI Manager");
|
||||
|
||||
if (GUILayout.Button("Disable UI Manager Connection", customSkin.button))
|
||||
{
|
||||
if (EditorUtility.DisplayDialog("Modern UI Pack", "Are you sure you want to disable UI Manager connection with the object? " +
|
||||
"This operation cannot be undone.", "Yes", "Cancel"))
|
||||
{
|
||||
try { DestroyImmediate(tempUIM); }
|
||||
catch { Debug.LogError("<b>[Pie Chart]</b> Failed to delete UI Manager connection.", this); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (tempUIM == null) { MUIPEditorHandler.DrawUIManagerDisconnectedHeader(); }
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (Application.isPlaying == false) { this.Repaint(); }
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
18
Assets/Modern UI Pack/Scripts/Charts/PieChartEditor.cs.meta
Normal file
18
Assets/Modern UI Pack/Scripts/Charts/PieChartEditor.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e02c02515eb09c04caeb0ab2d66d83da
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Charts/PieChartEditor.cs
|
||||
uploadId: 628721
|
||||
Reference in New Issue
Block a user