Files
daluandou/Assets/Script/CrystalMineCapture.cs
T
huadexuan 0e7de5f885 ```
feat(materials): 更新材质贴图纹理和缩放参数

- 修改CoreBattleGround.mat材质的MainTex纹理GUID为4435756e5310e408fb125e8f4bcee9bd
- 调整CoreBattleGround.mat材质中EmissionMap和MainTex的缩放比例从{x: 1, y: 1}改为{x: 3, y: 3}

fix(models): 调整建筑模型全局缩放比例

- 将主要基地模型的globalScale从3调整为5
- 将水晶矿模型的globalScale从1调整为7,对应骨骼缩放从1调整为0.07
- 将机械出兵点模型的globalScale从1调整为7,对应骨骼缩放从1调整为0.07

feat(textures): 为模型添加纹理贴图

- 为水晶矿模型材质添加MainTex纹理,GUID为c24a978a0c9ad4dabb831e4b2bff0e2f
- 为机械出兵点模型材质添加MainTex纹理,GUID为fb52fc095c9554db895dde01ba8a8b6c
```
2026-09-18 18:06:09 +08:00

198 lines
7.1 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class CrystalMineCapture : MonoBehaviour
{
[Header("争夺进度")]
[Range(0f, 100f)] public float blueProgress = 50f;
[Min(0f)] public float capturePerDamage = 1f;
[Header("水晶矿光球")]
[SerializeField] private AdjustableGlowOrb glowOrb;
[SerializeField] private Color neutralColor = Color.white;
[SerializeField] private Color blueColor = new Color(0.1f, 0.65f, 1f, 1f);
[SerializeField] private Color redColor = new Color(1f, 0.12f, 0.08f, 1f);
[Header("世界血条")]
[Min(0f)] public float heightOffset = 1.1f;
[Min(0.1f)] public float barWidth = 1.8f;
private Canvas canvas;
private RectTransform barRect;
private Image blueFill;
private Image redFill;
private RectTransform marker;
private UnitTeam unitTeam;
private static Sprite roundedSprite;
private static readonly int BaseColorId = Shader.PropertyToID("_Color");
private void Awake()
{
unitTeam = GetComponent<UnitTeam>();
if (unitTeam == null)
{
unitTeam = gameObject.AddComponent<UnitTeam>();
}
unitTeam.teamId = 2;
if (glowOrb == null)
{
glowOrb = GetComponentInChildren<AdjustableGlowOrb>(true);
}
CreateCaptureBar();
RefreshVisuals();
}
private void Start()
{
RefreshVisuals();
}
private void LateUpdate()
{
if (barRect == null) return;
barRect.position = transform.position + Vector3.up * heightOffset;
Vector3 parentScale = transform.lossyScale;
float targetScale = barWidth / 100f;
barRect.localScale = new Vector3(
parentScale.x != 0f ? targetScale / parentScale.x : targetScale,
parentScale.y != 0f ? targetScale / parentScale.y : targetScale,
parentScale.z != 0f ? targetScale / parentScale.z : targetScale);
Camera camera = Camera.main;
if (camera != null)
{
barRect.rotation = Quaternion.LookRotation(camera.transform.position - barRect.position, camera.transform.up);
}
}
public bool ReceiveCaptureDamage(float damage, GameObject attacker)
{
if (damage <= 0f || attacker == null) return false;
UnitTeam attackerTeam = attacker.GetComponentInParent<UnitTeam>();
if (attackerTeam == null) return false;
float direction = attackerTeam.teamId == 0 ? 1f : -1f;
blueProgress = Mathf.Clamp(blueProgress + direction * damage * capturePerDamage, 0f, 100f);
RefreshVisuals();
return true;
}
public void SetCaptureProgress(float value)
{
blueProgress = Mathf.Clamp(value, 0f, 100f);
RefreshVisuals();
}
private void RefreshVisuals()
{
if (blueFill == null || redFill == null) return;
float blue = blueProgress / 100f;
blueFill.fillAmount = blue;
redFill.fillAmount = 1f - blue;
marker.anchorMin = new Vector2(blue, 0f);
marker.anchorMax = new Vector2(blue, 1f);
marker.anchoredPosition = Vector2.zero;
marker.gameObject.SetActive(blueProgress > 0f && blueProgress < 100f);
if (unitTeam != null)
{
unitTeam.teamId = blueProgress >= 100f ? 0 : blueProgress <= 0f ? 1 : 2;
}
if (glowOrb != null)
{
Color color = blueProgress >= 100f ? blueColor : blueProgress <= 0f ? redColor : neutralColor;
glowOrb.SetGlowColor(color);
}
}
private void CreateCaptureBar()
{
GameObject root = new GameObject("CrystalCaptureBar", typeof(RectTransform), typeof(Canvas), typeof(CanvasScaler));
root.transform.SetParent(transform, false);
canvas = root.GetComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvas.sortingOrder = 110;
root.GetComponent<CanvasScaler>().dynamicPixelsPerUnit = 10f;
barRect = root.GetComponent<RectTransform>();
barRect.sizeDelta = new Vector2(100f, 16f);
barRect.localScale = Vector3.one * (barWidth / 100f);
RectTransform row = CreateRow(root.transform, "CaptureBar", 100f, 14f);
Image background = CreateImage(row, "Background", new Color(0.03f, 0.03f, 0.03f, 0.95f));
SetFullRect(background.rectTransform);
blueFill = CreateImage(row, "BlueFill", blueColor);
blueFill.type = Image.Type.Filled;
blueFill.fillMethod = Image.FillMethod.Horizontal;
blueFill.fillOrigin = 0;
blueFill.rectTransform.anchorMin = new Vector2(0f, 0f);
blueFill.rectTransform.anchorMax = new Vector2(1f, 1f);
blueFill.rectTransform.offsetMin = new Vector2(2f, 2f);
blueFill.rectTransform.offsetMax = new Vector2(-2f, -2f);
redFill = CreateImage(row, "RedFill", redColor);
redFill.type = Image.Type.Filled;
redFill.fillMethod = Image.FillMethod.Horizontal;
redFill.fillOrigin = 1;
redFill.rectTransform.anchorMin = new Vector2(0f, 0f);
redFill.rectTransform.anchorMax = new Vector2(1f, 1f);
redFill.rectTransform.offsetMin = new Vector2(2f, 2f);
redFill.rectTransform.offsetMax = new Vector2(-2f, -2f);
GameObject markerObject = new GameObject("CenterMarker", typeof(RectTransform), typeof(Image));
markerObject.transform.SetParent(row, false);
marker = markerObject.GetComponent<RectTransform>();
marker.sizeDelta = new Vector2(2f, 14f);
marker.anchorMin = new Vector2(0.5f, 0f);
marker.anchorMax = new Vector2(0.5f, 1f);
marker.GetComponent<Image>().color = Color.white;
}
private RectTransform CreateRow(Transform parent, string name, float width, float height)
{
GameObject rowObject = new GameObject(name, typeof(RectTransform));
rowObject.transform.SetParent(parent, false);
RectTransform rect = rowObject.GetComponent<RectTransform>();
rect.anchorMin = new Vector2(0.5f, 0.5f);
rect.anchorMax = new Vector2(0.5f, 0.5f);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.sizeDelta = new Vector2(width, height);
return rect;
}
private Image CreateImage(Transform parent, string name, Color color)
{
GameObject imageObject = new GameObject(name, typeof(RectTransform), typeof(Image));
imageObject.transform.SetParent(parent, false);
Image image = imageObject.GetComponent<Image>();
image.sprite = RoundedSprite();
image.color = color;
return image;
}
private void SetFullRect(RectTransform rect)
{
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.offsetMin = Vector2.zero;
rect.offsetMax = Vector2.zero;
}
private static Sprite RoundedSprite()
{
if (roundedSprite != null) return roundedSprite;
Texture2D texture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
texture.SetPixel(0, 0, Color.white);
texture.Apply();
roundedSprite = Sprite.Create(texture, new Rect(0f, 0f, 1f, 1f), new Vector2(0.5f, 0.5f));
roundedSprite.name = "CrystalCaptureBarSprite";
return roundedSprite;
}
}