主流程疏通完毕,可以选择地图尺寸、阵营、玩家数量

This commit is contained in:
daixiawu 2025-05-27 22:37:26 +08:00
parent 239d4e1980
commit bfd3ca4a11
7 changed files with 8438 additions and 6293 deletions

File diff suppressed because it is too large Load Diff

View File

@ -30,9 +30,6 @@ namespace Logic
public bool DebugShowAIID = false;
[Header("Play Settings")]
public uint width;
public uint height;
public uint playerCount;
public int cityCount;
public int unitCount;
public int turn;
@ -118,7 +115,7 @@ namespace Logic
//移动镜头+显示开局提示
//UIManager.CenterMessageUI.SetCenterMessageShow(UICenterMessageID.StartGame,MapData.PlayerMap.SelfPlayerData);
}
public void StartGame(uint h, uint w, uint civId, uint forceId)
public void StartGame(uint h, uint w,uint playerCount, uint civId, uint forceId)
{
MapConfig = new MapConfig(h,w,playerCount,civId,forceId);
//初始化所有Data

View File

@ -49,7 +49,7 @@ public class ChooseUI
public void Init()
{
for (int i = 0; i < 4; i++)
for (int i = 0; i < 3; i++)
{
int index = i; // 闭包用局部变量
int row = i / 3;
@ -59,20 +59,41 @@ public class ChooseUI
fadeIns[i] = Resources.Load<AnimationClip>($"Animations/UI/ChooseUI{_name[i]}PanelFadeIn");
fadeOuts[i] = Resources.Load<AnimationClip>($"Animations/UI/ChooseUI{_name[i]}PanelFadeOut");
_buttons[i].onClick.AddListener(() => SwitchToPanel(index));
if(!ROChooseUI.transform.Find($"ForceListPanel/TribeList/{_name[i]}Div").GetComponent<TribeHoverEffect>().IsLocked)
_buttons[i].onClick.AddListener(() => SwitchToPanel(index));
}
}
public void StartGame()
{
if (_currentPanelIndex == -1)
return;
NeedShow = false;
uint civ = _civ[_currentPanelIndex];
string mapSizeS = ROChooseUI.transform.Find("ForceListPanel/SettingBar/MapSize").GetComponent<ToggleButtonGroupController>()
.GetSelectedButtonName();
string playerCountS = ROChooseUI.transform.Find("ForceListPanel/SettingBar/EnemyCount").GetComponent<ToggleButtonGroupController>()
.GetSelectedButtonName();
string diffS = ROChooseUI.transform.Find("ForceListPanel/SettingBar/Difficulty").GetComponent<ToggleButtonGroupController>()
.GetSelectedButtonName();
if (!int.TryParse(mapSizeS.Substring(10), out int mapSize))
mapSize = 18;
if (!int.TryParse(playerCountS.Substring(12), out int playerCount))
playerCount = 18;
NeedShow = false;
_currentPanelIndex = 0;
for (int i = 0; i < 3; i++)
{
if (!ROChooseUI.transform.Find($"ForceListPanel/TribeList/{_name[i]}Div").GetComponent<TribeHoverEffect>()
.CheckIsSelected())
continue;
_currentPanelIndex = i;
break;
}
uint civ = _civ[_currentPanelIndex];
uint force = _force[_currentPanelIndex];
_main.StartGame(15, 15, civ, force);
_main.StartGame((uint)mapSize, (uint)mapSize,(uint)playerCount, civ, force);
}
public void Update()

View File

@ -1,112 +0,0 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
using System.Collections;
[RequireComponent(typeof(RectTransform))]
public class SnapScrollPicker : MonoBehaviour, IEndDragHandler
{
public ScrollRect scrollRect;
public RectTransform content;
public float itemHeight = 40f;
public float snapSpeed = 10f;
public float snapThreshold = 0.1f;
private int itemCount;
private bool isSnapping = false;
public int selectedIndex = -1;
void Start()
{
// 自动绑定组件
if (scrollRect == null)
scrollRect = GetComponentInChildren<ScrollRect>();
if (content == null && scrollRect != null)
content = scrollRect.content;
if (scrollRect != null)
{
scrollRect.horizontal = false;
scrollRect.vertical = true;
}
if (content != null)
{
itemCount = content.childCount;
// 默认选中第 3 项(可自行修改)
SnapToIndex(3);
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (!isSnapping && content != null)
{
Debug.Log("拖动结束,开始吸附");
StartCoroutine(SnapToClosestItem());
}
}
IEnumerator SnapToClosestItem()
{
isSnapping = true;
// ✅ 清除 ScrollRect 的惯性速度
scrollRect.velocity = Vector2.zero;
int closestIndex = GetClosestIndex();
float targetY = GetTargetAnchoredY(closestIndex);
while (Mathf.Abs(content.anchoredPosition.y - targetY) > snapThreshold)
{
float newY = Mathf.Lerp(content.anchoredPosition.y, targetY, Time.deltaTime * snapSpeed);
content.anchoredPosition = new Vector2(content.anchoredPosition.x, newY);
yield return null;
}
content.anchoredPosition = new Vector2(content.anchoredPosition.x, targetY);
selectedIndex = closestIndex;
HighlightSelectedItem(closestIndex);
isSnapping = false;
}
int GetClosestIndex()
{
float centerY = content.anchoredPosition.y + scrollRect.viewport.rect.height / 2;
int index = Mathf.RoundToInt(centerY / itemHeight);
return Mathf.Clamp(index, 0, itemCount - 1);
}
float GetTargetAnchoredY(int index)
{
return index * itemHeight - scrollRect.viewport.rect.height / 2 + itemHeight / 2;
}
void HighlightSelectedItem(int selected)
{
for (int i = 0; i < itemCount; i++)
{
var item = content.GetChild(i).GetComponent<TextMeshProUGUI>();
if (item != null)
{
item.color = (i == selected)
? new Color32(0xFF, 0xFF, 0xFF, 255) // 高亮白色
: new Color32(0x78, 0x78, 0x78, 255); // 默认灰色
}
}
}
public void SnapToIndex(int index)
{
index = Mathf.Clamp(index, 0, itemCount - 1);
float targetY = GetTargetAnchoredY(index);
content.anchoredPosition = new Vector2(content.anchoredPosition.x, targetY);
selectedIndex = index;
HighlightSelectedItem(index);
}
}

View File

@ -0,0 +1,61 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class ToggleButtonGroupController : MonoBehaviour
{
public string buttonGroupName = "ButtonGroup";
private Transform buttonGroup;
private List<Button> buttons = new List<Button>();
private Button selectedButton;
// 高亮与默认颜色
private readonly Color selectedColor = new Color32(0x3E, 0xAB, 0x02, 255); // #3EAB02
private readonly Color normalColor = new Color32(0x00, 0x00, 0x00, 112); // #000000, alpha 112
void Start()
{
buttonGroup = transform.Find(buttonGroupName);
if (buttonGroup == null)
{
Debug.LogError($"ButtonGroup '{buttonGroupName}' not found under {gameObject.name}");
return;
}
// 查找所有按钮并绑定点击事件
foreach (Transform child in buttonGroup)
{
Button btn = child.GetComponent<Button>();
if (btn != null)
{
buttons.Add(btn);
btn.onClick.AddListener(() => OnButtonClicked(btn));
}
}
// 默认选中第一个
if (buttons.Count > 0)
OnButtonClicked(buttons[0]);
}
private void OnButtonClicked(Button clickedButton)
{
selectedButton = clickedButton;
foreach (Button btn in buttons)
{
Image bgImage = btn.transform.Find("BG")?.GetComponent<Image>();
if (bgImage != null)
{
bgImage.color = (btn == selectedButton) ? selectedColor : normalColor;
}
}
}
public string GetSelectedButtonName()
{
return selectedButton != null ? selectedButton.name : "";
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 1e4899d9292f8b1439c8591248d450e6
guid: 328d89dcb4fe48b4299483c8e28faf2e
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -9,6 +9,7 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
private GameObject selectedObject;
private Button button;
private bool isSelected = false;
private Color highlightColor = new Color32(0x2A, 0xFF, 0x00, 255); // #2AFF00
@ -17,6 +18,8 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
private byte hoverAlpha = 200;
private byte selectedAlpha = 255;
public bool IsLocked;
void Awake()
{
// 自动获取 UI 组件
@ -30,11 +33,19 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
void Start()
{
if (!isSelected)
// 自动选中同级中第一个拥有 TribeHoverEffect 的对象
if (IsFirstTribeInParent())
{
ApplyNormalState();
if (selectedObject != null)
selectedObject.SetActive(false);
OnClicked(); // 默认选中
}
else
{
if (!isSelected)
{
ApplyNormalState();
if (selectedObject != null)
selectedObject.SetActive(false);
}
}
// 如果有 Button则通过代码绑定点击事件
@ -44,10 +55,27 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
}
}
private bool IsFirstTribeInParent()
{
Transform parent = transform.parent;
if (parent == null) return false;
foreach (Transform child in parent)
{
var effect = child.GetComponent<TribeHoverEffect>();
if (effect != null)
{
return child == transform; // 第一个找到的就是自己
}
}
return false;
}
// 供 Button 调用的封装
public void HandleButtonClick()
{
OnClicked();
if(!IsLocked)
OnClicked();
}
private void OnClicked()
@ -78,6 +106,7 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
public void OnPointerEnter(PointerEventData eventData)
{
if (IsLocked) return;
if (!isSelected)
{
selectImage.color = highlightColor;
@ -87,6 +116,7 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
public void OnPointerExit(PointerEventData eventData)
{
if (IsLocked) return;
if (!isSelected)
{
selectImage.color = defaultColor;
@ -96,7 +126,9 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
private void ApplyNormalState()
{
selectImage.color = defaultColor;
if (selectImage != null)
selectImage.color = defaultColor;
SetAlpha(maskImage, normalAlpha);
}
@ -115,4 +147,9 @@ public class TribeHoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExi
if (selectedObject != null)
selectedObject.SetActive(false);
}
public bool CheckIsSelected()
{
return isSelected;
}
}