栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

unity在UI界面画线

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity在UI界面画线

我软件中的需求是,鼠标点击二维界面的位置要生成两个点,然后要在两个点直接连线。因此,我在我函数中传入两个GameObject(需要连线的两个点),然后返回出线的长度和需要旋转的方向。(PS:我的线是挂在第一点下的子物体,用image直接形成的,线的尺寸如下图)

画线代码如下:

    public void drawLine(GameObject ICO1, GameObject ICO2, out double length, out double angel)
    {
        Vector2 position1 = ICO1.transform.localPosition;
        Vector2 position2 = ICO2.transform.localPosition;
        float xValue = 0, yValue = 0;
        if ((position1.x > 0 && position2.x > 0) || (position1.x < 0 && position2.x < 0))
            xValue = Mathf.Abs(position1.x - position2.x);
        else
            xValue = Mathf.Abs(position1.x) + Mathf.Abs(position2.x);
        if ((position1.y > 0 && position2.y > 0) || (position1.y < 0 && position2.y < 0))
            yValue = Mathf.Abs(position1.y - position2.y);
        else
            yValue = Mathf.Abs(position1.y) + Mathf.Abs(position2.y);
        length = Mathf.Sqrt(xValue * xValue + yValue * yValue);

        if (position1.x <= position2.x && position1.y <= position2.y)
            angel = -90 + Mathf.Atan(yValue / xValue) / Mathf.PI * 180;
        else if (position1.x >= position2.x && position1.y <= position2.y)
            angel = 90 - Mathf.Atan(yValue / xValue) / Mathf.PI * 180;
        else if(position1.x >= position2.x && position1.y >= position2.y)
            angel = 90 + Mathf.Atan(yValue / xValue) / Mathf.PI * 180;
        else
            angel = -90 - Mathf.Atan(yValue / xValue) / Mathf.PI * 180;
    }

最后画出图的效果如下:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/897897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号