zoukankan      html  css  js  c++  java
  • 【Unity3d】WWW类发起web连接

    初学unity3d,解决一个游戏与web服务器连接问题。

    看了项目中原始代码,发现每次之前的程序员每次调用WWW类都需要写一遍StartCoroutine,然后各种重复代码。

    于是写了一个简单的封装类,先记录下来,将来慢慢优化。

    如何使用:

         WWWConnection connection = new WWWConnection(url, form);  //url: 地址   ,   form: WWWForm--Unity3d用于封装参数的类
    	connection.connectionEvent += new WWWConnection.Callback(GetResult);  //注册回调方法
    	StartCoroutine(connection.Get());  //启动网络连接
    
    	void GetResult(WWWConnection connection)  //回调方法
    	{
    		Debug.Log(connection.content);
    	}
    

      

    using UnityEngine;
    using System.Collections;
    
    public class WWWConnection
    {
    	public WWWConnection(string url)
    	{
    		this.www = new WWW(url);
    	}
    	
    	public WWWConnection(string url, WWWForm form)
    	{
    		this.www = new WWW(url, form);
    	}
    	
    	private WWW www;
    	
    	public bool isDone{
    		get{return IsDone();}
    	}
    	
    	public float progress{
    		get{return Progress ();}
    	}
    	
    	public string content{
    		get{return Content ();}
    	}
    	
    	public delegate void Callback(WWWConnection connection);
    	
    	public event Callback connectionEvent;
    	
    	public IEnumerator Get()
    	{
    		yield return this.www;
    		if(this.connectionEvent != null)
    		{
    			connectionEvent(this);
    		}
    	}
    	
    	private bool IsDone()
    	{
    		return this.www.isDone;
    	}
    	
    	private float Progress()
    	{
    		return this.www.progress;
    	}
    	
    	private string Content()
    	{
    		return this.www.text;
    	}
    	
    	public WWW GetWWW()
    	{
    		return this.www;
    	}
    	
    }
    

      

  • 相关阅读:
    上海社保,统筹内不能转出的疑惑
    c# 代理IP获取通用方法
    element-ui 的el-button组件中添加自定义颜色和图标的实现方法
    前端实现打印功能
    elementUI表格合并单元格
    webpack打包图片资源找不到问题
    Webstorm/IntelliJ Idea 过期破解方法
    ES6 Promise 用法讲解
    移动端开发
    Stylus的基础用法
  • 原文地址:https://www.cnblogs.com/LoadingChan/p/3370630.html
Copyright © 2011-2022 走看看