English Български
Sign in Free Portal THE ALL - homeFree Portal THE ALL

Welcome!

Free Portal THE ALL » Tutorials

The Shortest Way To Make Online 3D Multiplayer Game Using Unity3D and Photon Unity Networking 2 (PUN 2) For Free (C#) From Scratch

How To Make - The Easiest And The Simplest - Using C#

The Shortest Way To Make Online 3D Multiplayer Game Using Unity3D and Photon Unity Networking 2 (PUN 2) For Free (C#) From Scratch

ID: 83943 | views 352 views | Published by Martina Kostova on 2019-11-26 23:16:18

The Shortest Way To Make Online 3D Multiplayer Game Using Unity3D and Photon Unity Networking 2 (PUN 2) For Free (C#) From Scratch The Shortest Way To Make Online 3D Multiplayer Game Using Unity3D and Photon Unity Networking 2 (PUN 2) For Free (C#) From Scratch The Shortest Way To Make Online 3D Multiplayer Game Using Unity3D and Photon Unity Networking 2 (PUN 2) For Free (C#) From Scratch The Shortest Way To Make Online 3D Multiplayer Game Using Unity3D and Photon Unity Networking 2 (PUN 2) For Free (C#) From Scratch The Shortest Way To Make Online 3D Multiplayer Game Using Unity3D and Photon Unity Networking 2 (PUN 2) For Free (C#) From Scratch

Hello all,

Continue reading, if you want to do this (see image1). 

Download Tutorial Test Project: http://play.zamakabg.com/tut.rar

Introduction:

I am combining all tutorials I watched and all the things I learned.

The programs I use:

The things I use:

Controls:
 
W, A, S, D To Move; Space To Change Color;
 
Steps/Clicks:
  1. Register And Login In Unity Site: https://id.unity.com/en/conversations/f45978d7-e1d7-4742-8212-28ae35ac3a8201bf;
  2. Download And Install Unity, including installing Visual Studio: https://store.unity.com/?_ga=2.86306911.617366148.1574793661-2079394660.1574116211#plans-individual;
  3. Open Unity;
  4. Add New 3D Project and Open it;
  5. Importing The Network Plugin: Go To Asset Store (Window > Asset Store (Ctrl + 9));
  6. Do Same Login, If Have To;
  7. Click [Search],  Type pun 2 free And Press Enter;
  8. Click On The Result;
  9. Click On [Download/Import];
  10. Click [Import];
  11. When Ready, You Will See a PUN Wizard Window;
  12. Register And Login In Photon Site: https://dashboard.photonengine.com/en-US/account/signin;
  13. Visit Photon Dashboard: https://dashboard.photonengine.com/en-US/publiccloud
  14. Click [CREATE A NEW APP];
  15. Choose Photon Type: Photon PUN;
  16. Type Name - Write Some Name (For Example Your Project Name);
  17. Click [CREATE];
  18. Bellow The Name Of Your App Click On The App ID And Copy It (Don't Share It);
  19. Open Unity Again And Paste It In The AppID or Email Field;
  20. Click [Setup Project];
  21. Click [Close] (Close The Window Wizard);
  22. In Assets Click On The Search Bar And Type photonserver;
  23. Click On The PhotonServerSettings From The Search Result;
  24. Click On Settings > App Version;
  25. Type 0;
  26. Go To Fixed Region;
  27. For European Type eu       ;
  28. If You Don't Have a Camera Object To The Scene, Add It By GameObject > Camera. Click On The Camera Object And Change Y and Z Of The Transform Position To 2.27 And -9.62;
  29. If You Don't Have a Directional Light Object To The Scene, Add It By GameObject > Light > Directional Light;
  30. Create New Empty Game Object: GameObject > Create Empty;
  31. Rename It (F2) And Press Enter;
  32. [Add Component] --> New Script --> Name It --> [Create And Add];
  33. Right Mouse Button Click On It And Press Edit Script (Opens Visual Studio);
  34. using Photon.Pun; //allows using PhotonNetwork. and PunRPC
  35. using Photon.Realtime; //allows using RoomOptions
  36. Change MonoBehaviour with MonoBehaviourPunCallbacks //allows using functions like OnConnectedToMaster()
  37. List<string> listLogs = new List<string>();
  38. public GameObject player;
  39. void Start(){
            listLogs.Add("Welcome!");
            PhotonNetwork.ConnectUsingSettings();
        }
  40. public override void OnConnectedToMaster(){
            listLogs.Add("Connected to the Photon master server.");
            PhotonNetwork.JoinRandomRoom();
        }
  41. public override void OnJoinRandomFailed(short returnCode, string message){
            listLogs.Add("Tried to join a random room, but failed. There must be no open rooms available.");
            CreateRoom();
        }
  42. void CreateRoom(){
            listLogs.Add("Trying to create a new Room."); 
            int randomRoomName = Random.Range(0, 10000);
            RoomOptions roomOps = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = 10 };
            PhotonNetwork.CreateRoom("Room" + randomRoomName, roomOps);
        }
  43. public override void OnCreateRoomFailed(short returnCode, string message){
            listLogs.Add("Tried to create a new room, but failed. There must already be a room with the same name."); 
            CreateRoom();
        }
  44. public override void OnJoinedRoom(){
            listLogs.Add("Joined a room.");
            PhotonNetwork.Instantiate(player.name, Vector3.zero, Quaternion.identity);
        }
  45. void OnGUI(){
            foreach(string log in listLogs){
                GUILayout.Label(log);
            }
            if(PhotonNetwork.IsMasterClient){ 
                GUILayout.Label("You are the host.");
            }else{
                GUILayout.Label("You are not a host.");
            }
        }
  46. Go Back To Unity;
  47. Assets > Create > Folder;
  48. Type Resources And Press Enter;
  49. GameObject > 3D Object > Cube;
  50. Rename It (F2) To PhotonPlayer And Press Enter;
  51. Drag And Drop It Into The Resources Folder In Assets Folder;
  52. Delete it;
  53. Click on the prefab, that you dropped;
  54. Component > Physics > Rigidbody;
  55. Is Kinematic [v];
  56. Component > Photon Networking > Photon View;
  57. Component > Photon Networking > Photon Transform View;
  58. Drag It To The Observed Coponents Field Of The Photon View Component And Drop It;
  59. [Add Component] --> New Script --> Name It --> [Create And Add];
  60. Right Mouse Button Click And Press Edit Script;
  61. using Photon.Pun; //allows using PhotonNetwork. and PunRPC
  62. public float speed = 10f;
  63. void Update(){
           if(GetComponent<PhotonView>().IsMine){
               if(Input.GetKey(KeyCode.W)) GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position + Vector3.forward * speed * Time.deltaTime);

               if(Input.GetKey(KeyCode.S)) GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position - Vector3.forward * speed * Time.deltaTime);

               if(Input.GetKey(KeyCode.D)) GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position + Vector3.right * speed * Time.deltaTime);

               if(Input.GetKey(KeyCode.A)) GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position - Vector3.right * speed * Time.deltaTime);

               if(Input.GetKeyDown(KeyCode.Space)) GetComponent<PhotonView>().RPC("ChangeColorTo", RpcTarget.AllBuffered, new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)));
           }
        }

  64. [PunRPC]
        void ChangeColorTo(Vector3 color){
            GetComponent<Renderer>().material.color = new Color(color.x, color.y, color.z, 1f);
        }

  65. Go Back To Unity;
  66. Click on the empty object;
  67. Drag and drop the cube prefab to the Player variable;
  68. File > Build Settiings (Ctrl + Shift + B);

  69. [Player Settings];

  70. Resolution and Presentation > Display Resolution Dialog: Enabled;

  71. [Build];

  72. Create New Folder;

  73. Name It Builds For Example;

  74. Open It;

  75. Create New Folder;

  76. Name It PC For Example;

  77. Open It;

  78. [Select Folder];

  79. Screen Resolution: 800 x 600 [v] Windowed;

  80. [Play]

  81. Open YourProjectNameFolderBuildsPCYourProjectName.exe;

  82. [Play]

  83. Enjoy Testing!