自动添加按钮

This commit is contained in:
monitor1394
2018-09-11 07:07:14 +08:00
parent 046f43cc57
commit b6a937d226
2 changed files with 628 additions and 527 deletions

View File

@@ -122,6 +122,8 @@ namespace xchart
public class LineChart : MaskableGraphic public class LineChart : MaskableGraphic
{ {
private const int MAX_GRADUATION = 10;
[SerializeField] [SerializeField]
private int pointWidth = 15; private int pointWidth = 15;
[SerializeField] [SerializeField]
@@ -129,11 +131,13 @@ namespace xchart
[SerializeField] [SerializeField]
private float pointSize = 1.5f; private float pointSize = 1.5f;
[SerializeField] [SerializeField]
private int graduationCount = 4;
[SerializeField]
private int graduationStep = 10; private int graduationStep = 10;
[SerializeField]
private float arrowLen = 12; private float arrowLen = 10;
[SerializeField]
private float arrowSize = 6; private float arrowSize = 6;
[SerializeField] [SerializeField]
private Color backgroundColor; private Color backgroundColor;
[SerializeField] [SerializeField]
@@ -150,6 +154,8 @@ namespace xchart
protected override void Awake() protected override void Awake()
{ {
base.Awake(); base.Awake();
InitGraduation();
InitButton();
for (int i = 0; i < lineList.Count; i++) for (int i = 0; i < lineList.Count; i++)
{ {
LineData line = lineList[i]; LineData line = lineList[i];
@@ -165,41 +171,98 @@ namespace xchart
} }
AddLineToLineMap(line); AddLineToLineMap(line);
} }
InitGraduation();
} }
private void InitGraduation() private void InitGraduation()
{ {
float chartHigh = rectTransform.sizeDelta.y; float chartHigh = rectTransform.sizeDelta.y;
float graduationHig = chartHigh / 4; float graduationHig = chartHigh / graduationCount;
for (int i = 0; i < 5; i++)
for (int i = 0; i < MAX_GRADUATION; i++)
{ {
GameObject g1; if (i >= graduationCount + 1)
if (transform.Find("graduation" + i))
{ {
g1 = transform.Find("graduation" + i).gameObject; if (transform.Find("graduation" + i))
graduationList.Add(g1.GetComponent<Text>()); {
transform.Find("graduation" + i).gameObject.SetActive(false);
}
} }
else else
{ {
g1 = new GameObject(); GameObject g1;
g1.name = "graduation" + i; if (transform.Find("graduation" + i))
g1.transform.parent = transform; {
g1.transform.localPosition = Vector3.zero; g1 = transform.Find("graduation" + i).gameObject;
g1.transform.localScale = Vector3.one; g1.SetActive(true);
g1.AddComponent<Text>(); }
Text txtg1 = g1.GetComponent<Text>(); else
txtg1.font = font; {
txtg1.text = (i * 100).ToString(); g1 = new GameObject();
txtg1.alignment = TextAnchor.MiddleRight; g1.name = "graduation" + i;
g1.transform.parent = transform;
g1.transform.localPosition = Vector3.zero;
g1.transform.localScale = Vector3.one;
Text txtg1 = g1.AddComponent<Text>();
txtg1.font = font;
txtg1.text = (i * 100).ToString();
txtg1.alignment = TextAnchor.MiddleRight;
}
RectTransform rect = g1.GetComponent<RectTransform>(); RectTransform rect = g1.GetComponent<RectTransform>();
rect.anchorMax = Vector2.zero; rect.anchorMax = Vector2.zero;
rect.anchorMin = Vector2.zero; rect.anchorMin = Vector2.zero;
rect.sizeDelta = new Vector2(50, 20); rect.sizeDelta = new Vector2(50, 20);
rect.localPosition = new Vector3(-33, i * graduationHig, 0); rect.localPosition = new Vector3(-33, i * graduationHig, 0);
graduationList.Add(txtg1); graduationList.Add(g1.GetComponent<Text>());
} }
}
}
private void InitButton()
{
float chartHigh = rectTransform.sizeDelta.y;
for (int i = 0; i < lineList.Count; i++)
{
if (lineList[i].button) continue;
GameObject goBtn;
if (transform.Find("button" + i))
{
goBtn = transform.Find("button" + i).gameObject;
lineList[i].button = goBtn.GetComponent<Button>();
}
else
{
goBtn = new GameObject();
goBtn.name = "button" + i;
goBtn.transform.parent = transform;
goBtn.transform.localPosition = Vector3.zero;
goBtn.transform.localScale = Vector3.one;
goBtn.AddComponent<Image>();
goBtn.AddComponent<Button>();
GameObject goTxt = new GameObject();
goTxt.name = "Text";
goTxt.transform.parent = goBtn.transform;
goTxt.transform.localPosition = Vector3.zero;
goTxt.transform.localScale = Vector3.one;
goTxt.AddComponent<Text>();
Text txtg1 = goTxt.GetComponent<Text>();
txtg1.font = font;
txtg1.text = (i * 100).ToString();
txtg1.alignment = TextAnchor.MiddleCenter;
}
RectTransform rect = goBtn.GetComponent<RectTransform>();
if (rect == null)
{
rect = goBtn.AddComponent<RectTransform>();
}
rect.anchorMax = Vector2.zero;
rect.anchorMin = Vector2.zero;
rect.pivot = Vector2.zero;
rect.sizeDelta = new Vector2(50, 20);
rect.localPosition = new Vector3(i*50, chartHigh + 30, 0);
Button btn = goBtn.GetComponent<Button>();
lineList[i].button = btn;
} }
} }
@@ -333,14 +396,12 @@ namespace xchart
} }
} }
float chartHigh = rectTransform.sizeDelta.y; float chartHigh = rectTransform.sizeDelta.y;
Debug.LogError("hig:"+chartHigh);
if (lastHig != chartHigh) if (lastHig != chartHigh)
{ {
lastHig = chartHigh; lastHig = chartHigh;
for (int i = 0; i < graduationList.Count; i++) for (int i = 0; i < graduationList.Count; i++)
{ {
Vector3 pos = graduationList[i].rectTransform.localPosition; Vector3 pos = graduationList[i].rectTransform.localPosition;
print("i:"+pos);
graduationList[i].rectTransform.localPosition = new Vector3(pos.x, lastHig * i / (graduationList.Count-1), pos.z); graduationList[i].rectTransform.localPosition = new Vector3(pos.x, lastHig * i / (graduationList.Count-1), pos.z);
} }
} }

File diff suppressed because it is too large Load Diff