Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class LineDraw : MonoBehaviour
- {
- static LineDraw instance = null;
- float widthStart = 0.1f;
- float widthFinish = 0.1f;
- ShaderOptionEnum shader = ShaderOptionEnum.SelfIlluminDiffuse;
- Color color = Color.red;
- string[] shaders = {"Self-Illumin/Diffuse", "Particles/Additive"};//, "VertexLit", "Diffuse"};
- public enum ShaderOptionEnum {SelfIlluminDiffuse = 0, ParticlesAdditive};
- //public class LineData{Vector3 a, b; float s, f; ShaderOptionEnum shader; Color c1, c2;}
- public void SetWidth_(float widthStart, float widthFinish)
- {
- this.widthStart = widthStart;
- this.widthFinish = widthFinish;
- }
- public void SetShaderOption_(ShaderOptionEnum shader)
- {
- this.shader = shader;
- }
- public void SetColor_(Color color)
- {
- this.color = color;
- }
- public static void SetShaderOption(ShaderOptionEnum shader){instance.SetShaderOption_(shader);}
- public static void SetColor(Color color){instance.SetColor_(color);}
- public static void SetWidth(float widthStart, float widthFinish){instance.SetWidth_(widthStart, widthFinish);}
- List<GameObject> temp = new List<GameObject>();
- List<GameObject> unusedTemp = new List<GameObject>();
- void LateUpdate()
- {
- for(int i = 0; i < temp.Count; ++i)
- {
- temp[i].active = false;
- unusedTemp.Add(temp[i]);
- }
- temp.Clear();
- // t += Time.deltaTime;
- // if(t >= 360) t-=360;
- // Quaternion q = Quaternion.AngleAxis(t * 360, Vector3.forward);
- // Vector3 v = q * new Vector3(-1,-1,-1);
- // LineDraw.AddTemporary(Vector3.zero, v);
- }
- // float t = 0;
- void Start()
- {
- if(instance == null)
- instance = this;
- // GameObject g = LineDraw.AddPersistent(new Vector3(-1,-1,-2), new Vector3(5,1,-2));
- // Destroy(g, 3);
- }
- GameObject GetLineGameObject()
- {
- GameObject go;
- if(unusedTemp.Count > 0)
- {
- int lastIndex = unusedTemp.Count-1;
- go = unusedTemp[lastIndex];
- unusedTemp.RemoveAt(lastIndex);
- go.active = true;
- }else{
- go = new GameObject("line");
- go.AddComponent<LineRenderer>();
- }
- go.transform.parent = transform;
- return go;
- }
- public GameObject AddLine_(Vector3 a, Vector3 b, bool persistent)
- {
- int numPoints = 2;
- GameObject lineg = GetLineGameObject();
- LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
- string shaderName = shaders[(int)shader];
- if(lineRenderer.material.shader.name != shaderName)
- {
- UnityEngine.Shader shaderObject = Shader.Find(shaderName);
- lineRenderer.material = new Material(shaderObject);
- }
- lineRenderer.material.color = color;
- lineRenderer.castShadows = false;
- lineRenderer.receiveShadows = false;
- lineRenderer.SetColors(color, color);
- lineRenderer.SetWidth(widthStart, widthFinish);
- lineRenderer.SetVertexCount(numPoints);
- lineRenderer.SetPosition(0, a);
- lineRenderer.SetPosition(1, b);
- //lineg.transform.position = (a+b)/2;
- if(!persistent)
- {
- temp.Add(lineg);
- }
- return lineg;
- }
- public static GameObject AddPersistent(Vector3 a, Vector3 b){return instance.AddLine_(a, b, true);}
- public static void AddTemporary(Vector3 a, Vector3 b){instance.AddLine_(a, b, false);}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement