Skip to content

Instantly share code, notes, and snippets.

@Hyperparticle
Created November 28, 2017 17:24
Show Gist options
  • Save Hyperparticle/a5c65cf1a7570dd515a265cd9364fb02 to your computer and use it in GitHub Desktop.
Save Hyperparticle/a5c65cf1a7570dd515a265cd9364fb02 to your computer and use it in GitHub Desktop.
Draw Controller - Version 2
public class DrawController : MonoBehaviour
{
public DrawMode Mode = DrawMode.Rectangle;
public DrawShape RectanglePrefab;
public DrawShape CirclePrefab;
public DrawShape TrianglePrefab;
// Associates a draw mode to the prefab to instantiate
private Dictionary<DrawMode, DrawShape> _drawModeToPrefab;
private readonly List<DrawShape> _allShapes = new List<DrawShape>();
private DrawShape CurrentShapeToDraw { get; set; }
private bool IsDrawingShape { get; set; }
private void Awake()
{
_drawModeToPrefab = new Dictionary<DrawMode, DrawShape> {
{DrawMode.Rectangle, RectanglePrefab},
{DrawMode.Circle, CirclePrefab},
{DrawMode.Triangle, TrianglePrefab}
};
}
private void Update()
{
// ...
}
private void AddShapeVertex(Vector2 position)
{
if (CurrentShapeToDraw == null) {
// No current shape -> instantiate a new shape and add two vertices:
// one for the initial position, and the other for the current cursor
var prefab = _drawModeToPrefab[Mode];
CurrentShapeToDraw = Instantiate(prefab);
CurrentShapeToDraw.name = "Shape " + _allShapes.Count;
// ...
_allShapes.Add(CurrentShapeToDraw);
} else {
// ...
}
}
private void UpdateShapeVertex(Vector2 position)
{
// ...
}
/// <summary>
/// Controlled via Unity GUI button
/// </summary>
public void SetDrawMode(string mode)
{
Mode = (DrawMode) Enum.Parse(typeof(DrawMode), mode);
}
/// <summary>
/// The types of shapes that can be drawn, useful for
/// selecting shapes to draw
/// </summary>
public enum DrawMode
{
Rectangle,
Circle,
Triangle
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment