Unity3d 는 Gizmos 를 이용해서 원 을 그 려 요.

Gizmos 는 장면 보기 의 시각 화 디 버 깅 도구 입 니 다.
프로젝트 를 하 는 과정 에서우 리 는 방사선 을 그 리 는 등 그것 을 자주 사용한다.
Unity3D 4.2 버 전 번호 까지 입 니 다.현재 방사선,선분,격자 구체,실체 구체,격자 입방체,실체 입방체,아이콘 만 제공 합 니 다.GUI 텍 스 처 와 카메라 프레임.
만약 에 링 을 그 려 야 한다 고 가정 하면 코드 를 직접 써 야 한다.

using UnityEngine;
using System;
public class HeGizmosCircle : MonoBehaviour
{
       public Transform m_Transform;
       public float m_Radius = 1; //      
       public float m_Theta = 0.1f; //         
       public Color m_Color = Color.green; //     
       
       void Start()
       {
              if (m_Transform == null)
              {
                     throw new Exception("Transform is NULL.");
              }
       }
       void OnDrawGizmos()
       {
              if (m_Transform == null) return;
              if (m_Theta < 0.0001f) m_Theta = 0.0001f;
              //     
              Matrix4x4 defaultMatrix = Gizmos.matrix;
              Gizmos.matrix = m_Transform.localToWorldMatrix;
              //     
              Color defaultColor = Gizmos.color;
              Gizmos.color = m_Color;
              //     
              Vector3 beginPoint = Vector3.zero;
              Vector3 firstPoint = Vector3.zero;
              for (float theta = 0; theta < 2 * Mathf.PI; theta += m_Theta)
              {
                     float x = m_Radius * Mathf.Cos(theta);
                     float z = m_Radius * Mathf.Sin(theta);
                     Vector3 endPoint = new Vector3(x, 0, z);
                     if (theta == 0)
                     {
                            firstPoint = endPoint;
                     }
                     else
                     {
                            Gizmos.DrawLine(beginPoint, endPoint);
                     }
                     beginPoint = endPoint;
              }
              //         
              Gizmos.DrawLine(firstPoint, beginPoint);
              //       
              Gizmos.color = defaultColor;
              //       
              Gizmos.matrix = defaultMatrix;
       }
}
코드 를 GameObject 에 끌 어 다 놓 으 면 이 GameObject 의 Transform 과 연 결 된 다음 Scene 보기 창 에 원 을 표시 할 수 있 습 니 다.


Transform 의 포지션 조정 을 통 해Rotation。Scale,원 의 위 치 를 조정 하고 회전 하 며 크기 를 조정 합 니 다.
추가:Unity3D 기반 LineRender 구성 요소 로 원 선 그리 기
Unity3D 의 LineRender 를 사용 하여 선 을 그 리 는 과정 을 기록 하고 LineRender 와 OpenGL 의 GL 테스트 를 거 쳤 습 니 다.LINE_STRIP 그리 기 방식 이 같 기 때문에 점 을 계산 한 후에 시작 점 을 종점 으로 하고 한 점 을 더 계산 해 야 닫 힌 다.
코드 는 다음 과 같 습 니 다:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; 
public class DrawLines: MonoBehaviour
{
    public float m_radius = 1.0f;
    public Material m_material;
    public float m_lineWidth = 1.0f;
    private List<Vector3> vPath = new List<Vector3>();
    // Start is called before the first frame update
    void Start()
    {
        int count = 60;
       for (int i=1; i<= (count+1); i++)
        {
            if(i == (count+1))
            {
                float x = Mathf.Cos(2 * Mathf.PI / count) * m_radius;
                float y = transform.localPosition.y;
                float z = Mathf.Sin(2 * Mathf.PI / count) * m_radius;
                vPath.Add(new Vector3(x, y, z));
            }
            else
            {
                float x = Mathf.Cos(2 * Mathf.PI / count * i) * m_radius;
                float y = transform.localPosition.y;
                float z = Mathf.Sin(2 * Mathf.PI / count * i) * m_radius;
                vPath.Add(new Vector3(x, y, z));
            } 
        } 
        GameObject lineGroup = new GameObject("LineGroup");
        GameObject lineObject = new GameObject("RadarLine");
        LineRenderer line = lineObject.AddComponent<LineRenderer>();
        line.material = m_material;
        line.useWorldSpace = false;
        line.positionCount = vPath.Count;
        line.startWidth = m_lineWidth;
        line.endWidth = m_lineWidth;
        line.SetPositions(vPath.ToArray()); 
    } 
    // Update is called once per frame
    void Update()
    {        
    }
}
실행 해 보 세 요.효과 보기:

이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기