UnityWebgl接收Html传递来的参数

在Unity(所用Unity是2018.2.10版本)内部新建了一个Cube,Cube的标签设置为Player,新建一个脚本 TestCommunacation 。点击Cube,text文本会变为由Html传过来的数据。场景如图:

UnityWebgl接收Html传递来的参数_第1张图片脚本代码如下:

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

public class TestCommunication : MonoBehaviour
{
    Ray ray;
    RaycastHit hitInfo;
    public Text text;
    private void Update()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Input.GetMouseButtonDown(0))
        {
            if(Physics.Raycast(ray,out hitInfo))
            {
                if(hitInfo.collider.tag=="Player")
                {
                  //  GetInformation("12345");
                  Application.ExternalCall("PassInformation");//PassInformation是html的 Function方法
                }
            }
        }
    }
    private void GetInformation(string s)//与html进行交互
    {
        text.text = s;
    }
}

打包好的Html的修改后的代码如下:



  
    
    
    Unity WebGL Player | Communication
    
    
      
    
    
  
  
      

运行效果如下:

UnityWebgl接收Html传递来的参数_第2张图片

(附:判断Webgl运行在PC端还是手机端也需要Unity与Html传参,具体参考:https://blog.csdn.net/weixin_43779625/article/details/100308202)

项目工程和打包后的Html云盘链接:

链接:https://pan.baidu.com/s/1zqApkJFjylmtPmwdBAnQjA
提取码:ih1b

你可能感兴趣的:(U3D)