Files
MRCS/Assets/_Valheim/Scripts/Player/GuideArrowPath.cs

102 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuideArrowPath : MonoBehaviour
{
public LayerMask obstacleMask;
public float pathHeight = 0.5f;
private LineRenderer lineRenderer;
private bool isPathVisible = false;
void Awake()
{
// 从预制体上获取已存在的LineRenderer组件
lineRenderer = GetComponent<LineRenderer>();
if (lineRenderer == null)
{
Debug.LogError("LineRenderer组件未找到请确保预制体上已挂载LineRenderer组件");
return;
}
// 预制体上已预设LineRenderer属性这里不再动态设置
// 如果需要在代码中覆盖预制体设置,可以取消注释以下代码
/*
lineRenderer.startWidth = 0.1f;
lineRenderer.endWidth = 0.1f;
lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
lineRenderer.startColor = Color.yellow;
lineRenderer.endColor = Color.red;
*/
lineRenderer.positionCount = 0;
lineRenderer.enabled = false; // 初始禁用
Debug.Log("GuideArrowPath 初始化完成LineRenderer: " + (lineRenderer != null));
}
/// <summary>
/// 设置路径
/// </summary>
public void SetPath(List<Vector3> pathPoints)
{
if (lineRenderer == null)
{
Debug.LogError("LineRenderer 未找到!");
return;
}
if (pathPoints == null || pathPoints.Count < 2)
{
lineRenderer.positionCount = 0;
return;
}
// 调整路径点高度
for (int i = 0; i < pathPoints.Count; i++)
{
pathPoints[i] = new Vector3(pathPoints[i].x, pathHeight, pathPoints[i].z);
}
lineRenderer.positionCount = pathPoints.Count;
lineRenderer.SetPositions(pathPoints.ToArray());
Debug.Log("设置路径,点数: " + pathPoints.Count);
}
/// <summary>
/// 显示路径
/// </summary>
public void ShowPath()
{
if (lineRenderer != null)
{
lineRenderer.enabled = true;
isPathVisible = true;
Debug.Log("显示路径LineRenderer enabled: " + lineRenderer.enabled);
}
}
/// <summary>
/// 关闭路径
/// </summary>
public void ClosePath()
{
if (lineRenderer != null)
{
lineRenderer.enabled = false;
isPathVisible = false;
lineRenderer.positionCount = 0;
Debug.Log("关闭路径");
}
}
/// <summary>
/// 检查路径是否可见
/// </summary>
public bool IsPathVisible()
{
return isPathVisible && lineRenderer != null && lineRenderer.enabled;
}
}