Advertisement
Guest User

Example

a guest
Feb 18th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var skin:GUISkin;
  2.  
  3. private var gldepth = -0.5;
  4. private var startTime = 0.1;
  5.  
  6. var mat:Material;
  7.  
  8. private var tris = 0;
  9. private var verts = 0;
  10. private var savedTimeScale:float;
  11. private var pauseFilter;
  12.  
  13. private var showfps:boolean;
  14. private var showtris:boolean;
  15. private var showvtx:boolean;
  16. private var showfpsgraph:boolean;
  17.  
  18. var lowFPSColor = Color.red;
  19. var highFPSColor = Color.green;
  20.  
  21. var lowFPS = 30;
  22. var highFPS = 50;
  23.  
  24. var start:GameObject;
  25.  
  26. var url = "unity.html";
  27.  
  28. var statColor:Color = Color.yellow;
  29.  
  30. var credits:String[]=[
  31.     "A Fugu Games Production",
  32.     "Programming by Phil Chu",
  33.     "Fugu logo by Shane Nakamura Designs",
  34.     "Copyright (c) 2007-2008 Technicat, LLC"] ;
  35. var crediticons:Texture[];
  36.  
  37. enum Page {
  38.     None,Main,Options,Credits
  39. }
  40.  
  41. private var currentPage:Page;
  42.  
  43. private var fpsarray:int[];
  44. private var fps:float;
  45.  
  46. function Start() {
  47.     fpsarray = new int[Screen.width];
  48.     GetComponent("MouseLook").enabled = true;
  49.     Time.timeScale = 1.0;
  50.     //pauseFilter = Camera.main.GetComponent(SepiaToneEffect);
  51.     PauseGame();
  52. }
  53.  
  54. function OnPostRender() {
  55.     if (showfpsgraph && mat != null) {
  56.         GL.PushMatrix ();
  57.         GL.LoadPixelMatrix();
  58.         for (var i = 0; i < mat.passCount; ++i)
  59.         {
  60.             mat.SetPass(i);
  61.             GL.Begin( GL.LINES );
  62.             for (var x=0; x<fpsarray.length; ++x) {
  63.                 GL.Vertex3(x,fpsarray[x],gldepth);
  64.             }
  65.         GL.End();
  66.         }
  67.         GL.PopMatrix();
  68.         ScrollFPS();
  69.     }
  70. }
  71.  
  72. function ScrollFPS() {
  73.     for (var x=1; x<fpsarray.length; ++x) {
  74.         fpsarray[x-1]=fpsarray[x];
  75.     }
  76.     if (fps < 1000) {
  77.         fpsarray[fpsarray.length-1]=fps;
  78.     }
  79. }
  80.  
  81. static function IsDashboard() {
  82.     return Application.platform == RuntimePlatform.OSXDashboardPlayer;
  83. }
  84.  
  85. static function IsBrowser() {
  86.     return (Application.platform == RuntimePlatform.WindowsWebPlayer ||
  87.         Application.platform == RuntimePlatform.OSXWebPlayer);
  88. }
  89.  
  90. function LateUpdate () {
  91.     if (showfps || showfpsgraph) {
  92.         FPSUpdate();
  93.     }
  94.     if (Input.GetKeyDown("escape")) {
  95.         switch (currentPage) {
  96.             case Page.None: PauseGame(); break;
  97.             case Page.Main: if (!IsBeginning()) UnPauseGame(); break;
  98.             default: currentPage = Page.Main;
  99.         }
  100.     }
  101. }
  102.  
  103. function OnGUI () {
  104.     if (skin != null) {
  105.         GUI.skin = skin;
  106.     }
  107.     ShowStatNums();
  108.     ShowLegal();
  109.     if (IsGamePaused()) {
  110.         GUI.color = statColor;
  111.         switch (currentPage) {
  112.             case Page.Main: PauseMenu(); break;
  113.             case Page.Options: ShowToolbar(); break;
  114.             case Page.Credits: ShowCredits(); break;
  115.         }
  116.     }  
  117. }
  118.  
  119. function ShowLegal() {
  120.     if (!IsLegal()) {
  121.         GUI.Label(Rect(Screen.width-100,Screen.height-20,90,20),
  122.         "fugugames.com");
  123.     }
  124. }
  125.  
  126. function IsLegal() {
  127.     return !IsBrowser() ||
  128.     Application.absoluteURL.StartsWith("http://www.fugugames.com/") ||
  129.     Application.absoluteURL.StartsWith("http://fugugames.com/");
  130. }
  131.  
  132. private var toolbarInt:int=0;
  133. private var toolbarStrings: String[]= ["Audio","Graphics","Stats","System"];
  134.  
  135. function ShowToolbar() {
  136.     BeginPage(300,300);
  137.     toolbarInt = GUILayout.Toolbar (toolbarInt, toolbarStrings);
  138.     switch (toolbarInt) {
  139.         case 0: VolumeControl(); break;
  140.         case 3: ShowDevice(); break;
  141.         case 1: Qualities(); QualityControl(); break;
  142.         case 2: StatControl(); break;
  143.     }
  144.     EndPage();
  145. }
  146.  
  147. function ShowCredits() {
  148.     BeginPage(300,300);
  149.     for (var credit in credits) {
  150.         GUILayout.Label(credit);
  151.     }
  152.     for (var credit in crediticons) {
  153.         GUILayout.Label(credit);
  154.     }
  155.     EndPage();
  156. }
  157.  
  158. function ShowBackButton() {
  159.     if (GUI.Button(Rect(20,Screen.height-50,50,20),"Back")) {
  160.         currentPage = Page.Main;
  161.     }
  162. }
  163.  
  164.  
  165. function ShowDevice() {
  166.     GUILayout.Label ("Unity player version "+Application.unityVersion);
  167.     GUILayout.Label("Graphics: "+SystemInfo.graphicsDeviceName+" "+
  168.     SystemInfo.graphicsMemorySize+"MB\n"+
  169.     SystemInfo.graphicsDeviceVersion+"\n"+
  170.     SystemInfo.graphicsDeviceVendor);
  171.     GUILayout.Label("Shadows: "+SystemInfo.supportsShadows);
  172.     GUILayout.Label("Image Effects: "+SystemInfo.supportsImageEffects);
  173.     GUILayout.Label("Render Textures: "+SystemInfo.supportsRenderTextures);
  174. }
  175.  
  176. function Qualities() {
  177.         GUILayout.Label(QualitySettings.names[QualitySettings.GetQualityLevel()]);
  178. }
  179.  
  180. function QualityControl() {
  181.     GUILayout.BeginHorizontal();
  182.     if (GUILayout.Button("Decrease")) {
  183.         QualitySettings.DecreaseLevel();
  184.     }
  185.     if (GUILayout.Button("Increase")) {
  186.         QualitySettings.IncreaseLevel();
  187.     }
  188.     GUILayout.EndHorizontal();
  189. }
  190.  
  191. function VolumeControl() {
  192.     GUILayout.Label("Volume");
  193.     AudioListener.volume = GUILayout.HorizontalSlider(AudioListener.volume,0.0,1.0);
  194. }
  195.  
  196. function StatControl() {
  197.     GUILayout.BeginHorizontal();
  198.     showfps = GUILayout.Toggle(showfps,"FPS");
  199.     showtris = GUILayout.Toggle(showtris,"Triangles");
  200.     showvtx = GUILayout.Toggle(showvtx,"Vertices");
  201.     showfpsgraph = GUILayout.Toggle(showfpsgraph,"FPS Graph");
  202.     GUILayout.EndHorizontal();
  203. }
  204.  
  205. function FPSUpdate() {
  206.     var delta = Time.smoothDeltaTime;
  207.         if (!IsGamePaused() && delta !=0.0) {
  208.             fps = 1 / delta;
  209.         }
  210. }
  211.  
  212. function ShowStatNums() {
  213.     GUILayout.BeginArea(Rect(Screen.width-100,10,100,200));
  214.     if (showfps) {
  215.         var fpsString= fps.ToString ("#,##0 fps");
  216.         GUI.color = Color.Lerp(lowFPSColor, highFPSColor,(fps-lowFPS)/(highFPS-lowFPS));
  217.         GUILayout.Label (fpsString);
  218.     }
  219.     if (showtris || showvtx) {
  220.         GetObjectStats();
  221.         GUI.color = statColor;
  222.     }
  223.     if (showtris) {
  224.         GUILayout.Label (tris+"tri");
  225.     }
  226.     if (showvtx) {
  227.         GUILayout.Label (verts+"vtx");
  228.     }
  229.     GUILayout.EndArea();
  230. }
  231.  
  232. function BeginPage(width,height) {
  233.     GUILayout.BeginArea(Rect((Screen.width-width)/2,(Screen.height-height)/2,width,height));
  234. }
  235.  
  236. function EndPage() {
  237.     GUILayout.EndArea();
  238.     if (currentPage != Page.Main) {
  239.         ShowBackButton();
  240.     }
  241. }
  242.  
  243. function IsBeginning() {
  244.     return Time.time < startTime;
  245. }
  246.  
  247.  
  248. function PauseMenu() {
  249.     BeginPage(200,200);
  250.     if (GUILayout.Button (IsBeginning() ? "Play" : "Continue")) {
  251.         UnPauseGame();
  252.  
  253.     }
  254.     if (GUILayout.Button ("Options")) {
  255.         currentPage = Page.Options;
  256.     }
  257.     if (GUILayout.Button ("Credits")) {
  258.         currentPage = Page.Credits;
  259.     }
  260.     if (IsBrowser() && !IsBeginning() && GUILayout.Button ("Restart")) {
  261.         Application.OpenURL(url);
  262.     }
  263.     EndPage();
  264. }
  265.  
  266. function GetObjectStats() {
  267.     verts = 0;
  268.     tris = 0;
  269.     var ob = FindObjectsOfType(GameObject);
  270.     for (var obj in ob) {
  271.         GetObjectStats(obj);
  272.     }
  273. }
  274.  
  275. function GetObjectStats(object) {
  276.     var filters : Component[];
  277.     filters = object.GetComponentsInChildren(MeshFilter);
  278.     for( var f : MeshFilter in filters )
  279.     {
  280.         tris += f.sharedMesh.triangles.Length/3;
  281.         verts += f.sharedMesh.vertexCount;
  282.     }
  283. }
  284.  
  285. function PauseGame() {
  286.     savedTimeScale = Time.timeScale;
  287.     GetComponent("MouseLook").enabled = false;
  288.     Time.timeScale = 0;
  289.     AudioListener.pause = true;
  290.     if (pauseFilter) pauseFilter.enabled = true;
  291.     currentPage = Page.Main;
  292. }
  293.  
  294. function UnPauseGame() {
  295.     Time.timeScale = savedTimeScale;
  296.     AudioListener.pause = false;
  297.     if (pauseFilter) pauseFilter.enabled = false;
  298.     currentPage = Page.None;
  299.     if (IsBeginning() && start != null) {
  300.         start.active = true;
  301.     }
  302. }
  303.  
  304. function IsGamePaused() {
  305.     return Time.timeScale==0;
  306. }
  307.  
  308. function OnApplicationPause(pause:boolean) {
  309.     if (IsGamePaused()) {
  310.         AudioListener.pause = true;
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement