zoukankan      html  css  js  c++  java
  • Unity 使用image绘制线段 直线

    一个类即可

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ImageLine : MonoBehaviour
    {
        //线条宽度
        public float lineWidth = 10;
    
        private Transform pos1;
        private Transform pos2;
        
        
        public void SetLine(Vector3 v1, Vector3 v2)
        {
            gameObject.SetActive(true);
            Vector3 mid = (v1 - v2) / 2;
            GetComponent<RectTransform>().anchoredPosition = mid;
            GetComponent<RectTransform>().sizeDelta = new Vector2(Vector3.Distance(v1, v2), lineWidth);
            //最后一个 Vector3.forward 控制方向正负,加负号可逆转方向
            GetComponent<RectTransform>().rotation = Quaternion.AngleAxis(Vector3.Angle(mid, Vector3.right),Vector3.forward);
        }
    
        //设置线段起点和终点(一般调用这个即可)
        public void SetLine(Transform t1, Transform t2)
        {
            pos1 = t1;
            pos2 = t2;
            Vector3 v1 = t1.transform.position;
            Vector3 v2 = t2.transform.position;
            SetLine(v1, v2);
        }
    
        //重新调整线段
        public void ResetLine()
        {
            if (pos1 != null && pos2 != null)
            {
                SetLine(pos1, pos2);
            }
        }
    }
  • 相关阅读:
    KBMMW 4.80.00 发布
    RCF库ClientStub.setAutoReconnect
    编译 boost
    2017学习计划
    _beginthreadex注意事项
    push_back模式工作
    总结2016
    <转>好婚姻是彼此放心
    ProcessExplore 最新版
    网站seo新手快速提升自己的技巧
  • 原文地址:https://www.cnblogs.com/sanyejun/p/14256133.html
Copyright © 2011-2022 走看看