protected override void DrawScale() { if (!ShowScale) { return; } RemoveButtons(); // Recycle buttons Vector2 center = GetCenter(); float radius = GetRadius(); float radiusOutter = radius + LabelGap; float radStep = (360 / Data.Items.Length) * Mathf.Deg2Rad; for (int i = 0; i < Data.Items.Length; i++) { float rad = radStep * i; float c = Mathf.Cos(rad); float s = Mathf.Sin(rad); Vector2 p0 = new Vector2(center.x + radiusOutter * s, center.y + radiusOutter * c); // 外边框 顶点0 RadarItemVO vo = Data.Items[i] as RadarItemVO; RectTransform button = CreateButton(vo, LabelWidth, LabelHeight); // Text label1 = button.GetComponentsInChildren<Text>()[0]; TextMeshProUGUI label = button.GetComponentsInChildren <TextMeshProUGUI>()[0]; // label1.text = "评分:" + vo.Score.ToString(); label.text = vo.label; button.localPosition = p0; if (rad == 0) { // label.alignment = TextAnchor.LowerCenter; button.pivot = new Vector2(0.5f, 0); } else if (rad < Mathf.PI / 2 && rad > 0) { // 1st dimention // label.alignment = TextAnchor.MiddleLeft; button.pivot = new Vector2(0, 0); } else if (rad >= Mathf.PI / 2 && rad <= Mathf.PI) { // 2nd dimention // label.alignment = TextAnchor.MiddleLeft; button.pivot = new Vector2(0, 1); } else if (rad > Mathf.PI && rad <= Mathf.PI * 1.5) { // 3rd dimention // label.alignment = TextAnchor.MiddleRight; button.pivot = new Vector2(1, 1); } else { // 4th dimention // label.alignment = TextAnchor.MiddleRight; button.pivot = new Vector2(1, 0); } } }
protected override void AfterDrawItems(float lerp) { if (Data == null || Data.Items == null || Data.Items.Length < 1) { return; } float radStep = (360 / Data.Items.Length) * Mathf.Deg2Rad; float radius = GetRadius(); Vector2 center = GetCenter(); canvas.Stroke(); canvas.strokeStyle.stroke = false; canvas.strokeStyle.strokeColor = Color.white; canvas.strokeStyle.thickness = 1; canvas.strokeStyle.fill = true; canvas.strokeStyle.fillColor = Data.Items[0].color; Vector2 prevPoint = Vector2.zero; Vector2 firstPoint = Vector2.zero; for (int i = 0; i < Data.Items.Length; i++) { RadarItemVO item = Data.Items[i] as RadarItemVO; float percentage = item.value / item.Range; float actualRadius = percentage * radius * lerp; float rad = radStep * i; float c = Mathf.Cos(rad); float s = Mathf.Sin(rad); Vector2 p0 = new Vector2(center.x + actualRadius * s, center.y + actualRadius * c); // 外边框 顶点0 if (i == 0) { // canvas.MoveTo(p0); prevPoint = p0; firstPoint = p0; continue; } // else canvas.LineTo(p0); canvas.MoveTo(p0); canvas.LineTo(center); canvas.LineTo(prevPoint); canvas.LineTo(p0); prevPoint = p0; } canvas.MoveTo(prevPoint); canvas.LineTo(center); canvas.LineTo(firstPoint); canvas.LineTo(prevPoint); canvas.Stroke(); // canvas.LineTo(firstPoint); }
protected override void AfterDrawItems(float lerp) { if (Data == null || Data.Items == null || Data.Items.Length < 1) { return; } float radStep = -(360 / Data.Items.Length) * Mathf.Deg2Rad; float radius = GetRadius(); Vector2 center = GetCenter(); canvas.Stroke(); canvas.strokeStyle.stroke = DrawGraphicBorder; canvas.strokeStyle.strokeColor = RadarBorderColor; canvas.strokeStyle.thickness = BorderThickness; canvas.strokeStyle.fill = FillRadar; canvas.strokeStyle.fillColor = RadarColor; Vector2 firstPoint = Vector2.zero; for (int i = 0; i < Data.Items.Length; i++) { RadarItemVO item = Data.Items[i] as RadarItemVO; float percentage = item.value / item.Range; float actualRadius = percentage * radius * lerp; float rad = radStep * i; float c = Mathf.Cos(rad); float s = Mathf.Sin(rad); Vector2 p0 = new Vector2(center.x + actualRadius * s, center.y + actualRadius * c); // 外边框 顶点0 if (i == 0) { canvas.MoveTo(p0); firstPoint = p0; } else { canvas.LineTo(p0); } } canvas.LineTo(firstPoint); canvas.Stroke(); }
private RectTransform CreateButton(RadarItemVO data, float width, float height) { RectTransform trans; if (m_ButtonsCache.Count > 0) { trans = m_ButtonsCache[0]; m_ButtonsCache.RemoveAt(0); } else { trans = Instantiate(LabelPrefab); } TextMeshProUGUI label = trans.GetComponentInChildren <TextMeshProUGUI>(); label.fontSize = LabelFontSize; label.color = LabelColor; label.text = data.label; trans.gameObject.SetActive(true); trans.transform.SetParent(transform); RectTransform rect = trans.transform as RectTransform; rect.localScale = Vector3.one; rect.localPosition = Vector3.zero; rect.localRotation = Quaternion.identity; rect.sizeDelta = new Vector2(width, height); // Button b1 = trans.GetComponentsInChildren<Button>()[0]; // Button b2 = trans.GetComponentsInChildren<Button>()[1]; // b1.onClick.AddListener(() => { // OnScoreClicked.Invoke(data); // }); // b1.onClick.AddListener(() => { // OnLabelClicked.Invoke(data); // }); m_Buttons.Add(trans); return(trans); }
protected override void DrawScale() { base.DrawScale(); if (!ShowScale) { return; } // RemoveButtons(); // Recycle buttons Vector2 center = GetCenter(); float radius = GetRadius(); float radiusOutter = radius + LabelGap; float radiusLookat = radius * 2; float radStep = (360 / Data.Items.Length) * Mathf.Deg2Rad; for (int i = 0; i < Data.Items.Length; i++) { float rad = radStep * i; float c = Mathf.Cos(rad); float s = Mathf.Sin(rad); Vector2 p0 = new Vector2(center.x + radiusOutter * s, center.y + radiusOutter * c); // 外边框 顶点0 Vector2 p1 = new Vector2(center.x + radiusLookat * s, center.y + radiusLookat * c); RadarItemVO vo = Data.Items[i] as RadarItemVO; Text label = CreateLabel(vo.label); RectTransform button = label.rectTransform; // Text label1 = button.GetComponentsInChildren<Text>()[0]; // Text label = button.GetComponentsInChildren<Text>()[0]; // label1.text = "评分:" + vo.Score.ToString(); label.text = vo.label; button.localPosition = p0; // float degree = Mathf.Rad2Deg * rad - 90; // Quaternion rotation = Quaternion.Euler(-90, 0, 0); button.pivot = Vector2.one * 0.5f; // button.localRotation = rotation; button.LookAt(p1); // if (rad == 0) // { // // label.alignment = TextAnchor.LowerCenter; // button.pivot = new Vector2(0.5f, 0); // } // else if (rad < Mathf.PI/2 && rad > 0) // { // // 1st dimention // // label.alignment = TextAnchor.MiddleLeft; // button.pivot = new Vector2(0, 0); // } // else if (rad >= Mathf.PI/2 && rad <= Mathf.PI) // { // // 2nd dimention // // label.alignment = TextAnchor.MiddleLeft; // button.pivot = new Vector2(0, 1); // } // else if (rad > Mathf.PI && rad <= Mathf.PI*1.5) // { // // 3rd dimention // // label.alignment = TextAnchor.MiddleRight; // button.pivot = new Vector2(1, 1); // } // else // { // // 4th dimention // // label.alignment = TextAnchor.MiddleRight; // button.pivot = new Vector2(1, 0); // } } }