x


Ingame Chat script

Hi, I found this script in one of unity demo projects...

    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; 
}

Don't know how to use it or apply it yet... Can someone please help?

more ▼

asked Nov 10 '10 at 03:58 PM

Antonios gravatar image

Antonios
9 8 8 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I believe you are talking about the Chat folder in Networking.zip that's out there in http://unity3d.com/support/resources/example-projects/networking-example

This folder comes with a demo scene called "test". If you open that, it'll tell you how this chat script is used. Essentially, you need an empty gameobject with this script added and with NetworkView. Create another empty gameobject with ConnectGUI script for initiating server/client. And you are good to go with a chat scene (using Unity's in-built networking; I'm assuming you've a PRO or 30 day license.)

more ▼

answered Nov 10 '10 at 10:02 PM

Tarun gravatar image

Tarun
71 3 3 8

I did the same as the demo, except that the demo has the window with the typing form... I don't! I replaced all missing components and stuff but I don't have the type box to type in!

Nov 11 '10 at 04:06 PM Antonios

When I do that, why appears to me the message:"MissingReferenceException: The object of type 'Texture2D' has been destroyed but you are still trying to access it.". It is supposed that the Texture2D is being taken from the GUIStyle, isn't it?. I suppose that the code lacks of something or needs something more...

Oct 20 '11 at 09:52 AM jorgon
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5106
x4396
x1035
x86

asked: Nov 10 '10 at 03:58 PM

Seen: 12745 times

Last Updated: Oct 20 '11 at 09:52 AM