Search Unity

How do I output a value to a JS file?

Discussion in 'Scripting' started by Dunsec, Jun 21, 2016.

  1. Dunsec

    Dunsec

    Joined:
    Apr 3, 2015
    Posts:
    5
    Hi,

    I've been using Unity for a while but I am completely new to the WebGL side of things.

    I want to start off simple.

    I want to make a scene with a cube, when the cube is clicked it adds a value of 1 to an int, what's the best way of going with this? Could some one show me a script for me to study or point me in a direction?

    Would it write to a JS script on the server?

    Best,
    Sec
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You basically want to talk to the outlaying javascript on the page?

    http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html

    Have a look at the link, it is about how to communicate back and forth from javascript on the web page to the app. To talk to a server, you would use an XHTTP communicator(ajax). You could also use an iframe, but that is way old school.
     
  3. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289

    Here is some of the code you might need:



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class CubeClicker : MonoBehaviour {
    6.     public Text clickText;
    7.     int counter = 0;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.  
    17.     }
    18.  
    19.     void OnMouseDown()
    20.     {
    21.         counter++;
    22.         clickText.text = "Clicks: " + counter;
    23.     }
    24.  
    25. }
    Also, here is a video of the progress: