Advertisement
LostMystic

Unity3d Popup Message

Jan 13th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. /// <summary>
  2. /// Popup Note Script
  3. ///
  4. /// Attach to a trigger object in the game.
  5. /// You can use the graphic provided, please give credit to the author:
  6. /// Shadows of Elear - Deviantart.com
  7. ///
  8. /// You may use this script for commercial purposes
  9. ///
  10. /// http://lostmystic.com
  11. /// Written in Unity 4.3.1
  12. /// </summary>
  13. using UnityEngine;
  14. using System.Collections;
  15.  
  16. public class TrigPopupMsg : MonoBehaviour {
  17.  
  18.     public string Text;
  19.     public Texture2D PageTexture;
  20.  
  21.     public GUIStyle myGuiStyle = new GUIStyle();
  22.  
  23.     // If true, will only show once
  24.     // If false, will show every time trigger area is entered
  25.     public bool bShowOnlyOnce = false;
  26.  
  27.     // Show button at bottom of screen
  28.     public bool bShowButton = true;
  29.  
  30.     private bool bShowNote = false;
  31.     private Rect rectBmpBg;
  32.     private Rect rectText;
  33.     private Rect rectBtn;
  34.  
  35.  
  36.    
  37.     void Start() {
  38.  
  39.         if (myGuiStyle == null) {
  40.             myGuiStyle.wordWrap = true;
  41.         }
  42.  
  43.         rectBmpBg = MakeRectByPercent(0.7f,0.6f,TextAnchor.MiddleCenter);
  44.         rectText = MakeRectByPercent(0.65f,0.45f,TextAnchor.MiddleCenter);
  45.         rectBtn = new Rect(rectText.x+rectText.width/2-40,rectText.y+rectText.height-25,80,20);
  46.  
  47.     }
  48.  
  49.     void OnTriggerEnter(Collider target) {
  50.        
  51.         if (target.tag.Equals("Player")) {
  52.             bShowNote = true;
  53.             AudioSource mySfx = GetComponent<AudioSource>() as AudioSource;
  54.             if (mySfx != null) audio.Play();
  55.         }
  56.     }
  57.  
  58.     void OnGUI() {
  59.  
  60.         if (!bShowNote) return;
  61.  
  62.         GUI.skin = GuiManager.Instance.myGuiSkin;
  63.  
  64.         // If you haven't added a page texture
  65.         if (PageTexture == null) {
  66.             GUI.Box(rectBmpBg,"");
  67.         } else
  68.             GUI.DrawTextureWithTexCoords(rectBmpBg,PageTexture,new Rect(0,0.6f,1,0.4f));
  69.  
  70.         GUI.Box(rectText, Text,myGuiStyle);
  71.  
  72.         // If escape, or enter or left click
  73.  
  74.         if (Input.GetKeyDown(KeyCode.Return)) bShowNote = false;
  75.         if (Input.GetKeyDown(KeyCode.KeypadEnter)) bShowNote = false;
  76.         if (Input.GetKeyDown(KeyCode.Escape)) bShowNote = false;
  77.  
  78.         if (bShowButton) {
  79.  
  80.             if (GUI.Button(rectBtn, "Resume") )
  81.                 bShowNote = false;
  82.  
  83.         } else {
  84.             if (Input.GetMouseButtonDown(0)) bShowNote = false;
  85.         }
  86.  
  87.     }
  88.     public static Rect MakeRectByPercent(float x, float y, TextAnchor position) {
  89.        
  90.        
  91.         if ((x > 1) || (y > 1)) {
  92.             return new Rect(0,0,Screen.width,Screen.height);
  93.         }
  94.        
  95.         x = Screen.width * x;
  96.         y = Screen.height * y;
  97.        
  98.         return MakeRectByPixel(x,y,position);
  99.     }
  100.    
  101.    
  102.     // Returns a rectangle centered on the screen according to the positions below
  103.     public static Rect MakeRectByPixel(float x, float y, TextAnchor position) {
  104.        
  105.         if (position == TextAnchor.MiddleCenter) {
  106.             return new Rect(Screen.width/2-(x/2),Screen.height/2-(y/2),x,y);
  107.         }
  108.        
  109.         if (position == TextAnchor.UpperCenter) {
  110.             return new Rect(Screen.width/2-(x/2),0,x,y);
  111.         }
  112.        
  113.         if (position == TextAnchor.LowerCenter) {
  114.             return new Rect(Screen.width/2-(x/2),Screen.height-y,x,y);
  115.         }
  116.        
  117.         if (position == TextAnchor.UpperLeft) {
  118.             return new Rect(0,0,x,y);
  119.         }
  120.        
  121.         if (position == TextAnchor.MiddleLeft) {
  122.             return new Rect(0,Screen.height/2-(y/2),x,y);
  123.         }
  124.        
  125.         if (position == TextAnchor.LowerLeft) {
  126.             return new Rect(0,Screen.height-y,x,y);
  127.         }
  128.        
  129.         if (position == TextAnchor.UpperRight) {
  130.             return new Rect(Screen.width-x,0,x,y);
  131.         }
  132.        
  133.         if (position == TextAnchor.MiddleRight) {
  134.             return new Rect(Screen.width-x,Screen.height/2-(y/2),x,y);
  135.         }
  136.        
  137.         if (position == TextAnchor.LowerRight) {
  138.             return new Rect(Screen.width-x,Screen.height-y,x,y);
  139.         }
  140.        
  141.         return new Rect(0,0,0,0);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement