环测试
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Ring : MonoBehaviour
{
/// <summary>
/// 显示的对象的用于生产拷贝的元
/// </summary>
public GameObject textGameObj;
/// <summary>
/// 位置偏移X
/// </summary>
private Vector3 offsetX;
/// <summary>
/// 位置偏移Y
/// </summary>
private Vector3 offsetY;
/// <summary>
/// 需要的数量
/// </summary>
public int number = 10;
/// <summary>
/// 记录当前数量
/// </summary>
private int current_number = 0;
/// <summary>
/// 记录点的坐标
/// </summary>
private struct Point2
{
public int x;
public int y;
public Point2(int x,int y)
{
this.x = x;
this.y = y;
}
public static Point2 operator *(Point2 point,int num)
{
return new Point2(point.x * num, point.y * num);
}
public static Point2 operator *(int num, Point2 point)
{
return new Point2(point.x * num, point.y * num);
}
}
void Start()
{
//获取大小
Vector2 text_size = textGameObj.GetComponent<RectTransform>().sizeDelta;
//设置偏移
offsetX = new Vector3(text_size.x, 0, 0);
offsetY = new Vector3(0, text_size.y, 0);
//起始点
Point2 point = new Point2(0, 0);
//起始长度
int length = 1;
//起始方向
Point2 dir = new Point2(1, 1);
//第一个点
CreateText(point);
//一次两个 一次加一次减 之后变向长一再来两个 四个一循环
while (number >= current_number)
{
//横向
for (int i = 0; i < length; i++)
{
point.x += dir.x;
CreateText(point);
}
//纵向
for (int i = 0; i < length; i++)
{
point.y += dir.y;
CreateText(point);
}
//变向
dir.x = -dir.x;
dir.y = -dir.y;
//长度
length++;
}
}
/// <summary>
/// 生成及显示
/// </summary>
/// <param name="point"></param>
private void CreateText(Point2 point)
{
if (current_number > number)
return;
Vector3 position = textGameObj.transform.position + offsetX * point.x + offsetY * point.y;
GameObject tempObj = Instantiate(textGameObj, textGameObj.transform.parent);
tempObj.transform.position = position;
tempObj.GetComponent<Text>().text = string.Format("({0},{1})={2}", point.x, point.y, current_number + 1);
tempObj.SetActive(true);
current_number++;
}
}