AR说明书

暑假在校的最后一天,日期2017.8.14,总结一下上次作品的收获。
放图,hiahiahia~
AR说明书登陆窗口
Ps:背景没有换,因为在诸多背景中还是挑选的原生。

实现内容:实现账号的正常登录,扫描3D物体出现弹幕评价信息,登录即可发送弹幕,这是我首次尝试用unity与后台进行通信,虽然功能不算很强大,但是在unity方面又学到不少新的知识!!!

首先做的是发送界面,第一步在客户端实现弹幕的发送。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextItem : MonoBehaviour {

    public string text = "";//控件显示文字
    private Text currentText;//当前脚本所在的text控件
    public float speed;//弹幕移动的速度


    // Use this for initialization
    void Start () {
        currentText = GetComponent<Text>();//初始化

        //设置随机的字体及颜色
        currentText.text = text;
        currentText.color = Random.ColorHSV();

        //获取屏幕范围内的y随机坐标,这里没有做屏幕适配,free aspect举例
        float y = Random.Range(-200f, 200f);
        transform.localPosition = new Vector3(550f, y, 0);

    }

    // Update is called once per frame
    void Update () {
        if (speed != 0) {
            float x = transform.localPosition.x + speed * Time.deltaTime;
            transform.localPosition = new Vector3(x, transform.localPosition.y, 1);

            //超出屏幕销毁
            //if (transform.localPosition .x<-550f) {
            //    Destroy(gameObject);
            //}
            Destroy(this.gameObject, 20f);
        }
    }
}

当然这个东西一定要放在Text文字物体上啊,然后做成Prefab啊,以便一会发弹幕的时候调用啊,就像这样子啊~~~~
弹幕预制体

接下来先制作发送界面,这个简答了,直接上UI。
这里写图片描述
啊哈哈哈,是不是很简单,主要是代码啊啊啊啊~~~

下面来研究一下与后台通信的代码。我把他做成了一个带构造的实体类,方面传值和重复调用。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
/// <summary>
/// 创建一个能够联网的实体类
/// </summary>
public class PostHttp
{
    public string responseContent;

    public PostHttp(string url, string body, string contentType)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

        httpWebRequest.ContentType = contentType;
        httpWebRequest.Method = "POST";
        httpWebRequest.Timeout = 5000;

        byte[] btBodys = Encoding.UTF8.GetBytes(body);
        httpWebRequest.ContentLength = btBodys.Length;
        httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

        HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
        string responseContent = streamReader.ReadToEnd();

        httpWebResponse.Close();
        streamReader.Close();
        httpWebRequest.Abort();
        httpWebResponse.Close();

        //return responseContent;
        this.responseContent = responseContent;
    }
}

也是不大清楚这里写的什么意思,大概直接调用后台就会返回一个String类型的字符串,里面好像包含了uid,id,弹幕内容,大概就这些,嗯,emmmm。

按钮上面挂的脚本就是这样的。

    //发送弹幕按钮
    public void MoveTextSendButton()
    {
        if (LoginManager.LoginID != null)
        {
            Debug.LogWarning("此时的ID为"+LoginManager.LoginID);
            MoveTextID = LoginManager.LoginID;
            PostHttp posthttp = new PostHttp("http://123.207.38.205:8080/Instruction/SendServlet", "id=" + MoveTextID + "&danmu=" + moveTextStr, "application/x-www-form-urlencoded");
        }
        else
        {
            Debug.LogWarning("此时的ID为Null" + LoginManager.LoginID);
            StartCoroutine(Load());
        }
    }

    IEnumerator Load()
    {
        sendMessageText.text = "您还没有登录,即将跳转到登录页面.....";
        yield return new WaitForSeconds(1f);
        SceneManager.LoadScene("Login");
    }

但new出上次创建的PostHttp的实体,就可以把输入的内容发送到后台喽,是不是炒鸡简单。回头看一看刚才的PostHttp类,public PostHttp(string url, string body, string contentType),里面需要传入三个参数,这是后台给的,听后台老大的话准没错!

登录:
ip:8080/Instruction/LoginServlet
发送参数:username    password 
返回结果  true+用户id/false
传递方式:post

发送弹幕:
ip:8080/Instruction/SendServlet
发送参数:id(用户id) danmu
返回结果: true/false
传递方式:post


弹幕显示:
ip:8080/Instruction/DisplayServlet
发送参数:上次发送弹幕的最后一个id
返回结果:id(弹幕id) message(弹幕具体内容) uid(用户id)
传递方式:post 

需要什么功能传一些对应的数值就好了,上面只是示例,千万不要直接照搬我的啊,555555
今天还有些事情,有时间在更啦~


猜你喜欢

转载自blog.csdn.net/gsm958708323/article/details/77164571