使用Unity实现打开Windows.Forms窗口将选择的视频或图片上传到服务器,之后下载播放视频。 Unity2018引用System.Windows.Forms.dll发布报错

51 篇文章 1 订阅

使用Unity实现打开Windows.Forms窗口将选择的视频或图片上传到服务器,之后下载播放视频。 Unity2018引用System.Windows.Forms.dll发布报错

 1 unity 端代码

    拷贝窗口 dl 文件    System.Windows.Forms.dll

    来自于unity 安装路径下的  :  Editor\Data\Mono\lib\mono\2.0,  拷贝到plugins 文件下 

 
 

   打开Windows.Forms 窗口,选中后将图片或者视频上传到服务器c#代码

 using UnityEngine;

using System.Collections;

using System;

using System.Windows.Forms;

using System.IO;

using UnityEngine.UI;
using UnityEngine.Networking;

public class AddManager : MonoSingleton<AddManager>
{
	 private Texture2D img = null;
	 public Image image;
	 public Sprite sprite;

 
	private void Start()
	{
		//AddImage();
		//AddMovie();
	}
	#region   图片
	//======================================================图片=================================================================================
	public void AddImage()
	{

		OpenFileDialog od = new OpenFileDialog();

		 od.Title = "请选择图片";

		 od.Multiselect = false;

		od.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";

		//od.Title = "请选择视频";

		//od.Multiselect = false;

		//od.Filter = "视频文件(*.mp4,*.wav)|*.mp4;.wav";

		if (od.ShowDialog() == DialogResult.OK)
		{

			Debug.Log(od.FileName); 
			StartCoroutine(GetTexture("file://" + od.FileName));

		}

		 image.sprite = sprite;

	}

	//加载 
	IEnumerator GetTexture(string url)
	{

		WWW www = new WWW(url);
		yield return www;
		if (www.isDone && www.error == null)
		{

			 img = www.texture;
			 sprite = Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
			 image.sprite = sprite;
			 image.GetComponent<RectTransform>().sizeDelta = new Vector2(img.width, img.height);
			 Debug.Log(img.width+" "+img.height); 
			 byte[] date = img.EncodeToPNG();

			//byte[] date = www.bytes;
			 Debug.Log(date.Length);
			StartCoroutine(UploadTexture(date));
		}

	}

	// 上传 
	IEnumerator UploadTexture(byte[] date)
	{
		WWWForm form = new WWWForm();
		form.AddField("Name", "3");  //这个3代表上传成功后的图片名字为 3.png      这个Name是和服务器自检的,不用修改
		form.AddBinaryData("post", date);       //这个post是和服务器自检的,不用修改         tx.EncodeToPNG()为图片转的16进制数组(byte[])
		WWW www = new WWW("http://127.0.0.1:9090/UpLoad/UnityUpload.php", form);    //必须上传这个路径http://xxxxxxxx/UpLoad/UnityUpload.php,因为服务器里的接收在这个路径下
		yield return www;
		if (www.isDone || www.error == null)
		{
			Debug.Log("Form upload complete!");
		}
		else
		{
			Debug.Log(www.error);

		}
	}
	#endregion
	//======================================================视频=================================================================================
	public string movieName;
	public void AddMovie()
	{

		OpenFileDialog od = new OpenFileDialog();

		od.Title = "请选择视频";

		od.Multiselect = false;

		od.Filter = "视频文件(*.mp4)|*.mp4";

		if (od.ShowDialog() == DialogResult.OK)
		{

			Debug.Log(od.FileName);
			 
			int lastIndexOf = od.FileName.LastIndexOf('\\');
			if (lastIndexOf != -1)  //存在反斜杠
			{
				movieName = od.FileName.Substring(lastIndexOf + 1).Replace(".mp4", "").Trim();
				Debug.Log(movieName);
			}
			
			StartCoroutine(GetMovie("file://" + od.FileName));

		}

	}

	//加载 
	IEnumerator GetMovie(string url)
	{

		WWW www = new WWW(url);
		yield return www;
		if (www.isDone && www.error == null)
		{
			byte[] date = www.bytes;
			Debug.Log(date.Length);
			StartCoroutine(UploadVideo(date));
		}

	}

	//  上传  
	//  下载路径 http://127.0.0.1:9090/UpLoad/upload/3.mp4
	IEnumerator UploadVideo(byte[] date)
	{
		string movieNameFull = GameMgr.RoomID + movieName;
		WWWForm form = new WWWForm();
		form.AddField("Name", movieNameFull);  //这个3代表上传成功后的图片名字为 3.png      这个Name是和服务器自检的,不用修改
		form.AddBinaryData("post", date);     //这个post是和服务器自检的,不用修改         tx.EncodeToPNG()为图片转的16进制数组(byte[])

		WWW www = new WWW("http://127.0.0.1:9090/UpLoad/UnityUploadMovie.php", form);    //必须上传这个路径http://xxxxxxxx/UpLoad/UnityUpload.php,因为服务器里的接收在这个路径下
		yield return www;
		if (www.isDone && www.error == null)
		{
			Debug.Log("Form upload complete!");
			ToolTipManager.Instance.ShowTips("视频文件上传成功");
		}
		else
		{
			Debug.Log(www.error);
		}
	
	}


}

2 服务器端

      2.1安装phpstudy

      下载phpstudy并安装到服务器上 (使用阿里云win服务器    )

      链接:https://pan.baidu.com/s/1YejQkb_rlRWGFiAjDmsyHA 
      提取码:8cqf 

      双击 exe 安装

  点击管理:对端口号进行设置

 这里设置 9090    物理路径默认  安装路径即可

       把首页里面的 Apache.2.4.39 那一行后面的启动打开 开启后 Apache 为蓝色小三角

  2.2下载UpLoad

   是一些写好的服务器端PHP文件,可以接受客户端传送的字节流数据。保存到服务器固定的文件目录下       

   链接:https://pan.baidu.com/s/1g1k176wMeb-noHv8zJCriQ 
   提取码:mtsl

   然后解压完成,把这个整体文件夹拖拽到你的phpstudy根路径下面(根据自己安装phpstudy的路径来)

 如下图:

     如若上传成功的话,图片/ 视频放置的位置在C:/phpstudy_pro/WWW/UpLoad/upload这个文件夹下面,可以手动点开看

     图片下载使用的url为  http://xxxxxxxxxxx/UpLoad/upload/xx.png

     视频播放url  为: http://127.0.0.1:9090/UpLoad/upload/3.mp4

  2.3视频播放测试

     上传成功后,使用AVPro Media Player  插件         设置  如下  :     测试视频播放是可以的

 

3发布报错

运行正常,可是发布到PC端编译报错。

The Assembly Mono.WebBrowser is referenced by System.Windows.Forms.But the dll is not allowed to be included or could not be found.

修改下面参数

完成OK

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值