148 lines
4.4 KiB
C#
148 lines
4.4 KiB
C#
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using AibisDream;
|
|
|
|
public class InteractiveLineSegment : VisualLineSegment
|
|
{
|
|
private Transform startTransform;
|
|
public Transform StartTransform
|
|
{
|
|
get { return startTransform; }
|
|
set { startTransform = value; }
|
|
}
|
|
|
|
private Transform endTransform;
|
|
public Transform EndTransform
|
|
{
|
|
get { return endTransform; }
|
|
set { endTransform = value; }
|
|
}
|
|
|
|
private LineRenderer strokeLineRenderer; // 新的 LineRenderer 对象作为描边
|
|
|
|
private BoxCollider2D boxCollider;
|
|
|
|
private YarnOption _option;
|
|
|
|
public YarnOption Option
|
|
{
|
|
get => _option;
|
|
set => _option = value;
|
|
}
|
|
public bool IsCuttable
|
|
{
|
|
get { return _isCuttable; }
|
|
set
|
|
{
|
|
_isCuttable = value;
|
|
// Change the color of the line segment based on its cuttability
|
|
//GetComponent<LineRenderer>().material.color = value ? Color.white : Color.black;
|
|
}
|
|
}
|
|
private bool _isCuttable=false;
|
|
|
|
public void OnHover()
|
|
{
|
|
InitializeStrokeLineRenderer();
|
|
strokeLineRenderer.enabled = true;
|
|
// if (IsCuttable)
|
|
// {
|
|
// TurnGreen();
|
|
// }
|
|
// else
|
|
// {
|
|
// TurnRed();
|
|
// }
|
|
|
|
}
|
|
|
|
public void OnHoverExit()
|
|
{
|
|
//InitializeStrokeLineRenderer();
|
|
strokeLineRenderer.enabled = false;
|
|
}
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
boxCollider = GetComponent<BoxCollider2D>();
|
|
|
|
}
|
|
public void Start()
|
|
{
|
|
Initialize();
|
|
IsCuttable=false;
|
|
InitializeStrokeLineRenderer();
|
|
strokeLineRenderer.enabled = false;
|
|
|
|
}
|
|
void Update()
|
|
{
|
|
Initialize();
|
|
//InitializeStrokeLineRenderer();
|
|
}
|
|
private LineRenderer CopyLineRenderer(LineRenderer source)
|
|
{
|
|
LineRenderer newLineRenderer = new GameObject("StrokeLine").AddComponent<LineRenderer>();
|
|
|
|
// Copy the properties from the source LineRenderer to the new one
|
|
newLineRenderer.material = source.material;
|
|
newLineRenderer.startColor = source.startColor;
|
|
newLineRenderer.endColor = source.endColor;
|
|
newLineRenderer.startWidth = source.startWidth;
|
|
newLineRenderer.endWidth = source.endWidth;
|
|
newLineRenderer.numCornerVertices = source.numCornerVertices;
|
|
newLineRenderer.numCapVertices = source.numCapVertices;
|
|
newLineRenderer.positionCount = source.positionCount;
|
|
newLineRenderer.sortingLayerName = source.sortingLayerName;
|
|
newLineRenderer.sortingOrder = source.sortingOrder-1;
|
|
|
|
// Set the parent of the new GameObject to the same as the source
|
|
newLineRenderer.transform.parent = source.transform;
|
|
|
|
return newLineRenderer;
|
|
}
|
|
private void InitializeStrokeLineRenderer()
|
|
{
|
|
strokeLineRenderer = CopyLineRenderer(GetComponent<LineRenderer>());
|
|
// 设置描边的 LineRenderer 的属性
|
|
strokeLineRenderer.SetPosition(0, StartTransform.position);
|
|
strokeLineRenderer.SetPosition(1, EndTransform.position);
|
|
strokeLineRenderer.endColor = Color.white;
|
|
strokeLineRenderer.startColor = Color.white;
|
|
strokeLineRenderer.widthMultiplier = lineRenderer.widthMultiplier * 1.2f; // 假设描边的宽度是原线条宽度的 1.2 倍
|
|
//strokeLineRenderer.enabled = false;
|
|
// 初始时禁用描边
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
//IsCuttable=false;
|
|
if (StartTransform == null || EndTransform == null)
|
|
{
|
|
Debug.LogError("StartTransform or EndTransform is not set.");
|
|
return;
|
|
}
|
|
|
|
base.Initialize(StartTransform.position, EndTransform.position);
|
|
|
|
// Calculate the length of the line segment
|
|
float length = Vector3.Distance(StartPoint, EndPoint);
|
|
|
|
// Calculate the direction of the line segment
|
|
Vector3 direction = (EndPoint - StartPoint).normalized;
|
|
|
|
// Set the size of the BoxCollider2D
|
|
boxCollider.size = new Vector2(length, 0.6f); // Change 0.1f to the desired thickness of the line
|
|
|
|
// Set the center of the BoxCollider2D
|
|
boxCollider.offset = new Vector2(length / 2, 0);
|
|
|
|
// Rotate the BoxCollider2D to align with the line segment
|
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|
boxCollider.transform.rotation = Quaternion.Euler(0, 0, angle);
|
|
|
|
// Set the position of the BoxCollider2D to the start point
|
|
boxCollider.transform.position = StartPoint;
|
|
}
|
|
}
|