Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-----------------------------+------------------------------\
- | |
- | !!!NOTICE!!! |
- | |
- | These libraries are under heavy development so they are |
- | subject to make many changes as development continues. |
- | For this reason, the libraries may not be well commented. |
- | THANK YOU for supporting forge with all your feedback |
- | suggestions, bug reports and comments! |
- | |
- | - The Forge Team |
- | Bearded Man Studios, Inc. |
- | |
- | This source code, project files, and associated files are |
- | copyrighted by Bearded Man Studios, Inc. (2012-2015) and |
- | may not be redistributed without written permission. |
- | |
- \------------------------------+-----------------------------*/
- using UnityEngine;
- using BeardedManStudios.Network;
- using System.Collections.Generic;
- namespace BeardedManStudios.Forge.Examples
- {
- public class ForgeExample_Move : NetworkedMonoBehavior
- {
- [NetSync("Print", NetworkCallers.Everyone)]
- public int number = 0;
- public TextMesh nameMesh = null;
- private BMSByte rawMessageCache = new BMSByte();
- public void Print()
- {
- Debug.Log("The number has been updated to " + number);
- }
- protected override void NetworkStart()
- {
- // Always call the base Start method
- base.NetworkStart();
- if (!IsOwner)
- return;
- // Read raw data from the network
- OwningNetWorker.rawDataRead += OwningNetWorker_rawDataRead;
- }
- private void OwningNetWorker_rawDataRead(NetworkingPlayer sender, BMSByte data)
- {
- // In this test we are just writing a string across the network
- string message = System.Text.Encoding.UTF8.GetString(data.byteArr, data.StartIndex(), data.Size);
- Debug.Log("Hello " + message);
- }
- protected override void OwnerUpdate()
- {
- base.OwnerUpdate();
- if (Input.GetKeyDown(KeyCode.B))
- AssignName("Brent");
- else if (Input.GetKeyDown(KeyCode.F))
- AssignName("Farris");
- if (Input.GetKey(KeyCode.UpArrow))
- transform.position += Vector3.up * 5.0f * Time.deltaTime;
- if (Input.GetKey(KeyCode.DownArrow))
- transform.position += Vector3.down * 5.0f * Time.deltaTime;
- if (Input.GetKey(KeyCode.LeftArrow))
- transform.position += Vector3.left * 5.0f * Time.deltaTime;
- if (Input.GetKey(KeyCode.RightArrow))
- transform.position += Vector3.right * 5.0f * Time.deltaTime;
- // Send a raw message across the network (total of 13 bytes)
- if (Input.GetKeyDown(KeyCode.E))
- {
- rawMessageCache.Clone(System.Text.Encoding.UTF8.GetBytes("Brent Farris"));
- Networking.WriteRaw(OwningNetWorker, rawMessageCache);
- }
- if (Input.GetKeyDown(KeyCode.Space))
- number++;
- }
- private void AssignName(string newName)
- {
- NetworkingManager.Instance.SetName(newName);
- RPC("UpdatePlayers");
- }
- [BRPC]
- private void UpdatePlayers()
- {
- NetworkingManager.Instance.PollPlayerList(GetPlayers);
- }
- private void GetPlayers(List<NetworkingPlayer> players)
- {
- CallOnMainThread((object[] args) =>
- {
- // Note: This is not optimal, it is just for the idea
- foreach (KeyValuePair<ulong, SimpleNetworkedMonoBehavior> kv in networkedBehaviors)
- {
- if (kv.Value is ForgeExample_Move)
- {
- foreach (NetworkingPlayer player in players)
- {
- if (kv.Value.OwnerId == player.NetworkId)
- {
- ((ForgeExample_Move)kv.Value).nameMesh.text = !string.IsNullOrEmpty(player.Name) ? player.Name : "Client";
- break;
- }
- }
- }
- }
- }, null);
- }
- private void OnApplicationQuit()
- {
- Networking.Destroy(this);
- }
- private void OnGUI()
- {
- if (!IsOwner)
- return;
- GUILayout.Space(25);
- // The server NetworkingManager object controls how fast the client's times are updated
- GUILayout.Label("The current server time is: " + NetworkingManager.Instance.ServerTime);
- GUILayout.Label("Press B or F to assign your name across the network.");
- GUILayout.Label("This will also updated get the latest list of players");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment