var skin : GUISkin;

var showChat = false;
private var inputField = "";
private var display = true;
private var entries = ArrayList();
private var scrollPosition : Vector2;
var userName : String ;
var chatobject : Transform;
private var window = Rect(50, 100, 200, 300);

class ChatEntry
{
    var sender = "";
    var text = "";    
    var mine = true;
}

function Update () {

var go = GameObject.Find("Network");
   userName = go.GetComponent("NetworkConnection").playerName;

}

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(userName+": "+""+inputField , 1);
        networkView.RPC("ApplyGlobalChatText", RPCMode.Others, userName+": " + 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;    
}

实现类似QQ对话聊天功能脚本的更多相关文章

  1. 网页调启用qq对话聊天客服窗口的链接地址方法大全(包含移动端)

    z转自:    http://www.wazhuti.com/1781.html 在PC端,腾讯的QQ软件还是应用最为广泛的即时通讯工具了,除了网站自动的一些对话软件外,qq可以有效的将用户留存下来, ...

  2. 类似QQ侧滑菜单功能实现

    之前的那文章简单实现了菜单侧拉功能,但是做不到像QQ那样导航条和tabBar一起移动...之后在网上找资料,有了思路,就自个写了个demo试试水. 先创建QHLMainController控制器,并把 ...

  3. java网络编程(三):一个类似QQ的聊天程序

    客户端: package QQ; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import ...

  4. AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)

    代码地址:https://github.com/Luction/AndroidRichText AndroidRichText帮助实现像QQ,微信一样的,一个TextView里既有文字又有表情又有图片 ...

  5. 类似QQ的聊天工程

    首先建立一个html:<!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  6. 用C#代码实现类似QQ窗体的“上、左、右”停靠功能

    大家都知道QQ有一个自动停靠功能,即“上.左.右”,当你把窗体拖到屏幕边缘,然后移开鼠标它会自动缩放,然后只显示一小小点出来,我们仔细观察会发现其实它只露3像素左右的边缘,当你鼠标移上去它又会伸出来, ...

  7. 详解C# 网络编程系列:实现类似QQ的即时通信程序

    https://www.jb51.net/article/101289.htm 引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例来让大家更好的理解它们的工作原理以及怎样.Net ...

  8. [C# 网络编程系列]专题九:实现类似QQ的即时通信程序

    转自:http://www.cnblogs.com/zhili/archive/2012/09/23/2666987.html 引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例 ...

  9. 转:【专题九】实现类似QQ的即时通信程序

    引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例来让大家更好的理解它们的工作原理以及怎样.Net类库去实现它们的.为了让大家更好的理解我们平常中常见的软件QQ的工作原理,所以在 ...

随机推荐

  1. http权威指南

    我的plan :one day two hour,one week done!!!争取不超过2周看完

  2. linux 关闭防火墙

    ) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off ) 即时生效,重启后失效 开启: service iptables start ...

  3. FreeBSD 无线配置

    无线配置在 handbook 中早已有了,但 handbook 中采用写入配置文件的办法,缺乏灵活性,补充下 on the fly 的方法 假设网卡接口为 ath0 ifconfig wlan0 cr ...

  4. html5 EvnetSource 与 JSP页面结合使用

    最近一直在研究前端的技术,特别是html5的技术,本人觉得html5的发展将越来越好,废话少说.看到了EventSource,根据官方的说法是服务器向客户端的推送服务.自己想找一些例子看看,发现都是P ...

  5. 自建yum源及分组安装

    最近在研究一套自动化运维方向的框架,在想到远程安装软件包的时候,觉得有yum支持会更方便一些.主要思路是把程序员写的代码或程序打包成rpm,然后提交到自建yum源,并实现按组安装,本地yum源实现步骤 ...

  6. 系统安全:Nessus Home版安装使用

    1.安装  下载地址:http://www.tenable.com/products/nessus/select-your-operating-system#tos 安装命令:rpm -ivh  Ne ...

  7. 支付宝支付参数MD5签名

    protected String signMD5(List<String> paramNames, String key, String charset) throws Unsupport ...

  8. IIS7下使用4.0框架集成模式URLRewriter重写中文URL乱码问题

    解决方法: 1 url中中文先通过Server.UrlEncode编码 2 修改网站编码 3.更改URLRewriter.net中源码 找到RewriterEngine类中 private strin ...

  9. 自己修改的两个js文件

    sea-base.js /** * Sea.js 2.2.3 | seajs.org/LICENSE.md */ (function(global, undefined) { // Avoid con ...

  10. MyBatis入门学习教程-使用MyBatis对表执行CRUD操作

    上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...