Using the photon Engine for Unity Network game Development (IV)--photon engine implements network game logic

Source: Internet
Author: User
Tags unity network

Using the photon Engine for Unity Network game Development (IV)--photon engine realization Network game Logic photonpununity Network game Development Network game logic Processing and masterclient network game logic Processing: Method One: Write Photon server and client program

The client passes the data to the server, the server completes the logical judgment, and returns the result to the client

Method Two: Write the client program

The game logic is handled on the client, the photon server is only responsible for data validation and delivery between the clients

Master Client
    • When using the client to process the network game logic, we must ensure that only one client in the game room handles the network game logic, we select a client to handle the game logic on the network.
    • This client handles network game logic based on data sent by other clients. After that, the processing results are sent to all clients for synchronization
PUN Master Client

In pun client clients, which contains a masterclient, we use the masterclient to handle the network game logic

    • Masterciient is a special client in pun and has only one masterclient in each game room.
    • Masterciient has higher privileges than normal clients, including kicking other clients out of the game room, managing network game scene objects, etc.
    • When Masterclient leaves the room, Gameserver automatically selects the client with the lowest ID as the Masterciient
    • Use Photonnetwork.ismasterciient to determine if the local client is Masterciient
Player Object Generation

Creating a Player object in photon requires invoking the following method instantiation.

    • Resourcesprefabname: Need to instantiate prefab under Resources file under Name
    • Position: The spawn point of the object to instantiate
    • Quaternion: Need to instantiate rotation information for an object
    • Group: Group information, easy to distinguish between the objects, such as distinguish between us and the enemy photonnetwork.instantiate ("Resourcesprefabname", Position,quaternion, Group)
      For example:
    //Generate Player Object     void instanceplayer()     {playercustomproperties = PhotonNetwork.player.CustomProperties;if(playercustomproperties["Team"]. ToString (). Equals ("TEAM1") {Localplayer = Photonnetwork.instantiate ("Myplayer", teamonesptrans[(int) playercustomproperties["Teamnum"]].position, Quaternion.identity,0); }//If player belongs to Team 2, generate Robotplayer object         Else if (photonnetwork.player.customproperties["Team"). ToString (). Equals        ("Team2")) {Localplayer = Photonnetwork.instantiate ("Myplayer", teamtwosptrans[(int) playercustomproperties["Teamnum"]].position, Quaternion.identity,0);    } localplayer.name = PhotonNetwork.player.NickName; }
Game object State synchronization position toward information synchronization method one: By dragging the Transfrom component onto the observed components property on the Photonview component. Although this method transmits the position of the object rotation information, but due to the existence of communication interval, resulting in the object motion discontinuous method two: using Photontransformview to achieve the player position and direction of synchronization.

The Photontransformview component incorporates several interpolation algorithms that enable the player to "move smoothly" using the built-in interpolation method.

Method Three: Use the Onphotonserializeview (Photonstream,photonmessageinfo) method to synchronize

When using this method, the interpolation of position and rotation should be handled on its own.

 private void onphotonserializeview(Photonstream stream, Photonmessageinfo info)       {if(stream.iswriting) {//we Own This player:send the others our data... stream.              Sendnext (transform.position); Stream.          Sendnext (transform.rotation); }Else{              ... Playerposition = (Vector3) stream.              Receivenext (); Playerrotation = (quaternion) stream.          Receivenext (); }      }





Animation information synchronization method one: For animations using animator components, use Photonanimatorview to develop quickly.


Method Two: Use the Onphotonserializeview (Photonstream,photonmessageinfo) method to synchronize. This method is used to pass the value of the animation parameter to synchronize the animation, which is applicable to the animation component animation.

Animation animations:
If the player has a control script for its animations, the animation is judged by the playerstate in the update function, where we pass playerstate to synchronize the animation

 private void onphotonserializeview(Photonstream stream, Photonmessageinfo info)       {if(stream.iswriting) {//we Own This player:send the others our data... stream.          Sendnext (player.playerstate); }Else{... player.playerstate = (playanimation) stream.          Receivenext (); }      }

Animator Animations:
The animator component controls the branching direction of the animation through various parameters, where we synchronize its parameters to achieve animation synchronization

 private void onphotonserializeview(Photonstream stream, Photonmessageinfo info)       {if(stream.iswriting) {//we Own This player:send the others our data... stream. Sendnext (ANI. GetFloat ("Runspeed")); Stream. Sendnext (ANI. Getbool ("Isjump")); }Else{... ani. SetFloat ("Runspeed",(string) stream.   Receivenext ()); Ani. Setbool ("Isjump",(BOOL) stream.        Receivenext ()); }      }

Using the photon Engine for Unity Network game Development (IV)--photon engine implements network game logic

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.