- Home /
Webplayer interaction between 3d scene and text on webpage
Is it possible to interact between the objects of a scene and the text contained in the same webpage(which contains the player).
As an example, let's say I want to change the color of an object by clicking the word "color" on the webpage.(later maybe I will have also some css with different a styles that will update also the 3d view)
How can I achieve this? What is the best approach? Many thanks
Answer by taxvi · Dec 15, 2014 at 05:46 PM
sure mate, read the Unity browser communication manual, should work
Hi $$anonymous$$ate thanks a lot for your advice! Didn't see that before Just had a quick look at the manual and it looks pretty straight forward. At the moment in fact I am interested on how to Call unity webplayer content from the webpage(later I will do also the opposite).
<script type="text/javascript" language="javascript">
<!--
//initializing the WebPlayer
var u = new UnityObject2();
u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");
function SaySomethingToUnity()
{
u.getUnity().Send$$anonymous$$essage("$$anonymous$$yObject", "$$anonymous$$yFunction", "Hello from a web page!");
}
-->
</script>
However I was thinking, what about when I will have a scene with many objects? Do I have to create a variable for each one? Is it better to use arrays? Do I have to do it manually for each object? I was thinking maybe somone out there as written some scripts or even just a methodology walktroghout. $$anonymous$$any thanks and sorry but I am not a programmer although I can go trought scripts.
of course arrays are much more laconic. you can always use tags or layers to make things easier. create an empty object responsible only for communication with the browser. then in the communicator's start function use
List<GameObject> myObjects = GameObject.FindObjectsWithTag("browserBased");
note that you need to import System.Collections.Generic to use Lists. and lists are just the same as arrays with flexible size, u can add remove stuff from it dynamically. then put your desired objects under the tag "braowserBased" and wolah!
good thing is you'd need to call a single function from the browser. bad is you'd have to iterate through all the 'myObjects' on each call.
Wow thanks a lot taxvi! I will give it a go and let you know if succeed. There are so many other things I would like to implement but this will come later. $$anonymous$$aybe I should start a blog/wiki on the topic so that I can collect all the information in one place, also for other people that need to work in this way, there are many fields(not only games) where a web interface is preferred to GUI and I could not see many project oriented in this direction (am I wrong?).
Hi again, sorry... I did a quick test... I am using this into my page
<script type="text/javascript" language="javascript">
<!--
//initializing the WebPlayer
var u = new UnityObject2();
u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");
function SaySomethingToUnity()
{
u.getUnity().Send$$anonymous$$essage("$$anonymous$$yObject", "Colorchange", "");
}
-->
</script>
...
<button type="button" onclick="SaySomethingToUnity()">Click $$anonymous$$e!</button>
And this into my object script:
function Colorchange () {
// Set specular shader
renderer.material.shader = Shader.Find ("Diffuse");
// Set red specular highlights
renderer.material.SetColor ("_Color", Color.red);;
}
But nothing happen when I click the button.
???