GameObjectSpawner.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /****************************************
  2. GameObjectSpawner.js
  3. Copyright 2014 Unluck Software
  4. www.chemicalbliss.com
  5. GUI Buttons to view GameObject / Particle Systems from list
  6. *****************************************/
  7. #pragma strict
  8. import System.Collections.Generic; //Used to sort particle system list
  9. //Visible properties
  10. var particles:GameObject[]; //gameObjects to spawn (used to only be particle systems aka var naming)
  11. var materials:Material[];
  12. var cameraColors:Color[];
  13. var maxButtons:int = 10; //Maximum buttons per page
  14. var buttonWidth:int = 250;
  15. var hSpace:int = 20;
  16. var spawnOnAwake:boolean = true; //Instantiate the first model on start
  17. var showInfo:boolean; //Show info text on start
  18. var removeTextFromButton:String; //Unwanted text
  19. var removeTextFromMaterialButton:String;//Unwanted text
  20. var autoChangeDelay:float;
  21. var image:GUITexture;
  22. //Hidden properties
  23. private var page:int = 0; //Current page
  24. private var pages:int; //Number of pages
  25. private var currentGOInfo:String; //Current particle info
  26. private var currentGO:GameObject; //GameObject currently on stage
  27. private var currentColor:Color;
  28. private var isPS:boolean; //Toggle to check if this is a PS or a GO
  29. private var material:Material;
  30. private var _active:boolean = true;
  31. private var counter:int = -1;
  32. private var matCounter:int = -1;
  33. private var colorCounter:int;
  34. var bigStyle: GUIStyle;
  35. var android:boolean;
  36. function Start(){
  37. // InvokeRepeating("RandomSpawn", .5, .1);
  38. //Sort particle system list alphabeticly
  39. particles.Sort(particles, function(g1,g2) String.Compare(g1.name, g2.name));
  40. materials.Sort(materials, function(g1,g2) String.Compare(g1.name, g2.name));
  41. //Calculate number of pages
  42. pages = Mathf.Ceil((particles.length -1 )/ maxButtons);
  43. //Debug.Log(pages);
  44. if(spawnOnAwake){
  45. counter=0;
  46. ReplaceGO(particles[counter]);
  47. Info(particles[counter], counter);
  48. }
  49. if(autoChangeDelay > 0){
  50. InvokeRepeating("NextModel", autoChangeDelay,autoChangeDelay);
  51. }
  52. }
  53. function RandomSpawn() {
  54. var p:int = Random.Range(0,particles.Length);
  55. if(p!=11&&p!=10&&p!=33&&p!=32){
  56. var pp:GameObject = Instantiate(particles[p], Vector3(Random.Range(175.0, -175.0), Random.Range(175.0, -175.0), Random.Range(0, 550.0)), transform.rotation);
  57. pp.GetComponent.<ParticleSystem>().loop = false;
  58. Destroy(pp, pp.GetComponent.<ParticleSystem>().duration+14);
  59. StopEmit(pp);
  60. }
  61. }
  62. function StopEmit(pp:GameObject){
  63. yield(WaitForSeconds(pp.GetComponent.<ParticleSystem>().duration));
  64. pp.GetComponent.<ParticleSystem>().Stop();
  65. }
  66. function Update () {
  67. if(Input.GetKeyDown(KeyCode.Space)) {
  68. if(_active){
  69. _active = false;
  70. if(image)
  71. image.enabled = false;
  72. }else{
  73. _active = true;
  74. if(image)
  75. image.enabled = true;
  76. }
  77. }
  78. if(Input.GetKeyDown(KeyCode.RightArrow)) {
  79. NextModel ();
  80. }
  81. if(Input.GetKeyDown(KeyCode.LeftArrow)) {
  82. PrevModel ();
  83. }
  84. if(Input.GetKeyDown(KeyCode.UpArrow) && materials.Length>0) {
  85. matCounter++;
  86. if(matCounter > materials.Length -1) matCounter = 0;
  87. material = materials[matCounter];
  88. if(currentGO){
  89. currentGO.GetComponent.<Renderer>().sharedMaterial = material;
  90. }
  91. }
  92. if(Input.GetKeyDown(KeyCode.DownArrow) && materials.Length>0) {
  93. matCounter--;
  94. if(matCounter < 0) matCounter = materials.Length-1;
  95. material = materials[matCounter];
  96. if(currentGO){
  97. currentGO.GetComponent.<Renderer>().sharedMaterial = material;
  98. }
  99. }
  100. if(Input.GetKeyDown(KeyCode.B)) {
  101. colorCounter++;
  102. if(colorCounter > cameraColors.Length -1) colorCounter = 0;
  103. }
  104. Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, cameraColors[colorCounter], Time.deltaTime*3);
  105. }
  106. function NextModel () {
  107. counter++;
  108. if(counter > particles.Length -1) counter = 0;
  109. ReplaceGO(particles[counter]);
  110. Info(particles[counter], counter+1);
  111. }
  112. function OnGUI () {
  113. GUI.skin.button.alignment = TextAnchor.LowerLeft;
  114. if(showInfo)GUI.Label (Rect(buttonWidth+hSpace+hSpace, hSpace*3,500,500), currentGOInfo, bigStyle);
  115. if(!android){
  116. if(_active){
  117. //Time Scale Vertical Slider
  118. //Time.timeScale = GUI.VerticalSlider (Rect (185, 50, 20, 150), Time.timeScale, 2.0, 0.0);
  119. //Field of view Vertical Slider
  120. //Camera.mainCamera.fieldOfView = GUI.VerticalSlider (Rect (225, 50, 20, 150), Camera.mainCamera.fieldOfView, 20.0, 100.0);
  121. //Check if there are more particle systems than max buttons (true adds "next" and "prev" buttons)
  122. if(particles.length > maxButtons){
  123. //Prev button
  124. if(GUI.Button(Rect(hSpace,(maxButtons+1)*18,buttonWidth*.5,18),"Prev"))if(page > 0)page--;else page=pages;
  125. //Next button
  126. if(GUI.Button(Rect(buttonWidth*.5+hSpace,(maxButtons+1)*18,buttonWidth*.5,18),"Next"))if(page < pages)page++;else page=0;
  127. //Page text
  128. GUI.Label (Rect(60,(maxButtons+2)*18,150,22), "Page" + (page+1) + " / " + (pages+1));
  129. }
  130. //Toggle button for info
  131. showInfo = GUI.Toggle (Rect(buttonWidth+hSpace+hSpace, hSpace,buttonWidth*.5,25), showInfo, "Info");
  132. //System info
  133. //Calculate how many buttons on current page (last page might have less)
  134. var pageButtonCount:int = particles.length - (page*maxButtons);
  135. //Debug.Log(pageButtonCount);
  136. if(pageButtonCount > maxButtons)pageButtonCount = maxButtons;
  137. //Adds buttons based on how many particle systems on page
  138. for(var i:int=0;i < pageButtonCount;i++){
  139. var buttonText:String = particles[i+(page*maxButtons)].transform.name;
  140. if(removeTextFromButton != "")
  141. buttonText = buttonText.Replace(removeTextFromButton, "");
  142. if(GUI.Button(Rect(hSpace,i*18+18,buttonWidth,18),buttonText)){
  143. if(currentGO) Destroy(currentGO);
  144. var go:GameObject = Instantiate(particles[i+page*maxButtons]);
  145. currentGO = go;
  146. counter = i + (page * maxButtons);
  147. if(material)
  148. go.GetComponent.<Renderer>().sharedMaterial = material;
  149. Info(go, i + (page * maxButtons) +1);
  150. }
  151. }
  152. for(var m:int=0;m < materials.Length;m++){
  153. var b:String = materials[m].name;
  154. if(removeTextFromMaterialButton != "")
  155. b = b.Replace(removeTextFromMaterialButton, "");
  156. if(GUI.Button(Rect(hSpace,(maxButtons+m+4)*18,150,18),b)){
  157. material = materials[m];
  158. if(currentGO){
  159. currentGO.GetComponent.<Renderer>().sharedMaterial = material;
  160. }
  161. }
  162. }
  163. }
  164. if(image){
  165. image.pixelInset.x = (Screen.width) -(image.texture.width) ;
  166. }
  167. }else{
  168. if(GUI.Button(Rect((Screen.width*.5)-150,Screen.height-120,150,100),"Prev")){
  169. PrevModel();
  170. }
  171. if(GUI.Button(Rect((Screen.width*.5),Screen.height-120,150,100),"Next")){
  172. NextModel();
  173. }
  174. }}
  175. function PrevModel(){
  176. counter--;
  177. if(counter < 0) counter = particles.Length-1;
  178. ReplaceGO(particles[counter]);
  179. Info(particles[counter], counter+1);
  180. }
  181. function Info(go:GameObject, i:int) {
  182. if(go.GetComponent(ParticleSystem)){
  183. PlayPS(go.GetComponent(ParticleSystem), i );
  184. InfoPS(go.GetComponent(ParticleSystem), i );
  185. }else{
  186. InfoGO(go, i);
  187. }
  188. }
  189. function ReplaceGO (_go:GameObject){
  190. if(currentGO) Destroy(currentGO);
  191. var go:GameObject = Instantiate(_go);
  192. currentGO = go;
  193. if(material)
  194. go.GetComponent.<Renderer>().sharedMaterial = material;
  195. }
  196. //Play particle system (resets time scale)
  197. function PlayPS (_ps:ParticleSystem, _nr:int){
  198. Time.timeScale = 1;
  199. _ps.Play();
  200. }
  201. function InfoGO (_ps:GameObject, _nr:int){
  202. currentGOInfo = "" + "" + _nr + "/" + particles.length +"\n"+
  203. _ps.gameObject.name +"\n" + _ps.GetComponent(MeshFilter).sharedMesh.triangles.Length/3 + " Tris";
  204. currentGOInfo = currentGOInfo.Replace("_", " ");
  205. //Instructions();
  206. }
  207. function Instructions() {
  208. currentGOInfo = currentGOInfo + "\n\nUse mouse wheel to zoom \n"+"Click and hold to rotate\n"+"Press Space to show or hide menu\n"+"Press Up and Down arrows to cycle materials\n"+"Press B to cycle background colors";
  209. currentGOInfo = currentGOInfo.Replace("(Clone)", "");
  210. }
  211. function InfoPS (_ps:ParticleSystem, _nr:int){
  212. //Change current particle info text
  213. currentGOInfo = "System" + ": " + _nr + "/" + particles.length +"\n"+
  214. _ps.gameObject.name +"\n\n" ;
  215. //"Main PS Sub Particles: " + _ps.transform.childCount +"\n" ;
  216. //"Main PS Materials: " + _ps.renderer.sharedMaterials.length +"\n" +
  217. //"Main PS Shader: " + _ps.renderer.sharedMaterial.shader.name;
  218. //If plasma(two materials)
  219. // if(_ps.renderer.materials.length >= 2)currentGOInfo = currentGOInfo + "\n\n *Plasma not mobile optimized*";
  220. //Instructions();
  221. currentGOInfo = currentGOInfo.Replace("_", " ");
  222. currentGOInfo = currentGOInfo.Replace("(Clone)", "");
  223. }