PostUnityEngine Drawing Class C#

Posts 13 of 3 · Page 1 of 1
UnityEngine Drawing Class C#
Sup guys!
I coded a small Drawing Class for Unity C#:
Just put this into a new .cs file in your Unity Hack.

Code:
/////////////////////// YDrawing.cs //////////////////
using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;


namespace DrawDev
{

    class DrawDevice
    {

        public static Material lineMaterial;
        public static bool intialized = false;
        private static float CorrectY(float value)
        {

            return (Screen.height / 2 - value) + Screen.height / 2;

        }      
        public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color)
        {

            lineMaterial.SetPass(0);
            GL.Begin(1);
            GL.Color(color);
            GL.Vertex3(pointA.x, pointA.y, 0f);
            GL.Vertex3(pointB.x, pointB.y, 0f);
            GL.End();

        }
        public static void DrawRect(Vector2 Point, int width,int height,Color color)
        {            
            DrawLine(Point, new Vector2(Point.x + width, Point.y),color);
            DrawLine(Point, new Vector2(Point.x, Point.y + height), color);
            DrawLine(new Vector2(Point.x + width, Point.y + height), new Vector2(Point.x + width, Point.y), color);
            DrawLine(new Vector2(Point.x + width, Point.y + height), new Vector2(Point.x, Point.y + height), color);
        }
        public static void DrawTriangle(Vector2 Point, int left, int right, int height, Color color)
        {

            DrawLine(Point, new Vector2(Point.x - left, Point.y + height), color);
            DrawLine(Point, new Vector2(Point.x + right, Point.y + height), color);
            DrawLine(new Vector2(Point.x - left, Point.y + height), new Vector2(Point.x + right, Point.y + height), color);

        }
        public static void DrawCross(Vector2 Point, int width, int height, Color color)
        {

            DrawLine(new Vector2(Point.x - width, Point.y), new Vector2(Point.x + width, Point.y),color);
            DrawLine(new Vector2(Point.x, Point.y - height), new Vector2(Point.x, Point.y + height), color);

        }
        public static void DrawCircle(Vector2 Point, int radius, Color color)
        {

             double PI = 3.1415926535;
             double i, x1, y1;
             double angle;
             

             for (i = 0; i < 360; i += 1)
             {
                 angle = i;
                 x1 = radius * Math.Cos(angle * PI / 180);
                 y1 = radius * Math.Sin(angle * PI / 180);
                 DrawLine(new Vector2(Point.x + Convert.ToSingle(x1), Point.y + Convert.ToSingle(y1)),new Vector2(Point.x + 1 + Convert.ToSingle(x1), Point.y + 1 + Convert.ToSingle(y1)),color);
             }
             

        }
        public static void DrawFont(Vector2 Point,int width,int height, string txt,Color color)
        {
            Color old = GUI.contentColor;
            GUI.contentColor = color;
            GUI.Label(new Rect(Point.x, Point.y, width, height), txt);
            GUI.contentColor = old;
        }
        public static void FillRGB(Vector2 point, float width, float height, Color color, float alpha)
        {

            Texture2D rgb = new Texture2D(1, 1);

            Color fillColor = color;
            fillColor.a = alpha;
            var fillColorArray =  rgb.GetPixels();

            for (var i = 0; i < fillColorArray.Length; ++i)
            {
                fillColorArray[i] = fillColor;
            }

            rgb.SetPixels(fillColorArray);
            rgb.Apply();
            GUI.DrawTexture(new Rect(point.x, point.y, width, height), rgb);

            
            
        } //Sometimes game crashes for me
        
        public static void Intialize()
        {

            lineMaterial = new Material("Shader \"Lines/Colored Blended\" {SubShader { Pass {   BindChannels { Bind \"Color\",color }   Blend SrcAlpha OneMinusSrcAlpha   ZWrite Off Cull Off Fog { Mode Off }} } }");
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;

            intialized = true;

        }

    }

}
How to initialize:
Code:
if (DrawDevice.intialized == false) DrawDevice.Intialize();
How to use:
Code:
using DrawDev;
DrawDevice.DrawLine(new Vector2(20,20), new Vector2(200,200), Color.Red);
Screenshot:
Posts 13 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?