WXGame/box1/Assets/Script/UI/HomePanel/BuildButton.cs
2024-12-04 17:26:27 +08:00

160 lines
5.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using cfg;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class BuildButton : MonoBehaviour
{
public BuildEnum build;
[SerializeField] private HomePanel _homePanel;
[SerializeField] private Button _button;
[SerializeField] private TextMeshProUGUI _textMeshProUGUI;
[SerializeField] private GameObject name;
[SerializeField] private GameObject upImage;
private bool eventOpen;
private void Awake()
{
_button = GetComponent<Button>();
}
private void OnEnable()
{
_button.onClick.AddListener(PayItem);
}
private void OnDisable()
{
_button.onClick.RemoveListener(PayItem);
}
private void PayItem()
{
if (!eventOpen)
{
return;
}
switch(build)
{
case BuildEnum.window:
var windowLv = DataManager.GetHomeFurnitureID("window");
var windowData = JsonTab.Instance.tables.BuildLevelUp.Get(windowLv);
DataManager.SetSpend(windowData.Buildingup + DataManager.GetSpend());
DataManager.SetHomeFurnitureID("window",windowData.Buildingnext);
break;
case BuildEnum.light:
var lightLv = DataManager.GetHomeFurnitureID("light");
var lightData = JsonTab.Instance.tables.BuildLevelUp.Get(lightLv);
DataManager.SetSpend(lightData.Buildingup + DataManager.GetSpend());
DataManager.SetHomeFurnitureID("light",lightData.Buildingnext);
break;
case BuildEnum.carpet:
var carpetLv = DataManager.GetHomeFurnitureID("carpet");
var carpetData = JsonTab.Instance.tables.BuildLevelUp.Get(carpetLv);
DataManager.SetSpend(carpetData.Buildingup + DataManager.GetSpend());
DataManager.SetHomeFurnitureID("carpet",carpetData.Buildingnext);
break;
case BuildEnum.closet:
var closetLv = DataManager.GetHomeFurnitureID("closet");
var closetData = JsonTab.Instance.tables.BuildLevelUp.Get(closetLv);
DataManager.SetSpend(closetData.Buildingup + DataManager.GetSpend());
DataManager.SetHomeFurnitureID("closet",closetData.Buildingnext);
break;
case BuildEnum.chair:
var chairLv = DataManager.GetHomeFurnitureID("chair");
var chairData = JsonTab.Instance.tables.BuildLevelUp.Get(chairLv);
DataManager.SetSpend(chairData.Buildingup + DataManager.GetSpend());
DataManager.SetHomeFurnitureID("chair",chairData.Buildingnext);
break;
default:
throw new ArgumentOutOfRangeException();
}
_homePanel.ResetImage();
}
public void RestGameObject(HomePanel panel)
{
_homePanel = panel;
switch (build)
{
case BuildEnum.window:
var windowLv = DataManager.GetHomeFurnitureID("window");
var windowData = JsonTab.Instance.tables.BuildLevelUp.Get(windowLv);
ButtonRest(windowData);
break;
case BuildEnum.light:
var lightLv = DataManager.GetHomeFurnitureID("light");
var lightData = JsonTab.Instance.tables.BuildLevelUp.Get(lightLv);
ButtonRest(lightData);
break;
case BuildEnum.carpet:
var carpetLv = DataManager.GetHomeFurnitureID("carpet");
var carpetData = JsonTab.Instance.tables.BuildLevelUp.Get(carpetLv);
ButtonRest(carpetData);
break;
case BuildEnum.closet:
var closetLv = DataManager.GetHomeFurnitureID("closet");
var closetData = JsonTab.Instance.tables.BuildLevelUp.Get(closetLv);
ButtonRest(closetData);
break;
case BuildEnum.chair:
var chairLv = DataManager.GetHomeFurnitureID("chair");
var chairData = JsonTab.Instance.tables.BuildLevelUp.Get(chairLv);
ButtonRest(chairData);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void ButtonRest(BBuildLevelUp chairData)
{
BoxDebug.Log(chairData);
if (chairData.Buildingup == 0)
{
gameObject.SetActive(false);
}
else
{
gameObject.SetActive(true);
if (chairData.Buildingup<=DataManager.GetStar())
{
upImage.SetActive(true);
eventOpen = true;
name.SetActive(false);
_textMeshProUGUI.text = "x" + chairData.Buildingup;
}
else
{
upImage.SetActive(false);
eventOpen = false;
name.SetActive(false);
}
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
public enum BuildEnum
{
window,
light,
carpet,
closet,
chair
}