Score









How do I use Score
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: GameManager.cs Copy
19  public static void Score(string wallID) {
20   if (wallID == "RightWall") {
21    PlayerScore1++;
22   } else {
23    PlayerScore2++;
24   }
25  }
File name: GameManager.cs Copy
27  void OnGUI() {
28   GUI.skin = layout;
29   GUI.Label (new Rect (Screen.width / 2 - 150 - 12, 20, 100, 100), "" + PlayerScore1);
30   GUI.Label (new Rect (Screen.width / 2 + 150 + 12, 20, 100, 100), "" + PlayerScore2);
31
32   if (GUI.Button (new Rect (Screen.width / 2 - 60, 35, 120, 53), "RESTART")) {
33    PlayerScore1 = 0;
34    PlayerScore2 = 0;
35    theBall.SendMessage ("RestartGame", 0.5f, SendMessageOptions.RequireReceiver);
36   }
37
38   if (PlayerScore1 == 10) {
39    GUI.Label (new Rect (Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER ONE WINS");
40    theBall.SendMessage ("ResetBall", null, SendMessageOptions.RequireReceiver);
41   } else if (PlayerScore2 == 10) {
42    GUI.Label (new Rect (Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER TWO WINS");
43    theBall.SendMessage ("ResetBall", null, SendMessageOptions.RequireReceiver);
44   }
45  }
File name: SideWalls.cs Copy
7  void OnTriggerEnter2D(Collider2D hitInfo) {
8   if (hitInfo.name == "Ball")
9   {
10    string wallName = transform.name;
11    GameManager.Score (wallName);
12    hitInfo.gameObject.SendMessage ("RestartGame", 1, SendMessageOptions.RequireReceiver);
13   }
14  }
File name: HubGui.cs Copy
28     void OnGUI()
29     {
30         GUI.skin = this.Skin;
31         GUILayout.Space(10);
32
33         GUILayout.BeginHorizontal();
34         GUILayout.Space(10);
35         scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37         GUILayout.Label("Basics", m_Headline);
38         if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39         {
40             demoDescription = "Demo Boxes\n\nUses ConnectAndJoinRandom script.\n(joins a random room or creates one)\n\nInstantiates simple prefab.\nSynchronizes positions without smoothing.";
41             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42         }
43         if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44         {
45             demoDescription = "Demo Worker\n\nJoins the default lobby and shows existing rooms.\nLets you create or join a room.\nInstantiates an animated character.\nSynchronizes position and animation state of character with smoothing.\nImplements simple in-room Chat via RPC calls.";
46             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47         }
48         if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49         {
50             demoDescription = "Movement Smoothing\n\nUses ConnectAndJoinRandom script.\nShows several basic ways to update positions of remote objects.";
51             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52         }
53
54         GUILayout.Label("Advanced", m_Headline);
55         if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56         {
57             demoDescription = "Ownership Transfer\n\nShows how to transfer the ownership of a PhotonView.\nThe owner will send position updates of the GameObject.\nTransfer can be edited per PhotonView and set to Fixed (no transfer), Request (owner has to agree) or Takeover (owner can't object).";
58             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59             this.webLink = new DemoBtn();
60         }
61         if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62         {
63             demoDescription = "Pickup, Teams, Scores\n\nUses ConnectAndJoinRandom script.\nImplements item pickup with RPCs.\nUses Custom Properties for Teams.\nCounts score per player and team.\nUses PhotonPlayer extension methods for easy Custom Property access.";
64             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65             this.webLink = new DemoBtn();
66         }
67
68         GUILayout.Label("Feature Demos", m_Headline);
69         if (GUILayout.Button("Chat", GUILayout.Width(280)))
70         {
71             demoDescription = "Chat\n\nUses the Chat API (now part of PUN).\nSimple UI.\nYou can enter any User ID.\nAutomatically subscribes some channels.\nAllows simple commands via text.\n\nRequires configuration of Chat App ID in scene.";
72             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73             this.webLink = new DemoBtn();
74         }
75         if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76         {
77             demoDescription = "RPG Movement\n\nDemonstrates how to use the PhotonTransformView component to synchronize position updates smoothly using inter- and extrapolation.\n\nThis demo also shows how to setup a Mecanim Animator to update animations automatically based on received position updates (without sending explicit animation updates).";
78             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79             this.webLink = new DemoBtn();
80         }
81         if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82         {
83             demoDescription = "Mecanim Animations\n\nThis demo shows how to use the PhotonAnimatorView component to easily synchronize Mecanim animations.\n\nIt also demonstrates another feature of the PhotonTransformView component which gives you more control how position updates are inter-/extrapolated by telling the component how fast the object moves and turns using SetSynchronizedValues().";
84             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85             this.webLink = new DemoBtn();
86         }
87         if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88         {
89             demoDescription = "2D Game Demo\n\nSynchronizes animations, positions and physics in a 2D scene.";
90             this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91             this.webLink = new DemoBtn();
92         }
93         if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94         {
95             demoDescription = "Friends & Authentication\n\nShows connect with or without (server-side) authentication.\n\nAuthentication requires minor server-side setup (in Dashboard).\n\nOnce connected, you can find (made up) friends.\nJoin a room just to see how that gets visible in friends list.";
96             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97             this.webLink = new DemoBtn();
98         }
99
100         GUILayout.Label("Tutorial", m_Headline);
101         if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102         {
103             demoDescription = "Marco Polo Tutorial\n\nFinal result you could get when you do the Marco Polo Tutorial.\nSlightly modified to be more compatible with this package.";
104             this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105             this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106         }
107         GUILayout.EndScrollView();
108
109         GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110         GUILayout.Label(demoDescription);
111         GUILayout.Space(10);
112         if (!string.IsNullOrEmpty(this.demoBtn.Text))
113         {
114             if (GUILayout.Button(this.demoBtn.Text))
115             {
116                 Application.LoadLevel(this.demoBtn.Link);
117             }
118         }
119         if (!string.IsNullOrEmpty(this.webLink.Text))
120         {
121             if (GUILayout.Button(this.webLink.Text))
122             {
123                 Application.OpenURL(this.webLink.Link);
124             }
125         }
126         GUILayout.EndVertical();
127
128
129         GUILayout.EndHorizontal();
130     }
File name: OnPickedUpScript.cs Copy
6  public void OnPickedUp(PickupItem item)
7  {
8      if (item.PickupIsMine)
9      {
10          Debug.Log("I picked up something. That's a score!");
11          PhotonNetwork.player.AddScore(1);
12      }
13      else
14      {
15             Debug.Log("Someone else picked up something. Lucky!");
16      }
17  }
File name: PickupDemoGui.cs Copy
15     public void OnGUI()
16     {
17         if (!PhotonNetwork.inRoom)
18         {
19             return;
20         }
21
22
23         if (this.ShowScores)
24         {
25             GUILayout.Label("Your Score: " + PhotonNetwork.player.GetScore());
26         }
27
28
29         if (this.ShowDropButton)
30         {
31             foreach (PickupItem item in PickupItem.DisabledPickupItems)
32             {
33                 if (item.PickupIsMine && item.SecondsBeforeRespawn <= 0)
34                 {
35                     if (GUILayout.Button("Drop " + item.name))
36                     {
37                         item.Drop(); // drops the item at the place where it originates
38                     }
39
40                     GameObject playerCharGo = PhotonNetwork.player.TagObject as GameObject;
41                     if (playerCharGo != null && GUILayout.Button("Drop here " + item.name))
42                     {
43                         // drop the item at some other place. next to the user's character would be fine...
44                         Vector3 random = Random.insideUnitSphere;
45                         random.y = 0;
46                         random = random.normalized;
47                         Vector3 itempos = playerCharGo.transform.position + this.DropOffset * random;
48
49                         item.Drop(itempos);
50                     }
51                 }
52             }
53         }
54
55
56         if (this.ShowTeams)
57         {
58             foreach (var teamName in PunTeams.PlayersPerTeam.Keys)
59             {
60                 GUILayout.Label("Team: " + teamName.ToString());
61                 List teamPlayers = PunTeams.PlayersPerTeam[teamName];
62                 foreach (PhotonPlayer player in teamPlayers)
63                 {
64                     GUILayout.Label(" " + player.ToStringFull() + " Score: " + player.GetScore());
65                 }
66             }
67
68             if (GUILayout.Button("to red"))
69             {
70                 PhotonNetwork.player.SetTeam(PunTeams.Team.red);
71             }
72             if (GUILayout.Button("to blue"))
73             {
74                 PhotonNetwork.player.SetTeam(PunTeams.Team.blue);
75             }
76         }
77     }
File name: PunPlayerScores.cs Copy
15     public static void SetScore(this PhotonPlayer player, int newScore)
16     {
17         Hashtable score = new Hashtable(); // using PUN's implementation of Hashtable
18         score[PunPlayerScores.PlayerScoreProp] = newScore;
19
20         player.SetCustomProperties(score); // this locally sets the score and will sync it in-game asap.
21     }
File name: PunPlayerScores.cs Copy
23     public static void AddScore(this PhotonPlayer player, int scoreToAddToCurrent)
24     {
25         int current = player.GetScore();
26         current = current + scoreToAddToCurrent;
27
28         Hashtable score = new Hashtable(); // using PUN's implementation of Hashtable
29         score[PunPlayerScores.PlayerScoreProp] = current;
30
31         player.SetCustomProperties(score); // this locally sets the score and will sync it in-game asap.
32     }
File name: PunPlayerScores.cs Copy
34     public static int GetScore(this PhotonPlayer player)
35     {
36         object teamId;
37         if (player.customProperties.TryGetValue(PunPlayerScores.PlayerScoreProp, out teamId))
38         {
39             return (int)teamId;
40         }
41
42         return 0;
43     }
File name: Hud.cs Copy
29         protected override void Start()
30         {
31             base.Start();
32
33             GameService.OnGameStartSignal.AddListener(OnGameStart);
34             GameService.OnGameResultSignal.AddListener(UpdateGameScore);
35             GameService.OnGameQuitSignal.AddListener(Hide);
36         }

Download file with original file name:Score

Score 126 lượt xem

Gõ tìm kiếm nhanh...