You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.5 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace BuildSystem {
public class SetPanel : MonoBehaviour
{
GridUI grid;
PlaceObject_SO placeObject;
public Image img;
public Transform road;
public Transform[] dir;
public GameObject cansetPre;
public GameObject notsetPre;
public GameObject roadPre;
public Button close;
public Button sure;
private void Awake()
{
close.onClick.AddListener(() => {
gameObject.SetActive(false);
});
sure.onClick.AddListener(SetData);
}
public void Init(GridUI grid,PlaceObject_SO placeObject_SO,Sprite sprite)
{
this.grid = grid;
placeObject = placeObject_SO;
this.img.sprite = sprite;
CreatePanel();
}
void CreatePanel() {
//
int i = 1;
foreach (object[] r in placeObject.roadGoInfo) {
//判断该车道是否存在
bool flag = false;
foreach (object o in r)
{
if (o != null) {
flag = true;
break;
}
}
//对存在车道的道路进行ui创建
if (flag) {
GameObject text = Instantiate(roadPre, road);
text.GetComponent<Text>().text = i.ToString();
for (int l=0;l<r.Length;l++) {
if (l == 0) {
bool isCanStraight = true;
if ((r[0] == null || (bool)r[0] == false) && (r[2] == null || (bool)r[2] == false)) {
isCanStraight = false;
}
GameObject toggle = Instantiate(cansetPre, dir[0]);
toggle.GetComponent<Toggle>().isOn = isCanStraight;
}
//奇数 左右
if (l % 2 != 0) {
if (r[l] == null)
{
Instantiate(notsetPre, dir[l]);
}
else
{
GameObject toggle = Instantiate(cansetPre, dir[l]);
toggle.GetComponent<Toggle>().isOn = (bool)r[l];
}
}
}
i++;
}
}
}
void SetData()
{
int i = 0;
foreach (object[] r in placeObject.roadGoInfo)
{
//判断车道是否存在
bool flag = false;
foreach (object o in r)
{
if (o != null)
{
flag = true;
break;
}
}
if (flag)
{
for (int d = 0;d < 4;d++) {
//对左右进行数据写入
if (d % 2 != 0)
{
if (dir[d].GetChild(i).GetComponent<Toggle>())
{
r[d] = dir[d].GetChild(i).GetComponent<Toggle>().isOn;
}
else
{
r[d] = null;
}
}
//对上下数据进行写入
else {
if (r[d] != null) {
r[d] = dir[0].GetChild(i).GetComponent<Toggle>().isOn;
}
}
}
i++;
}
}
grid.SetData(placeObject.roadGoInfo);
}
private void OnDisable()
{
//清空之前设置的显示
for (int t = 0; t < road.childCount; t++)
{
Destroy(road.GetChild(t).gameObject);
}
foreach (Transform trans in dir)
{
for (int t = 0; t < trans.childCount; t++)
{
Destroy(trans.GetChild(t).gameObject);
}
}
}
}
}