Draw a Circle on the Screen Unity
- Spaces
- Default
- Assist Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Habitation /
0
Depict circle
How to draw a circle at position of an object just like in RTS when you select units u have circles around selected units. Please explain step by stride and from A to Z
2 Replies
· Add together your reply
- Sort:
1
Answer by Cherno · Apr 22, 2015 at 01:31 PM
The Unity wiki has a script "Drawing.cs", there's also a thread on the forums. I modified it a little:
using System.Reflection; using UnityEngine; // Line drawing routine originally courtesy of Linusmartensson: // http://forum.unity3d.com/threads/71979-Drawing-lines-in-the-editor // // Rewritten to ameliorate performance by Yossarian Rex / August 2013. // // This version produces nearly identical results to the original (tested past drawing // ane over the other and observing errors of one pixel or less), but for big numbers // of lines this version is more than than four times faster than the original, and comes // within almost seventy% of the raw operation of Graphics.DrawTexture. // // Acme operation on my laptop is effectually 200,000 lines per second. The laptop is // Windows vii 64-scrap, Intel Core2 Duo CPU 2.53GHz, 4G RAM, NVIDIA GeForce GT 220M. // Line width and anti-aliasing had negligible bear on on performance. // // For a graph of benchmark results in a standalone Windows build, see this epitome: // https://app.box.com/south/hyuhi565dtolqdm97e00 // // For a Google spreadsheet with full benchmark results, run into: // https://docs.google.com/spreadsheet/ccc?key=0AvJlJlbRO26VdHhzeHNRMVF2UHZHMXFCTVFZN011V1E&usp=sharing public static class Drawing { private static Texture2D aaLineTex = goose egg; private static Texture2D lineTex = zero; private static Cloth blitMaterial = null; individual static Material blendMaterial = nada; private static Rect lineRect = new Rect(0, 0, 1, 1); // Describe a line in screen space, suitable for employ from OnGUI calls from either // MonoBehaviour or EditorWindow. Note that this should but be called during repaint // events, when (Event.electric current.blazon == EventType.Repaint). // // Works by computing a matrix that transforms a unit square -- Rect(0,0,1,1) -- into // a scaled, rotated, and offset rectangle that corresponds to the line and its width. // A DrawTexture telephone call used to draw a line texture into the transformed rectangle. // // More specifically: // scale ten past line length, y past line width // rotate around z past the angle of the line // outset by the position of the upper left corner of the target rectangle // // Past working out the matrices and applying some trigonometry, the matrix calculation comes // out pretty simple. See https://app.box.com/s/xi08ow8o8ujymazg100j for a picture of my // notebook with the calculations. public static void DrawLine(Vector2 pointA, Vector2 pointB, Color colour, bladder width, bool antiAlias) { // Normally the static initializer does this, but to handle texture reinitialization // after editor play mode stops nosotros need this check in the Editor. #if UNITY_EDITOR if (!lineTex) { Initialize(); }
There's no born functionality for drawing circles in Unity. You are left with a bunch of choices that you need to research:
You may be able to use a circle texture. If the objects you lot are circling don't vary in screen size too much, you can create a circle texture in Photoshop. Depending on the utilise, you lot can display the texture using a Quad, GUITexture, or GUI.DrawTexture().
You lot can create an array of round points and draw a line using the line renderer or an improver packet like Vectorosity.
You could elect to highlight not by circling but by changing the material or texture in some way, or by using a shader that outlines the object.
Y'all could write a shader that draws a circle mathematically.
You could create a round (donut-hole) mesh in a modeling plan and size and position the mesh to circle an object.
Give thanks You lot. I gauge the first choice would be the easiest, Writing a shader will exist a pain in ... ;)
And some other option might be projectors. Drawing circles in Unity has been asked and answered before in a diverseness of ways. Do some searching to figure out what is the best fit for your specific app.