等待Unity WebPlayer中的输入

时间:2014-08-27 12:02:16

标签: c# javascript winforms unity3d unity-web-player

我正在构建一个嵌入在Windows窗体应用程序中的Unity3d可视化系统。我可以来回地进行WebPlayer和C#通信。

WebPlayer需要能够根据用户的输入绘制任意数量的模型(例如,绘制30个足球,然后操纵它们)。当对象的数量是硬编码的时候,我可以绘制对象并操纵它们,但是如果在场景中绘制任何东西之前如何获取用户输入呢?

目前我有一个附加到主摄像头的脚本,该脚本对javascript函数进行外部调用,在该函数中我提示输入,检查输入是否大于0并在摄像头脚本的Update()中启动创建。

在统一编辑器中,当我按下播放时,没有任何事情发生,直到我修改公共变量,然后模型按预期创建,但在WebPlayer中,它只是在场景中绘制其他东西,就好像条件不是'因为整数仍为0,所以没有绘制模型。

请帮助我了解如何实现这一目标。谢谢

在主相机脚本中:

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;
    }
}

在网络播放器的javascript中:

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 );
}

最后在创建附加到中心点的对象的脚本中:

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);
    }        
}

0 个答案:

没有答案