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 Forsstrom · Mar 10, 2013 at 08:05 PM · networkinginputnetworkchat

Only activate chat if T is pressed

Hi, I customized a youtube tutorial script for a multiplayer chat. I just need some help with some features with it.

  1. You can only write if T is pressed

  2. You can send messages by pressing enter

  3. Messages gets removed after a certain time

Here's the two codes:

using UnityEngine; using System.Collections;

public class ChatBase : MonoBehaviour { public enum Clean { Full = 0, Semi = 1, none = 0 }; public static Clean cleaning = Clean.Semi; public static ArrayList messages = new ArrayList(); public static string user = "";

 public string[] Swares;

 private string messageToSend = "";

 private void Start()
 {
     DontDestroyOnLoad(gameObject);
 }

 public static void CleanChat()
 {
     ArrayList temp = new ArrayList();
     for (int x = 1; x < messages.Count; x++)
     {
         temp.Add(messages[x]);
     }
     messages = temp;
 }

 public void sendMessage(string mess)
 {
     messageToSend = mess;
     if (mess != "")
     {

         for (int c = 0; c < mess.Length; c++)
         {
             foreach (string s in Swares)
             {
                 if (mess[c].ToString().ToUpper() == s[0].ToString().ToUpper())
                 {
                     string temp = "";
                     int extra = 0;
                     for (int x = c; x < messageToSend.Length; x++)
                     {
                         if (messageToSend[x].ToString().ToUpper() != messageToSend[0].ToString().ToUpper())
                         {
                             if (messageToSend[(x - 1)] == messageToSend[x])
                             {
                                 extra++;
                                 continue;
                             }
                         }

                         if (messageToSend[x].ToString().ToUpper() != s[s.Length - 1].ToString().ToUpper())
                         {

                             temp += messageToSend[x].ToString().ToUpper();
                         }
                         else
                         {
                             string reg = temp + messageToSend.Substring(x, 1).ToString().ToUpper();
                             if (reg.ToUpper() == s.ToUpper())
                             {
                                 string firstHalf = messageToSend.Substring(0, c);
                                 string secondHalf = messageToSend.Substring(c + s.Length + extra, messageToSend.Length - s.Length - c - extra);
                                 string stars = new string('*', s.Length);
                                 messageToSend = firstHalf + stars + secondHalf;
                             }
                             break;
                         }
                     }
                 }
             }
             if (c == (mess.Length - 1))
                 networkView.RPC("ReciveMessage", RPCMode.All, user + ": " + messageToSend);
         }
     }
 }

 [RPC]
 private void ReciveMessage(string sentMess)
 {
     messages.Add(sentMess);
     Invoke("Recheck", 5);
 }

 private void Recheck()
 {
     if (cleaning == Clean.Semi)
         CleanChat();
 }

}

using UnityEngine; using System.Collections;

public class Chat : MonoBehaviour { private string message = ""; private Vector2 scrollView = Vector2.zero;

 private MultiplayerManager MpManager;
 
 public bool IsTyping = false;

 private void Start()
 {
     ChatBase.user = "";
     ChatBase.messages = new ArrayList();
     ChatBase.user = MultiplayerManager.instance.PlayerName;
 }

 private void OnGUI()
 {
     GUILayout.BeginArea(new Rect(Screen.width - 200, Screen.height - 250, 200 ,200));
     scrollView = GUILayout.BeginScrollView(scrollView);
     foreach(string c in ChatBase.messages)
     {
         GUILayout.Label(c);
     }
     GUILayout.EndArea();
     GUILayout.EndScrollView();
     scrollView.y++;
     
     if (IsTyping == false & Input.GetButton("Chat"))
     {
     message = GUI.TextField(new Rect(Screen.width - 200, Screen.height - 25, 150, 25), message);
     if (GUI.Button(new Rect(Screen.width - 50, Screen.height - 25, 50, 25), "Send"))
     {
         ChatBase.cleaning = ChatBase.Clean.none;
         GameObject.Find("Chat").GetComponent<ChatBase>().sendMessage(message);
         message = "";
     }
     }
 }

}

I would really appreciate help with this.

Comment
Add comment · Show 1
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 Benproductions1 · Apr 09, 2013 at 02:19 AM 0
Share

Format your code, don't ask for features, delete this question... Vottedown

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

How can I set up the VoiceChat asset by the GitHub user fholm in an existing project in unity 5.2.1f1 1 Answer

Need help with basic networking 2 Answers

Questions about a chat system architecture 1 Answer


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