博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现类似QQ对话聊天功能脚本
阅读量:5361 次
发布时间:2019-06-15

本文共 2912 字,大约阅读时间需要 9 分钟。

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

转载于:https://www.cnblogs.com/943711466qq/p/4631590.html

你可能感兴趣的文章
图片压缩
查看>>
Hadoop-2.6.5安装
查看>>
教你如何一步步将项目部署到Github
查看>>
关于Android圆形图片的一种优化方案(可以显示网络图片)
查看>>
Windows路由表详解
查看>>
.NET性能优化方面的总结
查看>>
Windows下文件夹扩展名
查看>>
今天早上6:00起来,每天晚上回来6点多已经天黑
查看>>
debian开启cgroup memory子系统
查看>>
信息收集
查看>>
SQL Server 中使用Convert来取得datetime数据类型样式(全)
查看>>
Python中list的拷贝问题
查看>>
Java学习第二周学习笔记
查看>>
SQL基本语句
查看>>
linux-Centos7安装python3并与python2共存
查看>>
redis 安装 yum install gcc tcl
查看>>
序时薄二次开发(新增按钮)
查看>>
PHP实现根据浏览器跳转不同语言页面代码
查看>>
四、XML语言学习(1)
查看>>
无线网络发射选址
查看>>