Unity 使用LineRender画物体的立方体框

使用LineRender组件画物体的包围盒线框
转载:Unity学习——LineRender画物体的立方体框_Alxes_七局的博客-CSDN博客

 public Material Mater;
    public Material Mater1;
    private LineRenderer line;
    
    //8个顶点
    #region MyRegion
    //private Vector3 v1 = new Vector3(-0.5f, -0.5f, 0.5f);
    //private Vector3 v2 = new Vector3(0.5f, -0.5f, 0.5f);
    //private Vector3 v3 = new Vector3(0.5f, -0.5f, -0.5f);
    //private Vector3 v4 = new Vector3(-0.5f, -0.5f, -0.5f);
    //private Vector3 v5 = new Vector3(-0.5f, 0.5f, 0.5f);
    //private Vector3 v6 = new Vector3(0.5f, 0.5f, 0.5f);
    //private Vector3 v7 = new Vector3(0.5f, 0.5f, -0.5f);
    //private Vector3 v8 = new Vector3(-0.5f, 0.5f, -0.5f); 
    #endregion
    private Bounds bounds;
 
    void Start ()
    {
        bounds = GetComponent<SkinnedMeshRenderer>().bounds;
        line = this.gameObject.AddComponent<LineRenderer>();
        line.enabled = false;
    }
	void Update ()
    {
        if (Input.GetMouseButton(0))
        {
            DrawCube(bounds, Mater, true);
        }
        if (Input.GetMouseButton(1))
        {
            DrawCube(bounds, Mater, false);
        }
    }
    void DrawLines()//GameObject selectedBrick, int style)
    {
        //获得8个点的参数
 
        //line.SetPosition(0, v1);
        //line.SetPosition(1, v2);
        //line.SetPosition(2, v3);
        //line.SetPosition(3, v4);
        //line.SetPosition(4, v1);
        //line.SetPosition(5, v5);
        //line.SetPosition(6, v6);
        //line.SetPosition(7, v7);
        //line.SetPosition(8, v8);
        //line.SetPosition(9, v5);
        //line.SetPosition(10, v8);
        //line.SetPosition(11, v4);
        //line.SetPosition(12, v3);
        //line.SetPosition(13, v7);
        //line.SetPosition(14, v6);
        //line.SetPosition(15, v2);
    }
    
    void DrawCube(Bounds bound, Material mat, bool DrawOrDel)
    {
        #region 以Bound求8个顶点
        Vector3 centerPOs = bound.center;//中心点
        Vector3 extens = bound.size / 2;
        Vector3 MaxPos = centerPOs + extens;//最大顶点
        Vector3 MinPos = centerPOs - extens;//最小顶点
        //8个点顶点
        Vector3 A = new Vector3(MinPos.x, MaxPos.y, MinPos.z);
        Vector3 B = new Vector3(MinPos.x, MaxPos.y, MaxPos.z);
        Vector3 C = new Vector3(MaxPos.x, MaxPos.y, MaxPos.z);
        Vector3 D = new Vector3(MaxPos.x, MaxPos.y, MinPos.z);
        Vector3 A1 = new Vector3(MinPos.x, MinPos.y, MinPos.z);
        Vector3 B1 = new Vector3(MinPos.x, MinPos.y, MaxPos.z);
        Vector3 C1 = new Vector3(MaxPos.x, MinPos.y, MaxPos.z);
        Vector3 D1 = new Vector3(MaxPos.x, MinPos.y, MinPos.z); 
        #endregion
        
        if (DrawOrDel)//Bool为true时,显示画框颜色
        {
            line.enabled = true;
            line.material = mat;
        }
        else //Bool为False时,清除颜色
        {
            line.enabled = false;
            //line.material= mat;
        }
      
        //设置线段数量
        line.SetVertexCount(16);
        //线的宽度
        line.SetWidth(0.01f, 0.01f);
        //根据8点画的Line;
        line.SetPosition(0, B1);//前左下
        line.SetPosition(1, C1);//前右下
        line.SetPosition(2, D1);//后右下
        line.SetPosition(3, A1);//后左下
        line.SetPosition(4, B1);
        line.SetPosition(5, B);//前左上
        line.SetPosition(6, C);//前右上
        line.SetPosition(7, D);//后右上
        line.SetPosition(8, A);//后左上
        line.SetPosition(9, B);
        line.SetPosition(10, A);
        line.SetPosition(11, A1);
        line.SetPosition(12, D1);
        line.SetPosition(13, D);
        line.SetPosition(14, C);
        line.SetPosition(15, C1);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值