DCS/ruiyiweiUX/Assets/GeneralTools/Scripts/Always/ShowSeam.cs

80 lines
2.6 KiB
C#
Raw Permalink Normal View History

2026-06-09 13:59:11 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace GeneralTools
{
[ExecuteInEditMode]
public class ShowSeam : MonoBehaviour
{
public bool isShowSeam = false;
public int widthCount = 0;
public int heightCount = 0;
[Range(0, 10)]
public float lineWidth = 2;
public Color lineColor = Color.white;
public RectTransform linePrefab;
public Transform seamCnavas;
void Update()
{
#if UNITY_EDITOR
UtilityTool.DeleteChild(seamCnavas);
if (!isShowSeam)
return;
//边
CreateHorizontalLine(new Vector2(0, -Screen.height / 2));
CreateHorizontalLine(new Vector2(0, Screen.height / 2));
CreateVerticalLine(new Vector2(-Screen.width / 2, 0));
CreateVerticalLine(new Vector2(Screen.width / 2, 0));
//内部
if (widthCount >= 2)
{
int lineCount = widthCount - 1;
float leftX = -Screen.width / 2;
float interval = Screen.width / widthCount;
for (int i = 0; i < lineCount; i++)
{
Vector2 newPos = new Vector2(leftX + interval * (i + 1), 0);
CreateVerticalLine(newPos);
}
}
if (heightCount >= 2)
{
int lineCount = heightCount - 1;
float downY = -Screen.height / 2;
float interval = Screen.height / heightCount;
for (int i = 0; i < lineCount; i++)
{
Vector2 newPos = new Vector2(0, downY + interval * (i + 1));
CreateHorizontalLine(newPos);
}
}
#endif
}
protected void CreateHorizontalLine(Vector2 pos)
{
RectTransform newLine = Instantiate(linePrefab, seamCnavas);
newLine.localScale = Vector3.one;
newLine.eulerAngles = Vector3.zero;
newLine.sizeDelta = new Vector2(Screen.width, lineWidth);
newLine.localPosition = pos;
newLine.GetComponent<Image>().color = lineColor;
}
protected void CreateVerticalLine(Vector2 pos)
{
RectTransform newLine = Instantiate(linePrefab, seamCnavas);
newLine.localScale = Vector3.one;
newLine.eulerAngles = Vector3.zero;
newLine.sizeDelta = new Vector2(lineWidth, Screen.height);
newLine.localPosition = pos;
newLine.GetComponent<Image>().color = lineColor;
}
}
}