Unity3d allows gamers to go online and can be viewed by another player.

Source: Internet
Author: User

In terms of network, I like to write it in the form of C/S, but the official example is in the form of peer-to-peer. I am readingOfHourThen we encountered a problem. Next let's take a lookCodeIt was written in the form of C/S. It was changed by referring to the official example.

 1. Server: the server is responsible for generating servers. The code is very simple and the same as that in the chat room.

2.Client: it is also very simple. It just connects to the server, and then adds a code that instantiates a game object when the connection to the server is successful. Add a cube prefab prefabrication on the client end, add a networkview to the Cube prefabrication, And then pre-fabricate the cube to control the movement of the code segment.

Buy on behalf of the following:

 
Public var playerprefab: transform; function onconnectedtoserver () {spawnplayer ();} function spawnplayer () {var mynewtrans: Transform = network. instantiate (playerprefab, transform. position, transform. rotation, 0);} function onplayerdisconnected (Player: networkplayer) {debug. log ("Clean up after player" + player); network. removerpcs (player); network. destroyplayerobjects (player);} function ondisconnectedfromserver (Info: networkdisconnection) {debug. log ("clean up a bit after server quit"); network. removerpcs (network. player); network. destroyplayerobjects (network. player); application. loadlevel (application. loadedlevel );}

3. Open the server, open the two customer service terminals, and have two objects in the two customer service terminals respectively (the objects are overlapped and you can see them when you move one object in the same customer service end)

4. I found that many errors occurred these days due to the synchronization of networkview and repeated port numbers. If you have any problems, you can try these two aspects. In addition, when the client is forcibly disconnected, when the client connects to the server again, there will be one more client than all clients, so we 'd better not force the disconnection. If the client has been forcibly disconnected, well, you can change the port number again.

5. I will write more data interaction information as I study it. Make progress together ~~~~ QQ: 397319689 (a bit stupid )~~~


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.