Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sveyh · Mar 27, 2014 at 06:07 PM · guiguitexturegui-buttonpasswordfield

C# GUI keypad

Hello, I'm a newcomer and I need help with one thing, I've written 9 GUI buttons like this GUI.Button(new Rect(350, 125, 50, 50), "1"); (this is one of the 9 GUI buttons) and I've put them on a position on the screen so it looks like a calculator/key pad. The thing I need help with is, how do I script/write so each button has their own specific number and that number appears in the PasswordField so the Player can type in the correct code ingame and unlock a door for an example. I'll post my code so you guys might give me a better advice or something.

 public string PlayerPassword = "";
 public string password = "1234";
 private bool Frodo;
 // Use this for initialization
 void Start () 
 {
     Frodo = false;
 }
 void OnTriggerEnter()
 {   
         Frodo = true;    
 }
 void OnGUI()
 {
     if (Frodo == true)
     {
         GUI.Button(new Rect(350, 125, 50, 50), "1");
         GUI.Button(new Rect(400, 125, 50, 50), "2");
         GUI.Button(new Rect(450, 125, 50, 50), "3");
         GUI.Button(new Rect(350, 175, 50, 50), "4");
         GUI.Button(new Rect(400, 175, 50, 50), "5");
         GUI.Button(new Rect(450, 175, 50, 50), "6");
         GUI.Button(new Rect(350, 225, 50, 50), "7");
         GUI.Button(new Rect(400, 225, 50, 50), "8");
         GUI.Button(new Rect(450, 225, 50, 50), "9");
         password = GUI.PasswordField(new Rect(325, 60, 200, 50), password, "*"[0], 0);
     }
 }
 void OnTriggerExit()
 {
     Frodo = false;
 }

}

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Slobdell · Mar 27, 2014 at 06:59 PM

Gui.button responds when it's touched. So you need an if statement for each

 if(GUI.Button(new Rect(350, 125, 50, 50), "1"))
 {
        // do whatever happens when 1 is pressed
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by hkessock · Mar 27, 2014 at 07:27 PM

Here's a PIN dialog I wrote that works sort of modally.

 public class PINDialog
 {
     private static Texture2D m_oBlackTexture = null;
 
     public string m_strPIN = "";
     public bool m_bSettingPIN = false;
     public bool m_bOK = false;
     public bool m_bFinished = false;
     public string m_strPINToMatch = "";
 
     protected bool m_bHaveEdited = false;
 
     //GUI Scale related
     int m_nScreenWidth = -1;
     int m_nScreenHeight = -1;
 
 
     public PINDialog( bool in_bSettingPIN )
     {
         if( null == m_oBlackTexture )
         {
             CreateBlackTexture();
         }
 
         m_bSettingPIN = in_bSettingPIN;
     }
 
     void CreateBlackTexture()
     {
         m_oBlackTexture = new Texture2D( 2, 2 );
         m_oBlackTexture.SetPixel( 0, 0, Color.black );
         m_oBlackTexture.SetPixel( 0, 1, Color.black );
         m_oBlackTexture.SetPixel( 1, 0, Color.black );
         m_oBlackTexture.SetPixel( 1, 1, Color.black );
         m_oBlackTexture.Apply();
     }
 
     public void DrawPINDialog()
     {
         //We scale all OnGUI calls to match the iPad4 - so use that resolution for screen height/width
         m_nScreenWidth = g_oProgram.m_nGUITargetResolutionWidth;
         m_nScreenHeight = g_oProgram.m_nGUITargetResolutionHeight;
 
         GUI.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f );
         GUI.Window( 0, new Rect( 0, 0, m_nScreenWidth, m_nScreenHeight ), DrawControls, "DialogBox Click Preventer 1" );
     }
 
     void DrawControls( int in_nWindowID )
     {
         Color l_oCurrentColor = GUI.color;
         GUIStyle l_oButtonStyle = new GUIStyle( GUI.skin.button );
         l_oButtonStyle.fontSize = 32;
         int l_nMiddleX = m_nScreenWidth / 2;
         int l_nMiddleY = m_nScreenHeight / 2;
 
         //Background for PIN entry
         GUIStyle l_oDialogStyle = new GUIStyle( GUI.skin.box );
         l_oDialogStyle.alignment = TextAnchor.MiddleCenter;
         l_oDialogStyle.normal.background = m_oBlackTexture;
 
         int l_nDialogWidth = 680;
         int l_nDialogHeight = 1200;
 
         Rect l_oDialogRect = new Rect( l_nMiddleX - ( l_nDialogWidth / 2 ), l_nMiddleY - ( l_nDialogHeight / 2 ), l_nDialogWidth, l_nDialogHeight );
         GUI.Box( l_oDialogRect, "", l_oDialogStyle );
 
         /*
             PIN Display is on a neutered button...
         */
         GUIStyle l_oPINDisplayStyle = new GUIStyle( GUI.skin.button );
 
         l_oPINDisplayStyle.active = l_oPINDisplayStyle.normal;
         l_oPINDisplayStyle.hover = l_oPINDisplayStyle.normal;
         l_oPINDisplayStyle.fontSize = 128;
         l_oPINDisplayStyle.alignment = TextAnchor.MiddleCenter;
 
         int l_nPINDisplayWidth = 600;
         int l_nPINDisplayHeight = 200;
 
         int l_nPINDisplayX = l_nMiddleX - ( l_nPINDisplayWidth / 2 );
         int l_nPINDisplayY = l_nMiddleY - 550;
 
         Rect l_oPINDisplayRect = new Rect( l_nPINDisplayX, l_nPINDisplayY, l_nPINDisplayWidth, l_nPINDisplayHeight );
 
         string l_strPINText = m_strPIN;
 
         //Do we need to mask the pin?
         if( false == m_bSettingPIN )
         {
             l_oPINDisplayStyle.fontSize = 256;
             l_oPINDisplayStyle.alignment = TextAnchor.UpperCenter;
             l_strPINText = new string( '*', l_strPINText.Length );
         }
 
         GUI.Button( l_oPINDisplayRect, l_strPINText, l_oPINDisplayStyle );
 
         /*
             Number keys 0-9
         */
         GUIStyle l_oKeyStyle = new GUIStyle( GUI.skin.button );
 
         l_oKeyStyle.fontSize = 96;
         l_oKeyStyle.alignment = TextAnchor.MiddleCenter;
 
         int l_nVerticalOffset = 180;
         int l_nHorizontalOffset = 180;
         int l_nKeyWidth = 150;
         int l_nKeyHeight = 150;
 
         int l_nRow1Y = l_nPINDisplayY + l_nPINDisplayHeight + ( l_nVerticalOffset - l_nKeyHeight ) + ( l_nKeyHeight / 2 );
         int l_nRow2Y = l_nRow1Y + l_nVerticalOffset;
         int l_nRow3Y = l_nRow2Y + l_nVerticalOffset;
         int l_nRow4Y = l_nRow3Y + l_nVerticalOffset;
         int l_nRow5Y = l_nRow4Y + l_nVerticalOffset;
 
         int l_nColumn1X = l_nMiddleX - ( 180 );
         int l_nColumn2X = l_nColumn1X + l_nHorizontalOffset;
         int l_nColumn3X = l_nColumn2X + l_nHorizontalOffset;
 
         //7 key
         Rect l_o7Rect = new Rect( l_nColumn1X - ( l_nKeyWidth / 2 ), l_nRow1Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o7Rect, "7", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "7";
             }
         }
 
         //8 key
         Rect l_o8Rect = new Rect( l_nColumn2X - ( l_nKeyWidth / 2 ), l_nRow1Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o8Rect, "8", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "8";
             }
         }
 
         //9 key
         Rect l_o9Rect = new Rect( l_nColumn3X - ( l_nKeyWidth / 2 ), l_nRow1Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o9Rect, "9", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "9";
             }
         }
 
         //4 key
         Rect l_o4Rect = new Rect( l_nColumn1X - ( l_nKeyWidth / 2 ), l_nRow2Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o4Rect, "4", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "4";
             }
         }
 
         //5 key
         Rect l_o5Rect = new Rect( l_nColumn2X - ( l_nKeyWidth / 2 ), l_nRow2Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o5Rect, "5", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "5";
             }
         }
 
         //6 key
         Rect l_o6Rect = new Rect( l_nColumn3X - ( l_nKeyWidth / 2 ), l_nRow2Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o6Rect, "6", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "6";
             }
         }
 
         //1 key
         Rect l_o1Rect = new Rect( l_nColumn1X - ( l_nKeyWidth / 2 ), l_nRow3Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o1Rect, "1", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "1";
             }
         }
 
         //2 key
         Rect l_o2Rect = new Rect( l_nColumn2X - ( l_nKeyWidth / 2 ), l_nRow3Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o2Rect, "2", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "2";
             }
         }
 
         //3 key
         Rect l_o3Rect = new Rect( l_nColumn3X - ( l_nKeyWidth / 2 ), l_nRow3Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
 
         if( true == GUI.Button( l_o3Rect, "3", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "3";
             }
         }
 
         /*
             Zero key and Del
         */
         Rect l_o0Rect = new Rect( l_nColumn1X - ( l_nKeyWidth / 2 ), l_nRow4Y - ( l_nKeyHeight / 2 ), l_nKeyWidth * 2 + ( l_nHorizontalOffset - l_nKeyWidth ), l_nKeyHeight );
 
         if( true == GUI.Button( l_o0Rect, "0", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length < 4 )
             {
                 m_strPIN += "0";
             }
         }
 
         Rect l_oDelRect = new Rect( l_nColumn3X - ( l_nKeyWidth / 2 ), l_nRow4Y - ( l_nKeyHeight / 2 ), l_nKeyWidth, l_nKeyHeight );
         l_oKeyStyle.fontSize = 56;
 
         if( true == GUI.Button( l_oDelRect, "DEL", l_oKeyStyle ) )
         {
             g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
             ClearIfFirstEdit();
 
             if( m_strPIN.Length > 0 )
             {
                 m_strPIN = m_strPIN.Remove( m_strPIN.Length - 1 );
             }
         }
 
         if( true == m_bSettingPIN )
         {
             /*
                 Apply Button
             */
             Rect l_oApplyRect = new Rect( l_nColumn1X - ( l_nKeyWidth / 2 ), l_nRow5Y - ( l_nKeyHeight / 2 ), ( l_nKeyWidth * 3 + ( l_nHorizontalOffset - l_nKeyWidth ) * 2 ) / 2, l_nKeyHeight );
             l_oKeyStyle.fontSize = 56;
 
             if( true == GUI.Button( l_oApplyRect, "Apply", l_oKeyStyle ) )
             {
                 g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
 
                 //Validate
                 if( m_strPIN.Length != 4 )
                 {
                     g_oProgram.PlaySound( g_oProgram.MenuSound.Warning );
                     MessageBox.Show( "A PIN must consist of 4 numbers, you have specified " + m_strPIN.Length );
 
                     return;
                 }
 
                 //Ok, now close
                 m_bOK = true;
                 m_bFinished = true;
             }
 
             /*
                 Cancel Button
             */
             Rect l_oCancelRect = new Rect( l_nMiddleX + ( l_nHorizontalOffset - l_nKeyWidth ), l_nRow5Y - ( l_nKeyHeight / 2 ), ( l_nKeyWidth * 3 + ( l_nHorizontalOffset - l_nKeyWidth ) * 2 ) / 2, l_nKeyHeight );
             l_oKeyStyle.fontSize = 56;
 
             if( true == GUI.Button( l_oCancelRect, "Cancel", l_oKeyStyle ) )
             {
                 g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
 
                 //Close
                 m_bOK = false;
                 m_bFinished = true;
             }
         }
         else
         {
             /*
                 Enter Button
             */
             Rect l_oEnterRect = new Rect( l_nColumn1X - ( l_nKeyWidth / 2 ), l_nRow5Y - ( l_nKeyHeight / 2 ), ( l_nKeyWidth * 3 + ( l_nHorizontalOffset - l_nKeyWidth ) * 2 ) / 2, l_nKeyHeight );
             l_oKeyStyle.fontSize = 56;
 
             if( true == GUI.Button( l_oEnterRect, "Enter", l_oKeyStyle ) )
             {
                 g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
 
                 //Validate
                 if( m_strPIN != m_strPINToMatch )
                 {
                     g_oProgram.PlaySound( g_oProgram.MenuSound.Warning );
                     MessageBox.Show( "Invalid PIN" );
                     m_strPIN = "";
 
                     return;
                 }
 
                 //Ok, now close
                 m_bOK = true;
                 m_bFinished = true;
             }
 
             /*
                 Cancel Button
             */
             Rect l_oCancelRect = new Rect( l_nMiddleX + ( l_nHorizontalOffset - l_nKeyWidth ), l_nRow5Y - ( l_nKeyHeight / 2 ), ( l_nKeyWidth * 3 + ( l_nHorizontalOffset - l_nKeyWidth ) * 2 ) / 2, l_nKeyHeight );
             l_oKeyStyle.fontSize = 56;
 
             if( true == GUI.Button( l_oCancelRect, "Cancel", l_oKeyStyle ) )
             {
                 g_oProgram.PlaySound( g_oProgram.MenuSound.KeypadClick );
 
                 //Close
                 m_bOK = false;
                 m_bFinished = true;
             }
         }
 
         //Draw fullscreen background cover (dumb click through bugs)
         GUI.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f );
         GUI.Button( new Rect( 0, 0, m_nScreenWidth, m_nScreenHeight ), "" );
         GUI.color = l_oCurrentColor;
     }
 
     protected void ClearIfFirstEdit()
     {
         if( false == m_bHaveEdited )
         {
             m_strPIN = "";
             m_bHaveEdited = true;
         }
     }
 }

You use it like by creating an instance of it, then calling ::DrawPINDialog() each frame in OnGUI(), then checking periodically to see if the PIN Dialog says that it is finished, and whether 'OK' was the result...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sveyh · Mar 27, 2014 at 07:55 PM 0
Share

I dont really wanna s$$anonymous$$l your script as I probably don't understand half of it, I'm sorry but I rather create my own.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gui button still visible when texture added. 1 Answer

How do I set up a GUITexture as a button? (Java Script) 2 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

C# pausing the game during GUI keypad 1 Answer

Why GUI TEXT Move ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges