Giter VIP home page Giter VIP logo

gizmos's Introduction

Ah, gl lines

Gizmos

Used for drawing runtime gizmos in builds and editor from any context in the code.

Requirements

  • .NET Framework 4.5
  • Git

Installation

To install for use in Unity, copy everything from this repository to <YourUnityProject>/Packages/Popcron.Gizmos folder.

If using 2018.3.x or higher, you can add a new entry to the manifest.json file in your Packages folder:

"com.popcron.gizmos": "https://github.com/popcron/gizmos.git"

Example

using UnityEngine;
using Gizmos = Popcron.Gizmos;

[ExecuteAlways]
public class GizmoDrawer : MonoBehaviour
{
    public Material material = null;

    //this will draw in scene view and in game view, in both play mode and edit mode
    private void OnRenderObject()
    {
        //draw a line from the position of the object, to world center
        //with the color green, and dashed as well
        Gizmos.Line(transform.position, Vector3.one, Color.green, true);

        //draw a cube at the position of the object, with the correct rotation and scale
        Gizmos.Cube(transform.position, transform.rotation, transform.lossyScale);
    }

    private void Update()
    {
        //use custom material, if null it uses a default line material
        Gizmos.Material = material;

        //toggle gizmo drawing using the same key as in minecraft
        if (Input.GetKeyDown(KeyCode.F3))
        {
            Gizmos.Enabled = !Gizmos.Enabled;
        }
        
        //can also draw from update
        Gizmos.Cone(transform.position, transform.rotation, 15f, 45f, Color.green);
    }
}

Custom drawers

The ability to add custom drawers is possible. Inherit from the Drawer class and implement the Draw method. To see an example of drawing a line using a custom drawer, look at the LineDrawer.cs file.

The Draw method takes in a ref parameter to a Vector3 array as the buffer, and then a params array of objects. The method expects to return the number of points allocated to the buffer. For example, if renderering a single line, allocate the two points at buffer[0] and buffer[1], and return 2. If the number returned is not the same as the amount of points actually used, then the end result of the drawn element will look incorrect and corrupt.

API

  • Gizmos.Line = Draws a line from point a to b. Equivalent to Gizmos.DrawLine
  • Gizmos.Square = Draws a 2D square in the XY plane
  • Gizmos.Cube = Draws a 3D cube in world space with orientation and scale parameters. Equivalent to Gizmos.DrawWireCube
  • Gizmos.Bounds = Draws a representation of a Bounds object
  • Gizmos.Cone = Draws a cone with specified orientation, length and angle
  • Gizmos.Sphere = Draws a 3D sphere. Equivalent to Gizmos.DrawWireSphere
  • Gizmos.Circle = Draws a 2D circle that is oriented to the camera by default

Notes

The package uses the same class name as the built-in gizmo class, because of this, you will need to use an alias to point to the right class (using Gizmos = Popcron.Gizmos).

The gizmos will only be processed on the scene view camera, and the default MainCamera. To change this, you can specify using the static property for Camera in the Gizmo class:

//draw for this camera instead of using the MainCamera
Gizmos.Camera = myCamera;

The alternative is to make a global class with the same name that redirects all of its calls to Popcron.Gizmos. The downside to this is that you will need to be explicit when calling the UnityEngine.Gizmos class. Choose your poison.

FAQ

  • What namespace? 'Popcron'
  • Does it work in builds? Yes
  • Is there frustum culling? Yes

gizmos's People

Contributors

popcron avatar runninglvlan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.