示例#1
0
    /**
     * Update the game object by
     *  (1) TODO: updating the attributes of the gameobject in unity
     *  (2) updating the attributes of the models in DEVS
     */
    void updateObject()
    {
        // Parse form data
        GameObject form    = GameObject.Find("Parameters Form");
        GameObject content = form.transform.Find("Viewport/Content").gameObject;

        // Loop over all elements in form
        Dictionary <string, string> parameters = new Dictionary <string, string>();

        foreach (Transform child in content.transform)
        {
            // Loop to get input value
            foreach (Transform child2 in child)
            {
                if (child2.name == "InputField")
                {
                    parameters.Add(child.name, child2.gameObject.GetComponent <InputField>().text);
                }
            }
        }

        // TODO: for other data objects
        GameObject gameObj = GameObject.Find(idObj);

        if (gameObj.GetComponent <Train>() != null)
        {
            Train     train     = gameObj.GetComponent <Train>();
            TrainData trainData = train.train;

            foreach (KeyValuePair <string, string> parameter in parameters)
            {
                trainData.GetType().GetField(parameter.Key).SetValue(trainData, parameter.Value);
            }
            socketManager.send(string.Format("UPDATE_{0} {1}", train.currentTrack, JsonUtility.ToJson(trainData)));
        }
        else if (gameObj.GetComponent <Track>() != null)
        {
            Track     track     = gameObj.GetComponent <Track>();
            TrackData trackData = track.track;

            foreach (KeyValuePair <string, string> parameter in parameters)
            {
                trackData.GetType().GetField(parameter.Key).SetValue(trackData, parameter.Value);
            }
            socketManager.send(string.Format("UPDATE_{0} {1}", trackData.id, JsonUtility.ToJson(trackData)));
        }
        else if (gameObj.GetComponent <StartStation>() != null)
        {
            StartStation     startStation     = gameObj.GetComponent <StartStation>();
            StartStationData startStationData = startStation.startStation;

            foreach (KeyValuePair <string, string> parameter in parameters)
            {
                startStationData.GetType().GetField(parameter.Key).SetValue(startStationData, parameter.Value);
            }
            socketManager.send(string.Format("UPDATE_{0} {1}", startStationData.id, JsonUtility.ToJson(startStationData)));
        }
    }