Skip to content

Instantly share code, notes, and snippets.

@bhupiister
Created June 1, 2020 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhupiister/7bc11c98ad50c7c64ed635bac08734bd to your computer and use it in GitHub Desktop.
Save bhupiister/7bc11c98ad50c7c64ed635bac08734bd to your computer and use it in GitHub Desktop.
DrawLines
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Handles line drawing. Stores data for a line.
/// </summary>
[RequireComponent(typeof(SpriteRenderer))]
public class Line : MonoBehaviour
{
/// <summary>
/// Start position in world space. Calculates new line position when value changed.
/// </summary>
/// <value>The start position.</value>
public Vector2 start
{
get
{
return startPosition;
}
set
{
startPosition = value;
UpdatePosition();
}
}
/// <summary>
/// End position in world space. Calculates new line position when value changed.
/// </summary>
/// <value>The end position.</value>
public Vector2 end
{
get
{
return endPosition;
}
set
{
endPosition = value;
UpdatePosition();
}
}
/// <summary>
/// Width of line. Updates sprite renderer when value changed.
/// </summary>
/// <value>The width.</value>
public float width
{
get { return lineWidth; }
set
{
lineWidth = value;
UpdateWidth();
}
}
/// <summary>
/// Line color. Updates sprite renderer color when value changed.
/// </summary>
/// <value>The color.</value>
public Color color
{
get { return lineColor; }
set
{
lineColor = value;
UpdateColor();
}
}
private Vector2 startPosition;
private Vector2 endPosition;
private float lineWidth;
private Color lineColor;
private int layerOrder;
private SpriteRenderer lineRenderer;
public float dividingFactor = 2;
public float scale = 1;
float scalex;
float scaley;
float scaleXY;
void Awake()
{
lineRenderer = GetComponent<SpriteRenderer>();
}
private void Start()
{
}
/// <summary>
/// Initialise the specified start, end, width and color of sprite.
/// </summary>
/// <param name="start">Start position.</param>
/// <param name="end">End position.</param>
/// <param name="width">Width.</param>
/// <param name="color">Color.</param>
public void Initialise(Vector2 start, Vector2 end, float width, Color color, float scale,int layer)
{
startPosition = start;
endPosition = end;
lineWidth = width;
lineColor = color;
scaleXY = scale;
layerOrder = layer;
UpdatePosition();
UpdateWidth();
UpdateColor();
Updatelayer();
}
private void UpdatePosition()
{
var heading = endPosition - startPosition;
var distance = heading.magnitude;
var direction = heading / distance;
Vector3 centerPos = new Vector3((startPosition.x + endPosition.x) / scaleXY, (startPosition.y + endPosition.y) / scaleXY) / dividingFactor;
lineRenderer.transform.position = centerPos;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
lineRenderer.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
var objectWidthSize = 10f / 5f; // 10 = pixels of line sprite, 5f = pixels per units of line sprite.
lineRenderer.transform.localScale = new Vector3(distance / objectWidthSize, width, lineRenderer.transform.localScale.z);
}
private void UpdateWidth()
{
lineRenderer.transform.localScale = lineRenderer.transform.localScale.WithY(lineWidth);
}
private void UpdateColor()
{
lineRenderer.color = lineColor;
}
private void Updatelayer()
{
lineRenderer.sortingOrder = layerOrder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment