Skip to content

Instantly share code, notes, and snippets.

@if1live
Created October 5, 2016 03:05
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 if1live/74f933175cab3f82fa4ae5fce62be361 to your computer and use it in GitHub Desktop.
Save if1live/74f933175cab3f82fa4ae5fce62be361 to your computer and use it in GitHub Desktop.
EffectGizmo
using UnityEngine;
namespace Assets.GameExternal.Scripts {
public class EffectGizmo : MonoBehaviour {
public enum Types {
Sphere,
DirectionX,
DirectionY,
DirectionZ,
}
public Types shape = Types.DirectionZ;
public Color color = Color.yellow;
public string description = "추가설명";
public bool showWhenPlaying = false;
public float size = 0.5f;
[Range(0.01f, 0.1f)]
public float guidelineScale = 0.1f;
Line[] lines_cache;
void InitializeLineCache() {
if (lines_cache == null) {
lines_cache = new Line[7];
}
}
void OnDrawGizmos() {
bool draw = false;
if (Application.isPlaying) {
if (showWhenPlaying) {
draw = true;
}
} else {
draw = true;
}
if (draw) {
Draw();
}
}
void Draw() {
var prevColor = Gizmos.color;
Gizmos.color = color;
switch (shape) {
case Types.Sphere:
Draw_Sphere();
break;
case Types.DirectionX:
Draw_Direction(transform.right, transform.forward, transform.up);
break;
case Types.DirectionY:
Draw_Direction(transform.up, transform.forward, transform.right);
break;
case Types.DirectionZ:
Draw_Direction(transform.forward, transform.right, transform.up);
break;
default:
break;
}
Gizmos.color = prevColor;
}
void Draw_Sphere() {
Gizmos.DrawSphere(transform.position, size);
}
struct Line {
public Vector3 a;
public Vector3 b;
}
void DrawLines(Line[] lines) {
foreach(var l in lines) {
Gizmos.DrawLine(l.a, l.b);
}
}
void Draw_Direction(Vector3 forward, Vector3 right, Vector3 up) {
var length = size;
var guidelineSize = size * guidelineScale;
var startpos = transform.position;
var endpos = startpos + forward * length;
var leftpos = startpos - right * guidelineSize;
var rightpos = startpos + right * guidelineSize;
var uppos = startpos + up * guidelineSize;
var downpos = startpos - up * guidelineSize;
StartLineConfig();
PushLine(startpos, endpos);
PushLine(leftpos, endpos);
PushLine(rightpos, endpos);
PushLine(uppos, endpos);
PushLine(downpos, endpos);
PushLine(leftpos, rightpos);
PushLine(uppos, downpos);
DrawLines(lines_cache);
}
int currLineIdx = 0;
void StartLineConfig() {
InitializeLineCache();
this.currLineIdx = 0;
}
void PushLine(Vector3 a, Vector3 b) {
lines_cache[currLineIdx].a = a;
lines_cache[currLineIdx].b = b;
currLineIdx += 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment