Example #1
0
    void Start()
    {
        try
        {
            udpClientReceiver = new UdpClient(this.serverRecievePort);
            udpClientSender   = new UdpClient();
            //TODO: Change IPAddress.Any to use this.serverIP
            RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            listenForMessage();
        }
        catch (Exception e)
        {
            UnityEngine.Debug.LogException(e);
        }

        //ForTest
        Byte[] test1 = new Byte[1];
        test1[0] = 1;

        this.CH0_ChildUpdateHolder = new ChildUpdateHolder(CH0_GameObj);
        this.CH1_ChildUpdateHolder = new ChildUpdateHolder(CH1_GameObj);
        this.CH2_ChildUpdateHolder = new ChildUpdateHolder(CH2_GameObj);
        this.CH3_ChildUpdateHolder = new ChildUpdateHolder(CH3_GameObj);
        this.CH4_ChildUpdateHolder = new ChildUpdateHolder(CH4_GameObj);
        this.CH5_ChildUpdateHolder = new ChildUpdateHolder(CH5_GameObj);
        this.CH6_ChildUpdateHolder = new ChildUpdateHolder(CH6_GameObj);

        this.CH0_ChildUpdateHolder.configureToSendOnChannel("CH0", this);
        this.CH1_ChildUpdateHolder.configureToSendOnChannel("CH1", this);
        this.CH2_ChildUpdateHolder.configureToSendOnChannel("CH2", this);
        this.CH3_ChildUpdateHolder.configureToSendOnChannel("CH3", this);
        this.CH4_ChildUpdateHolder.configureToSendOnChannel("CH4", this);
        this.CH5_ChildUpdateHolder.configureToSendOnChannel("CH5", this);
        this.CH6_ChildUpdateHolder.configureToSendOnChannel("CH6", this);


        //TIP: Below are examples of how you can manually send messages to the Unity CVE bridge.
        //sendLocation(1, 2, 3, "CH0");
        //sendPosition(4, 5, 6, 7, 0, 9, "CH0");
        //sendOrientation(10, 11, 12, "CH0");
        //sendExtraParam("test", 13, "CH0");
    }
Example #2
0
    /// <summary>
    /// Called once per frame.
    ///
    /// Currently designed so that if messages are recieved more frequently than Update is called
    /// (i.e. at a rate higher than the framerate) then only the last message received is kept.
    /// This can be changed by extending the OnRecieve method, but for now there is no downside to this approach.
    /// </summary>
    private void Update()
    {
        if (messageReceived == true)
        {
            //Message message = Unity_CVE.Message.GetRootAsMessage(new ByteBuffer(receiveBytes));
            ChildUpdateHolder ObjectToUpdate = null;
            switch (this.lastMessage.Channel)
            {
            case "CH0":
                ObjectToUpdate = this.CH0_ChildUpdateHolder;
                break;

            case "CH1":
                ObjectToUpdate = this.CH1_ChildUpdateHolder;
                break;

            case "CH2":
                ObjectToUpdate = this.CH2_ChildUpdateHolder;
                break;

            case "CH3":
                ObjectToUpdate = this.CH3_ChildUpdateHolder;
                break;

            case "CH4":
                ObjectToUpdate = this.CH4_ChildUpdateHolder;
                break;

            case "CH5":
                ObjectToUpdate = this.CH5_ChildUpdateHolder;
                break;

            case "CH6":
                ObjectToUpdate = this.CH6_ChildUpdateHolder;
                break;

            default:
                //Channel is not one that is monitored.
                UnityEngine.Debug.Log("Received message on unwatched channel. Channel: " + this.lastMessage.Channel);
                break;
            }
            if (ObjectToUpdate != null)
            {
                //This switch is based on message type.
                switch (this.lastMessage.MessageDataType)
                {
                case Data.Location:
                    //this.lastMessage.GetMessageData<>();
                    Location loc = this.lastMessage.GetMessageData <Location>(new Location());
                    ObjectToUpdate.setLocation(loc.X, loc.Y, loc.Z);
                    UnityEngine.Debug.Log("Recieved loc: " + loc.X + loc.Y + loc.Z);
                    break;

                case Data.Position:
                    Position pos = this.lastMessage.GetMessageData <Position>(new Position());
                    ObjectToUpdate.setPosition(pos.X, pos.Y, pos.Z, pos.Roll, pos.Pitch, pos.Yaw);
                    UnityEngine.Debug.Log("Recieved pos: " + pos.X + pos.Y + pos.Z + pos.Roll + pos.Pitch + pos.Yaw);
                    break;

                case Data.Orientation:
                    Orientation ori = this.lastMessage.GetMessageData <Orientation>(new Orientation());
                    ObjectToUpdate.setOrientation(ori.Roll, ori.Pitch, ori.Yaw);
                    UnityEngine.Debug.Log("Recieved ori: " + ori.ToString());
                    break;

                case Data.ExtraParam:
                    ExtraParam ext = this.lastMessage.GetMessageData <ExtraParam>(new ExtraParam());
                    ObjectToUpdate.setExtraParam(ext.Name, ext.Value);
                    UnityEngine.Debug.Log("Recieved ext: " + ext.ToString());
                    break;

                default:
                    //Empty Message or Error.
                    UnityEngine.Debug.Log("Networking Script unable to determine the type of a recieved message.");
                    break;
                }
            }
            messageReceived = false;
        }
    }