Guest User

Untitled

a guest
Feb 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. public class ShowMeshBounds : MonoBehaviour
  7. {
  8. public GameObject prefabEffect;
  9. public Color color = Color.green;
  10.  
  11. private Vector3 v3FrontTopLeft;
  12. private Vector3 v3FrontTopRight;
  13. private Vector3 v3FrontBottomLeft;
  14. private Vector3 v3FrontBottomRight;
  15. private Vector3 v3BackTopLeft;
  16. private Vector3 v3BackTopRight;
  17. private Vector3 v3BackBottomLeft;
  18. private Vector3 v3BackBottomRight;
  19.  
  20. private float counter = 0;
  21. public bool animateLines;
  22. public float speed = 1f;
  23.  
  24. private List<GameObject> allLines = new List<GameObject>();
  25. private List<GameObject> instancesToMove = new List<GameObject>();
  26. private Vector3 endPos;
  27.  
  28. private void Start()
  29. {
  30. CalcPositons();
  31. DrawBox();
  32. allLines = GameObject.FindGameObjectsWithTag("FrameLine").ToList();
  33.  
  34. DuplicatePrefabEffects(allLines.Count);
  35. instancesToMove = GameObject.FindGameObjectsWithTag("Duplicated Prefab").ToList();
  36.  
  37. StartCoroutine(moveStuff());
  38. }
  39.  
  40. private void DuplicatePrefabEffects(int duplicationNumber)
  41. {
  42. for (int i = 0; i < duplicationNumber; i++)
  43. {
  44. var go = Instantiate(prefabEffect);
  45. go.tag = "Duplicated Prefab";
  46. go.name = "Duplicated Prefab";
  47. }
  48. }
  49.  
  50. void CalcPositons()
  51. {
  52. Bounds bounds = GetComponent<MeshFilter>().sharedMesh.bounds;
  53.  
  54. Vector3 v3Center = bounds.center;
  55. Vector3 v3Extents = bounds.extents;
  56.  
  57. v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
  58. v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
  59. v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
  60. v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
  61. v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
  62. v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
  63. v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
  64. v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
  65.  
  66. v3FrontTopLeft = transform.TransformPoint(v3FrontTopLeft);
  67. v3FrontTopRight = transform.TransformPoint(v3FrontTopRight);
  68. v3FrontBottomLeft = transform.TransformPoint(v3FrontBottomLeft);
  69. v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
  70. v3BackTopLeft = transform.TransformPoint(v3BackTopLeft);
  71. v3BackTopRight = transform.TransformPoint(v3BackTopRight);
  72. v3BackBottomLeft = transform.TransformPoint(v3BackBottomLeft);
  73. v3BackBottomRight = transform.TransformPoint(v3BackBottomRight);
  74. }
  75.  
  76. void DrawBox()
  77. {
  78. SpawnLineGenerator(v3FrontTopLeft, v3FrontTopRight, color);
  79. SpawnLineGenerator(v3FrontTopRight, v3FrontBottomRight, color);
  80. SpawnLineGenerator(v3FrontBottomRight, v3FrontBottomLeft, color);
  81. SpawnLineGenerator(v3FrontBottomLeft, v3FrontTopLeft, color);
  82.  
  83. SpawnLineGenerator(v3BackTopLeft, v3BackTopRight, color);
  84. SpawnLineGenerator(v3BackTopRight, v3BackBottomRight, color);
  85. SpawnLineGenerator(v3BackBottomRight, v3BackBottomLeft, color);
  86. SpawnLineGenerator(v3BackBottomLeft, v3BackTopLeft, color);
  87.  
  88. SpawnLineGenerator(v3FrontTopLeft, v3BackTopLeft, color);
  89. SpawnLineGenerator(v3FrontTopRight, v3BackTopRight, color);
  90. SpawnLineGenerator(v3FrontBottomRight, v3BackBottomRight, color);
  91. SpawnLineGenerator(v3FrontBottomLeft, v3BackBottomLeft, color);
  92. }
  93.  
  94. void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
  95. {
  96. GameObject myLine = new GameObject();
  97.  
  98. myLine.tag = "FrameLine";
  99. myLine.name = "FrameLine";
  100.  
  101. myLine.AddComponent<LineRenderer>();
  102. myLine.AddComponent<EndHolder>();
  103. myLine.GetComponent<EndHolder>().EndVector = end;
  104. LineRenderer lr = myLine.GetComponent<LineRenderer>();
  105. lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
  106. lr.startColor = color;
  107. lr.useWorldSpace = false;
  108. lr.endColor = color;
  109. lr.startWidth = 0.1f;//0.03f;
  110. lr.endWidth = 0.1f;//0.03f;
  111. lr.SetPosition(0, start);
  112. lr.SetPosition(1, start);
  113. }
  114.  
  115. IEnumerator moveStuff()
  116. {
  117. for (int i = 0; i < allLines.Count; i++)
  118. {
  119. counter = 0;
  120.  
  121. while (Vector3.Distance(instancesToMove[i].transform.position, endPos) > 0.1f)
  122. {
  123. counter++;
  124. endPos = allLines[i].GetComponent<EndHolder>().EndVector;
  125. Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
  126. Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);
  127.  
  128. instancesToMove[i].transform.position =
  129. Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);
  130.  
  131. allLines[i].GetComponent<LineRenderer>().SetPosition(1, instancesToMove[i].transform.position);//tempPos);
  132.  
  133. //move towards destination
  134. yield return null;
  135. }
  136. }
  137. }
  138. }
  139.  
  140. allLines = GameObject.FindGameObjectsWithTag("FrameLine").ToList();
  141.  
  142. DuplicatePrefabEffects(allLines.Count);
Add Comment
Please, Sign In to add comment