Files
TaiWan/Assets/ZX/ZX7/Scripts/ZX7Controller.cs
2025-10-31 15:20:38 +08:00

318 lines
8.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Linq;
using UnityEngine.Networking;
public class ZX7Controller : MonoBehaviour
{
/// <summary>
/// 景点预设体
/// </summary>
public GameObject prePlace;
/// <summary>
/// 预设体所在父对象
/// </summary>
public Transform tranPlaceParent;
/// <summary>
/// 关闭按钮
/// </summary>
public Button btnClose;
/// <summary>
/// 下一页按钮
/// </summary>
public Button btnNext;
/// <summary>
/// 上一页按钮
/// </summary>
public Button btnPriv;
/// <summary>
/// 返回按钮
/// </summary>
public Button btnReturn;
/// <summary>
/// 城市标题
/// </summary>
public Text txtCity;
/// <summary>
/// 结果图片
/// </summary>
public Image imgDetail;
/// <summary>
/// 结果文本
/// </summary>
public Text txtDetail;
/// <summary>
/// 标题面板Image
/// </summary>
public Image imgPnlTitle;
/// <summary>
/// 标题图片Transform
/// </summary>
public Transform tranImgTitle;
/// <summary>
/// 数据文件名
/// </summary>
public string DataFileName = "CityPlaces.json";
public Sprite spSelected;
public Sprite spNormal;
/// <summary>
/// 选中的城市
/// </summary>
string selectedCity;
/// <summary>
/// 城市景点列表
/// </summary>
Dictionary<string, string> dicCityPlaces = new Dictionary<string, string>();
string placeName;
public Animator anim;
int currentIndex;
public RectTransform rectPlaceContent;
public RectTransform rectDetailContent;
string basePath;
void Start()
{
basePath = CommonData.DataServer + "/ZX7/";
StartCoroutine(JsonLoader.LoadJSON(basePath + "Data/" + DataFileName, onGetCityPlacesInfoCompleted));
if (btnClose != null)
btnClose.onClick.AddListener(OnBtnCloseClick);
if (btnPriv != null)
btnPriv.onClick.AddListener(OnBtnPrivClick);
if (btnNext != null)
btnNext.onClick.AddListener(OnBtnNextClick);
if (btnReturn != null)
btnReturn.onClick.AddListener(OnBtnReturnClick);
}
private void OnBtnReturnClick()
{
currentIndex = 0;
selectedCity = null;
rectPlaceContent.anchoredPosition = Vector2.zero;
anim.SetTrigger("HideCity");
}
private void OnBtnPrivClick()
{
if (currentIndex > 0)
{
currentIndex--;
btnPriv.enabled = false;
btnNext.enabled = false;
rectPlaceContent.anchoredPosition += Vector2.down * 138f;
Invoke("EnableButton", 0.2f);
//Tween myTween = rectPlaceContent.DOAnchorPosY(rectPlaceContent.anchoredPosition.y - 138, 0.2f);
//myTween.SetEase(Ease.OutQuint).onComplete = delegate
//{
// btnPriv.enabled = true;
// btnNext.enabled = true;
//};
}
else
{
btnPriv.GetComponent<Image>().color = Color.red;
Invoke("ResetPrivButtonColor", 0.2f);
//rectPlaceContent.DOShakePosition(1f, 2f);
}
}
void EnableButton()
{
btnPriv.enabled = true;
btnNext.enabled = true;
}
void ResetPrivButtonColor()
{
btnPriv.GetComponent<Image>().color = Color.white;
}
void ResetNextButtonColor()
{
btnNext.GetComponent<Image>().color = Color.white;
}
private void OnBtnNextClick()
{
if (currentIndex < rectPlaceContent.childCount - 1)
{
currentIndex++;
btnPriv.enabled = false;
btnNext.enabled = false;
rectPlaceContent.anchoredPosition += Vector2.up * 138f;
Invoke("EnableButton", 0.2f);
//Tween myTween = rectPlaceContent.DOAnchorPosY(rectPlaceContent.anchoredPosition.y + 138, 0.2f);
//myTween.SetEase(Ease.OutQuint).onComplete = delegate
//{
// btnPriv.enabled = true;
// btnNext.enabled = true;
//};
}
else
{
btnNext.GetComponent<Image>().color = Color.red;
Invoke("ResetNextButtonColor", 0.2f);
// rectPlaceContent.DOShakePosition(1f, 2f);
}
}
private void OnBtnCloseClick()
{
BaseController.HideView();
Destroy(gameObject.transform.parent.gameObject);
}
/// <summary>
/// 获取姓名列表
/// </summary>
public void onGetCityPlacesInfoCompleted(string data)
{
dicCityPlaces = JsonTools.DicFromJson<string, string>(data);
InitialCityList();
#region
//dicCityPlaces.Add("台北", "圆山文化遗址|红毛城|台北公会堂|台北府城|大坌坑文化遗址");
//dicCityPlaces.Add("台东", "长滨文化遗址|卑南文化遗址");
//dicCityPlaces.Add("台南", "热兰遮城|赤嵌城|台南孔子庙|二鯤鯓炮台|祀典武庙|五妃庙|大天后宫");
//dicCityPlaces.Add("彰化", "彰化孔子庙|道东书院|鹿港龙山寺");
//dicCityPlaces.Add("基隆", "二沙湾炮台");
//dicCityPlaces.Add("新竹", "金广福公馆");
//dicCityPlaces.Add("澎湖", "西屿东台|西屿西台|澎湖天后宫");
//dicCityPlaces.Add("高雄", "凤山县旧城");
//dicCityPlaces.Add("金门", "邱良功母节孝坊");
//string data = JsonTools.DicToJson(dicCityPlaces);
//StreamWriter writer = File.CreateText(DataFileName);
//writer.Write(data);
//writer.Close();
#endregion
}
void InitialCityList()
{
//foreach (var item in dicCityPlaces.Keys)
//{
//GameObject obj = Instantiate(preName, tranNameParent);
//obj.GetComponent<Button>().onClick.AddListener(OnBtnNameClick);
//obj.GetComponentInChildren<Text>().text = item.Key;
//obj.name = item.Key;
//}
}
public void OnBtnCityClick()
{
// 获取当前点击的对象
GameObject obj = EventSystem.current.currentSelectedGameObject;
if (selectedCity != obj.name)
{
selectedCity = obj.name;
txtCity.text = obj.name;
// 移除旧的景点列表
for (int i = 0; i < tranPlaceParent.childCount; i++)
{
Destroy(tranPlaceParent.GetChild(i).gameObject);
}
// 获取新的景点列表
string place = dicCityPlaces[obj.name];
lstPlaces = place.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
isNewCity = true;
if (lstPlaces != null)
{
int count = lstPlaces.Count;
ShowResult(lstPlaces[0]);
for (int i = 0; i < count; i++)
{
CreatePlace();
// Invoke("CreatePlace", 0.3f * i);
}
// OnBtnPlaceClick();
Invoke("OnBtnPlaceClick", 0.1f);
}
anim.SetTrigger("ShowCity");
}
}
List<string> lstPlaces;
void CreatePlace()
{
GameObject objPlace = Instantiate(prePlace, tranPlaceParent);
objPlace.name = lstPlaces[0];
objPlace.GetComponentInChildren<Text>().text = lstPlaces[0];
objPlace.GetComponent<Button>().onClick.AddListener(OnBtnPlaceClick);
lstPlaces.RemoveAt(0);
}
GameObject objSelected;
bool isNewCity;
public void OnBtnPlaceClick()
{
GameObject obj = EventSystem.current.currentSelectedGameObject;
if (isNewCity)
{
obj = tranPlaceParent.GetChild(0).gameObject;
}
isNewCity = false;
if (objSelected != null)
{
objSelected.GetComponent<Image>().sprite = spNormal;
}
obj.GetComponent<Image>().sprite = spSelected;
objSelected = obj;
rectDetailContent.anchoredPosition = Vector2.zero;
ShowResult(obj.name);
}
/// <summary>
/// 显示景点信息
/// </summary>
/// <param name="placeName">景点名称</param>
public void ShowResult(string placeName)
{
txtCity.text = string.Format("{0}︻{1}︼", selectedCity, placeName);
StartCoroutine(JsonLoader.LoadJSON(basePath + "Data/" + placeName +".txt", OnGetTextDetailsInfoCompleted));
StartCoroutine(ImageLoader<Image>.LoadImage(basePath + "Images/" + placeName + ".jpg", imgDetail));
}
/// <summary>
/// 获取文本信息结束
/// </summary>
void OnGetTextDetailsInfoCompleted(string data)
{
txtDetail.text = "\u3000\u3000" + data.Trim();
}
}