0

I am building a Unity3d visualization system that is embedded in a windows forms application. I can get WebPlayer and C# communication back and forth.

The WebPlayer needs to be able to draw an arbitrary number of models based on the input from the user (eg. draw 30 soccer balls and then manipulate them). I can draw objects and manipulate them just fine when the number of objects is hard-coded, but how does one go about getting user input before anything is drawn in the scene?

Currently I have a script attached to the Main Camera which does an external call to a javascript function in which I prompt for input, checks if the input is greater than 0 and initiates creation in the Update() of the camera script.

In the unity editor when I press play nothing happens until I modify the public variable and then the models get created as expected, but in the WebPlayer it just goes on drawing other stuff in the scene as if the condition wasn't there and no models are drawn since the integer is still 0.

Please help me understand how to achieve this. Thank you

In the main camera script:

public int _numObjects;
public static _creationScript;
private static bool created = false;
public static GameObject _centrePoint;

void Awake()
{
    Application.ExternalCall("getNumberOfObjects");
    _centrePoint = Resources.Load("centrePoint") as GameObject;

    _creationScript = Resources.Load("creationScript") as CreationScript;
}

public void SetNumObjects(int numObjects)
{
    _numObjects = numObjects;
}
void Update()
{
    if (_numObjects> 40 && !created)
    {
        GameObject centrePoint = GameObject.Instantiate(_centrePoint, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0)) as GameObject;
        centrePoint.AddComponent<CreationScript>()._cameraScript = this;

        created = true;
    }
}

in javascript of the web player:

var numObjects;

function setNumObjects(num) { u.getUnity().sendMessage("Main Camera","SetNumObjects", num) }

function getNumObjects(){
    do{
        numObjects = parseInt(window.prompt("Please enter number (40-100)", ""), 10);
    }while(isNaN(numObjects) || numObjects > 100 || numObjects < 40);
    setNumObjects(numObjects );
}

and finally in the script that creates the objects which is attached to the centre point:

public int _numObjects;
public GameObject _model;
public GameObject[] _models;
public CameraScript _cameraScript;

void Start()
{
    _numObjects = _cameraScript._numObjects;        

    SetUpModels();
}

void SetUpModels()
{
    _models = new GameObject[_numObjects];

    _model = Resources.Load("model") as GameObject;

    for (int i = 0; i < _models.Length; i++)
    {
        _models[i] = Instantiate(_model, new Vector3(20, 0, 0), new Quaternion()) as GameObject;
        _models[i].RotateAround(this.transform.position, Vector3.up, i); //arrange in a circle
        _models[i].transform.LookAt(this.transform);
    }        
}
  • Aren't you approaching the problem the wrong way around? How about using the SendMessage functionality to inform a particular component how many objects to instantiate from your JavaScript? i.e. as explained here: http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html – Bart Aug 27 '14 at 12:09
  • Hi, I thought I was doing that with function setNumObjects(num) { u.getUnity().sendMessage("Main Camera","SetNumObjects", num) } – Alen Lovric Aug 27 '14 at 12:12
  • Oh geez, totally glanced over that somehow. But then don't do the `Application.ExternalCall("getNumberOfObjects");` in your Awake. In fact, don't do it at all. Just have something on your Javascript side call `setNumObjects` at runtime. A button or something. And then respond to that. – Bart Aug 27 '14 at 12:12
  • How do you go about doing that at runtime? – Alen Lovric Aug 27 '14 at 12:20
  • I've fixed the issue. For anyone faced with the same issue of the WebPlayer or any standalone build behaving differently, this is how to do it: – Alen Lovric Aug 29 '14 at 16:03
  • - Start with only one game object, attach a script to it, do any initialization necessary - immediately after run a script that is not attached to any game object and DOES NOT inherit from MonoBehaviour, use this script to set your game up further, get references to all the resources, etc. Make use of the static keyword and the new keyword (you can do it, as they do not inherit from MonoBehaviour) to initialise and set things up. – Alen Lovric Aug 29 '14 at 16:13
  • - as you create game objects, run other non attached scripts to instantiate those objects (again new keyword is helpful) and THEN attach scripts to them - make a utility script (non-attached) which holds all your object references in one place If you do it like this, piecemeal, you will avoid any script execution order problems – Alen Lovric Aug 29 '14 at 16:13

0 Answers0