Unity3D: Gizmos circle (original)

Source: Internet
Author: User

Unity3D engine technology exchange QQ group: [21568554]

Gizmos is a visual debugging tool in the scenario view. We often use it during the project process, for example, drawing a ray.

As of Unity3D 4.2, only the rendering Ray, line segment, grid sphere, Solid Sphere, grid cube, solid cube, icon, GUI texture, and camera wiremap are available.

If you need to draw a ring, you also need to write your own code.

Using UnityEngine; using System; public class HeGizmosCircle: MonoBehaviour {public Transform m_Transform; public float m_Radius = 1; // the radius of the ring public float m_Theta = 0.1f; // The lower the value, the smoother the ring. public Color m_Color = Color. green; // wiremap color 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; // sets the matrix Matrix4x4 defaultMatrix = Gizmos. matrix; Gizmos. matrix = m_Transform.localToWorldMatrix; // set the Color defaultColor = Gizmos. color; Gizmos. color = m_Color; // plot the ring 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;} // draw the last line segment Gizmos. drawLine (firstPoint, beginPoint); // restores the default Gizmos color. color = defaultColor; // restore the default matrix Gizmos. matrix = defaultMatrix ;}}

Drag the code to a GameObject and associate the Transform of the GameObject. Then, a circle is displayed in the Scene View window.

Adjust the Position, Rotation, and scaling of the circle by adjusting the Position, Rotation, and Scale of the Transform.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.