I am developing a Unity game that will run in the browser. It runs fine but I need to call a C# function from my Vue component and send a string to the WebGL app. For some reason, this does not work. If I provide the Scene name as an argument, I get an error that the scene does not exist. If I provide the GameObject name, I get an error that the method I'm calling doesn't exist. I initally had the unityContext.send
function called right after I instantiate the unityContext variable but that didn't work either so I set it up as a button to see if it's a loading thing where the function was being called before objects in the scene had loaded. It doesn't work more in this format when I click on the button... Any help would be greatly appreciated!
<script setup>
import { shallowRef } from "vue";
import { useRouter, useRoute } from "vue-router";
import useTimelineStepper from "@/composables/timelinestepper";
import useSmileStore from "@/stores/smiledata"; // get access to the global store
import UnityWebgl from "unity-webgl";
import UnityVue from "unity-webgl/vue";
const router = useRouter();
const route = useRoute();
const smilestore = useSmileStore();
const { next, prev } = useTimelineStepper();
if (route.meta.progress) smilestore.global.progress = route.meta.progress;
// assign conditions here for testing
const conditions = ["level1"];
// load unity
const unityContext = new UnityWebgl({
loaderUrl: "/unity/Build/creative-physical-reasoning.loader.js",
dataUrl: "/unity/Build/creative-physical-reasoning.data",
frameworkUrl: "/unity/Build/creative-physical-reasoning.framework.js",
codeUrl: "/unity/Build/creative-physical-reasoning.wasm",
});
// setup event listener for button click
function loadData() {
console.log("loadData");
unityContext.send("Menu", "LoadAllLevels", JSON.stringify(conditions));
}
function finish(goto) {
// smilestore.saveData()
unityContext.unload();
if (goto) router.push(goto);
}
</script>
<template>
<div class="page">
<UnityVue :unity="unityContext" width="1080" height="675" />
<button class="button is-success is-light" id="finish" @click="finish(next())">
next <FAIcon icon="fa-solid fa-arrow-right" />
</button>
<button class="button is-success is-light" id="loadData" @click="loadData()">
load data
</button>
</div>
</template>