javascript - Unity WebGL SendMessage 到 Unity 函数错误对象 InGameCanvas/CoilValue 未找到

标签 javascript html unity-game-engine sendmessage unity-webgl

我一直在尝试使用 JavaScript 从网页向 Unity 函数发送消息,但在调用该函数时我仍然收到未找到的值。 关联的对象是 Canvas 的子对象,所以我不确定这是否是一个问题。我尝试了多种方法的组合来实现它

coilScore 是一个 float ,我只是传递数字。

我尝试到达子对象。

gameInstance.SendMessage('CoilValue', 'ReceiveCoilScore', coilScore);[![enter image description here][1]][1]
gameInstance.SendMessage('InGameCanvas/CoilValue', 'ReceiveCoilScore', coilScore);
gameInstance.SendMessage('InGameCanvas', 'ReceiveCoilScore', coilScore);
gameInstance.SendMessage('InGameCanvas.CoilValue', 'ReceiveCoilScore', coilScore);


Error: 
SendMessage: object InGameCanvas/CoilValue not found!  a4527c6b-a81a-4c23-8861-f3553ac675c8:8:48803
Type: number Total coilScore: 0.000001401 meta_monetization_handler.js:34:10

JavaScript snippet to call Unity function Unity Function Game Object Structure

编辑

为了清楚起见,我在此行下方添加了其他信息。

index.html 正在调用 .js 文件。在该 .js 文件中,我正在解析多个参数,但我想做的一项是将消息从 .js 文件发送到 Unity。

index.html

<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Moon Run</title>
    <link rel="shortcut icon" href="TemplateData/favicon.ico">
    <link rel="stylesheet" href="TemplateData/style.css">
    <script src="TemplateData/UnityProgress.javascript"></script>
    <script src="Build/UnityLoader.js"></script>
    <script>
        var gameInstance = UnityLoader.instantiate("gameContainer", "Build/Moon Run WebGL nonDev.json", {onProgress: UnityProgress});
    </script>
</head>
<body>
<div class="webgl-content">
    <div id="gameContainer" style="width: 1920px; height: 1080px"></div>
</div>
<script src="TemplateData/responsive.javascript"></script>
<script src="meta_monetization_handler.js"></script>
</body>
</html>

meta_monetization_handler.js

   function calcValue(data) {
    return (data.amount * (1 / Math.pow(10, data.assetScale)).toPrecision(data.assetScale)).toPrecision(data.assetScale);
}

if (typeof document.monetization == "undefined") {
    console.log('document.monetization not found :(');
} else {
    var info_div = document.querySelector('body > div > div.validator > div');
    var ilp_element = document.getElementById('ilpEnabled');
    var ilp_raw_init_el = document.getElementById('rawInit');
    var cur_pay_el = document.getElementById('currentPaymentAmt');
    var total_estimate_el = document.getElementById('totalEstimateAmt');
    var latest_json_el = document.getElementById('latestJson');

    document.monetization.addEventListener('monetizationstart', function(event) {
        info_div.style.backgroundColor = "rgba(63, 65, 68, 0.4)";
        ilp_element.innerHTML = "Interledger Enabled! (Meta-Tag)<br><br>:)  Thank You!<br>";
        ilp_raw_init_el.innerHTML = JSON.stringify(event.detail, null, 2).replace('{','').replace('}','');
    });

    document.monetization.addEventListener('monetizationprogress', function(event) {
        if (typeof this.runningTotal == "undefined") {
            this.runningTotal = 0;
        }
        var monetizationAmount = parseFloat(calcValue(event.detail));
        this.runningTotal += monetizationAmount;
        document.monetization.lastEventPayload = event.detail;
        cur_pay_el.innerHTML = "Latest ILP Payment: $" + monetizationAmount;
        total_estimate_el.innerHTML = "Estimated ILP Total: $" + this.runningTotal.toPrecision(4);
        latest_json_el.innerHTML = "Latest ILP Payment Response: " + JSON.stringify(event.detail);

    coilScore = this.runningTotal.toPrecision(4);
    console.log("Type: " + typeof coilScore  + " Total coilScore: " + coilScore );
    gameInstance.SendMessage('CoilValueTextObject', 'ReceiveCoilScore', coilScore);
    });
}

最佳答案

我更改了 Canvas 以匹配相机 View 。很久以后我还注意到 javascript 使用了 toPrecision(4) 并且我有一个接受 float 的函数。 toPrecision 正在将 float 转换为字符串,并使用字符串作为参数,因此它失败了。

解决方案:确保我发送的是一个字符串,并且接收端的函数接受一个字符串。

    public void ReceiveCoilScore(float newValue)
    {
        //gameSession.coilScore = newValue;
        //coilScoreText.text = string.Format("{0:N9}", gameSession.GetCoilScore());
        coilScore = newValue;
        coilScoreText.text = string.Format("{0:N9}", coilScore);
    }

将 float 转换为字符串

    coilScore = this.runningTotal.toPrecision(4);
    console.log("Type: " + typeof coilScore  + " Total coilScore: " + coilScore );
    gameInstance.SendMessage('CoilValueTextObject', 'ReceiveCoilScore', coilScore);

Canvas matches Camera

关于javascript - Unity WebGL SendMessage 到 Unity 函数错误对象 InGameCanvas/CoilValue 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57946009/

相关文章:

javascript - D3 Sankey 技术说明中节点的 x 位置

javascript - 如何在React Native中将State View样式宽度设置为+1,高度设置为+1?

javascript - 有没有办法重写这个基本函数,这样我就不会重复相同的行两次?

ios - Vuforia + Unity 为 iPhone X iOS 11.4 黑屏构建

javascript - "Cannot set color of undefined"是什么意思?

javascript - 哪些浏览器支持在 input[type ="file"] 元素上触发点击事件?

javascript - jQuery Click 函数,输入值长度和模式

html - DIV定位,内容总是溢出

android - 如何在Unity3d中制作相机摇摄js脚本

java - AdMob:CommandInvokationFailure:重新打包资源失败。详情请参阅控制台