Home > Software design >  How to draw on the boxcollider outlines in the scene view window in red and in the game view window
How to draw on the boxcollider outlines in the scene view window in red and in the game view window

Time:06-10

A small helper script for coloring the box collider in runtime in scene view and game view even if not selecting the gameobject with the collider.

the problem or another feature i want to add is when selecting in the enum the mode DrawOnBoth that it will draw in the scene view window in red and in the game view window in green.

Now when the mode is DrawOnBoth both windows scene view and game view are drawn in green color.

Collider drawn with colors

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class DrawOnBoxCollider : MonoBehaviour
{
    public enum DrawStates
    {
        DrawOnGizmosRuntime,
        DrawOnGameviewRuntime,
        DrawOnBoth

    }

    public DrawStates drawingStates;
    public Transform boxColliderToDrawOn;

    private List<LineRenderer> lines = new List<LineRenderer>();
    private BoxCollider boxCollider = null;
    private bool drawOnGizmos = false;
    private LineRenderer line;
    private bool drawOnce = true;

    // Start is called before the first frame update
    void Start()
    {
        if (boxColliderToDrawOn != null)
        {
            boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (boxColliderToDrawOn != null)
        {
            switch (drawingStates)
            {
                case DrawStates.DrawOnGameviewRuntime:
                    if (line != null)
                    {
                        line.enabled = true;
                    }
                    drawOnGizmos = false;
                    if (drawOnce)
                    {
                        if (boxCollider == null)
                        {
                            boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
                        }
                        DrawOnGameViewRuntime();
                        drawOnce = false;
                        drawOnGizmos = false;
                    }
                    break;

                case DrawStates.DrawOnGizmosRuntime:
                    drawOnce = true;
                    if (line != null && drawOnGizmos == false)
                    {
                        var drawns = transform.GetComponentsInChildren<Transform>().Skip(1);
                        if (drawns.Count() > 0)
                        {
                            foreach (Transform drawn in drawns)
                            {
                                Destroy(drawn.gameObject);
                            }
                        }
                    }
                    if (boxCollider == null)
                    {
                        boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
                    }
                    drawOnGizmos = true;
                    break;

                case DrawStates.DrawOnBoth:
                    if (line != null)
                    {
                        line.enabled = true;
                    }
                    if (boxCollider == null)
                    {
                        boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
                    }
                    if(drawOnce)
                    {
                        DrawOnGameViewRuntime();
                        drawOnce = false;
                        drawOnGizmos = true;
                    }
                    break;
            }
        }
    }

    private void DrawGizmosOnRunTime(Color color)
    {
        if (boxCollider != null && drawOnGizmos)
        {
            Gizmos.color = color;
            Matrix4x4 rotationMatrix = Matrix4x4.TRS(boxCollider.transform.position, boxCollider.transform.rotation, boxCollider.transform.lossyScale);
            Gizmos.matrix = rotationMatrix;
            Gizmos.DrawWireCube(boxCollider.center, boxCollider.size);
        }
    }

    private void OnDrawGizmos()
    {
        DrawGizmosOnRunTime(Color.red);
    }

    private void DrawOnGameViewRuntime()
    {
        Material material = new Material(Shader.Find("Unlit/Color"));
        Color color = Color.green;
        material.color = color;
        float width = 0.1f;
        Vector3 rightDir = boxCollider.transform.right.normalized;
        Vector3 forwardDir = boxCollider.transform.forward.normalized;
        Vector3 upDir = boxCollider.transform.up.normalized;
        Vector3 center = boxCollider.transform.position   boxCollider.center;
        Vector3 size = boxCollider.size;
        size.x *= boxCollider.transform.lossyScale.x;
        size.y *= boxCollider.transform.lossyScale.y;
        size.z *= boxCollider.transform.lossyScale.z;
        DrawLine(center   upDir * size.y / 2f   rightDir * size.x / 2f   forwardDir * size.z / 2f, center   upDir * size.y / 2f - rightDir * size.x / 2f   forwardDir * size.z / 2f, color, material, width);
        DrawLine(center - upDir * size.y / 2f   rightDir * size.x / 2f   forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f   forwardDir * size.z / 2f, color, material, width);
        DrawLine(center   upDir * size.y / 2f   rightDir * size.x / 2f   forwardDir * size.z / 2f, center - upDir * size.y / 2f   rightDir * size.x / 2f   forwardDir * size.z / 2f, color, material, width);
        DrawLine(center   upDir * size.y / 2f - rightDir * size.x / 2f   forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f   forwardDir * size.z / 2f, color, material, width);
        DrawLine(center   upDir * size.y / 2f   rightDir * size.x / 2f - forwardDir * size.z / 2f, center   upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
        DrawLine(center - upDir * size.y / 2f   rightDir * size.x / 2f - forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
        DrawLine(center   upDir * size.y / 2f   rightDir * size.x / 2f - forwardDir * size.z / 2f, center - upDir * size.y / 2f   rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
        DrawLine(center   upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
        DrawLine(center   upDir * size.y / 2f   rightDir * size.x / 2f   forwardDir * size.z / 2f, center   upDir * size.y / 2f   rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
        DrawLine(center - upDir * size.y / 2f   rightDir * size.x / 2f   forwardDir * size.z / 2f, center - upDir * size.y / 2f   rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
        DrawLine(center   upDir * size.y / 2f - rightDir * size.x / 2f   forwardDir * size.z / 2f, center   upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
        DrawLine(center - upDir * size.y / 2f - rightDir * size.x / 2f   forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
    }

    private void DrawLine(Vector3 start, Vector3 end, Color color, Material material, float width = 0.01f)
    {
        line = new GameObject("Line_"   start.ToString()   "_"   end.ToString()).AddComponent<LineRenderer>();
        line.name = "LineRenderer Draw Line";
        line.material = material;
        line.startColor = color;
        line.endColor = color;
        line.startWidth = width;
        line.endWidth = width;
        line.positionCount = 2;
        line.useWorldSpace = true;
        line.SetPosition(0, start);
        line.SetPosition(1, end);
        line.transform.SetParent(transform);
        lines.Add(line);
    }

    public void SetLinesColor(Color color)
    {
        for (int i = 0; i < lines.Count; i  )
        {
            lines[i].material.color = color;
            lines[i].startColor = color;
            lines[i].endColor = color;
        }
    }
}

CodePudding user response:

The below condition separates SceneView and GameView. If the above codes are not a problem, the answer is this.

DrawGizmosOnRunTime(SceneView.currentDrawingSceneView() ? Color.red : Color.green);
  • Related