环测试

环测试

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++;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值