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 darkcookie · Jun 28, 2013 at 06:53 AM · guitogglekey pressed

Switch from GUI button to toggle key

I have this script that's works fine currently although I want to press the "t" key instead of a GUI button toggleing the chat script to be visible.How can I change the toggle of the chat box from being toggled from a GUI button to being toggled with the "t" key?

Script:

 var skin : GUISkin;
 var showChat = false;
 private var inputField = "";
 private var display = true;
 private var entries = ArrayList();
 private var scrollPosition : Vector2;
 
 private var window = Rect(50, 50,700, 400);
 
 class ChatEntry
 {
     var sender = "";
     var text = "";    
     var mine = true;
 }
 
 function CloseChatWindow ()
 {
     showChat = false;
     inputField = "";
     entries = new ArrayList();
 }
 
 function FocusControl ()
 {
     // We can't select it immediately because the control might not have been drawn yet.
     // Thus it is not known to the system!
     yield;
     yield;
     yield;
     GUI.FocusControl("Chat input field");
 }
 
 function OnGUI ()
 {
     GUI.skin = skin;
     
     //if (GUILayout.Button(showChat ? "Hide Chat" : "Display Chat"))
     if (GUI.Button(new Rect(Screen.width-100, Screen.height-30, 90, 20), showChat ? "Hide Chat" : "Display Chat"))
     
     {
         // Focus first element
         if (showChat)
         {    
             CloseChatWindow ();
         }
         else
         {
             showChat = true;
             FocusControl();
         }
     }
     
     if (showChat)
         window = GUI.Window (1, window, GlobalChatWindow, "Chat");
 }
 
 function GlobalChatWindow (id : int) {
     
     var closeButtonStyle = GUI.skin.GetStyle("close_button");
     if (GUI.Button(Rect (4, 4, closeButtonStyle.normal.background.width, closeButtonStyle.normal.background.height), "", "close_button"))
     {
         CloseChatWindow();
     }
     
     // Begin a scroll view. All rects are calculated automatically - 
     // it will use up any available screen space and make sure contents flow correctly.
     // This is kept small with the last two parameters to force scrollbars to appear.
     scrollPosition = GUILayout.BeginScrollView (scrollPosition);
 
     for (var entry : ChatEntry in entries)
     {
         GUILayout.BeginHorizontal();
         if (!entry.mine)
         {
             GUILayout.FlexibleSpace ();
             GUILayout.Label (entry.text, "chat_rightaligned");
         }
         else
         {
             GUILayout.Label (entry.text, "chat_leftaligned");
             GUILayout.FlexibleSpace ();
         }
         
         GUILayout.EndHorizontal();
         GUILayout.Space(3);
         
     }
     // End the scrollview we began above.
     GUILayout.EndScrollView ();
     
     if (Event.current.type == EventType.keyDown && Event.current.character == "\n" && inputField.Length > 0)
     {
         //@TODO: This should be dependent on who actually sent the message
         //var mine = entries.Count % 2 == 0;
         ApplyGlobalChatText(inputField, 1);
         networkView.RPC("ApplyGlobalChatText", RPCMode.Others, inputField, 0);
         inputField = "";
     }
     GUI.SetNextControlName("Chat input field");
     inputField = GUILayout.TextField(inputField);
     
     GUI.DragWindow();
 }
 
 @RPC
 function ApplyGlobalChatText (str : String, mine : int)
 {
     var entry = new ChatEntry();
     entry.sender = "Not implemented";
     entry.text = str;
     if (mine == 1) entry.mine = true;
     else entry.mine = false;
 
     entries.Add(entry);
     
     if (entries.Count > 50)
         entries.RemoveAt(0);
         
     scrollPosition.y = 1000000;    
 }
 
 function Update (){
 if(Input.GetKeyDown(KeyCode.Return)){
 
 showChat = true;
 
 }
 
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 28, 2013 at 07:01 AM

Something like that:

    var e = Event.current;
    if (GUI.Button(...) || (e.type = EventType.KeyDown && e.keyCode == KeyCode.T && !showChat ))

This would allow the button or the t key to open the chat. I excluded closing the chat with the t key or it would close when the user types "t" inside the chat.

For more information see:

  • http://docs.unity3d.com/Documentation/ScriptReference/Event.html

  • http://docs.unity3d.com/Documentation/ScriptReference/KeyCode.html

Comment
Add comment · Show 2 · 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 darkcookie · Jun 28, 2013 at 07:06 AM 0
Share

I get allot of errors :( expecting ) found "=" expecting ) found EventType... I$$anonymous$$ inserting the piece of code...well the var in the top and the code in line 39..am i doing something wrong?

avatar image darkcookie · Jun 28, 2013 at 06:18 PM 0
Share

that code didn't work an any one else help me pls?

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

16 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

Related Questions

Toggle GuiTexture 1 Answer

Can I have toggle buttons in a selection grid? 1 Answer

Put toggle label on left side of button and hover 1 Answer

Is there a way of using just basic gui.Buttons as toggles? 1 Answer

Toggle Switch 6 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