11using MLAPI . Attributes ;
22using MLAPI . MonoBehaviours . Core ;
3+ using MLAPI . NetworkingManagerComponents . Binary ;
34using UnityEngine ;
45using UnityEngine . UI ;
56
@@ -10,17 +11,31 @@ public class PlayerTest : NetworkedBehaviour
1011 public Text TextField ;
1112 public GameObject cubePrefab ;
1213 public GameObject spherePrefab ;
14+ public Material planeMaterial ;
1315
1416 public override void NetworkStart ( )
1517 {
16- if ( isServer )
18+ if ( isServer && isLocalPlayer )
1719 MySyncedName = "SyncVarTest: " + Random . Range ( 50 , 10000 ) ;
20+
21+ planeMaterial = GameObject . Find ( "Plane" ) . GetComponent < MeshRenderer > ( ) . material ;
22+ if ( isClient )
23+ RegisterMessageHandler ( "OnChangeColor" , OnChangeColor ) ;
24+ }
25+
26+ private void OnChangeColor ( uint clientId , byte [ ] data )
27+ {
28+ BitReader reader = new BitReader ( data ) ;
29+ float r = reader . ReadFloat ( ) ;
30+ float g = reader . ReadFloat ( ) ;
31+ float b = reader . ReadFloat ( ) ;
32+ planeMaterial . color = new Color ( r , g , b ) ;
1833 }
1934
2035 private void OnGUI ( )
2136 {
2237 int y = 25 ;
23- if ( isServer )
38+ if ( isServer && isLocalPlayer )
2439 {
2540 y += 25 ;
2641 if ( GUI . Button ( new Rect ( 200 , y , 200 , 20 ) , "Change Text with SyncVar" ) )
@@ -43,25 +58,25 @@ private void OnGUI()
4358 go . transform . position = transform . position + new Vector3 ( 0 , 3f , 0 ) ;
4459 go . GetComponent < NetworkedObject > ( ) . Spawn ( ) ;
4560 }
61+
62+ y += 25 ;
63+ if ( GUI . Button ( new Rect ( 200 , y , 200 , 20 ) , "Set random plane color" ) )
64+ {
65+ planeMaterial . color = Random . ColorHSV ( ) ;
66+ using ( BitWriter writer = new BitWriter ( ) )
67+ {
68+ writer . WriteFloat ( planeMaterial . color . r ) ;
69+ writer . WriteFloat ( planeMaterial . color . g ) ;
70+ writer . WriteFloat ( planeMaterial . color . b ) ;
71+ SendToClientsTarget ( "OnChangeColor" , "ColorChannel" , writer . Finalize ( ) ) ;
72+ }
73+ }
4674 }
4775 }
4876
4977 private void Update ( )
5078 {
5179 TextField . text = MySyncedName ;
52- if ( isServer && Input . GetKeyDown ( KeyCode . Space ) && Input . GetKeyDown ( KeyCode . LeftShift ) && isLocalPlayer )
53- {
54- GameObject go = Instantiate ( cubePrefab ) ;
55- go . transform . position = transform . position + new Vector3 ( 0 , 1f , 0 ) ;
56- go . GetComponent < NetworkedObject > ( ) . Spawn ( ) ;
57- }
58- else if ( isServer && Input . GetKeyDown ( KeyCode . Space ) && isLocalPlayer )
59- {
60- MySyncedName = "SyncVarTest: " + Random . Range ( 50 , 10000 ) + " (Press space on server)" ;
61- GameObject go = Instantiate ( spherePrefab ) ;
62- go . transform . position = transform . position + new Vector3 ( 0 , 1f , 0 ) ;
63- go . GetComponent < NetworkedObject > ( ) . Spawn ( ) ;
64- }
6580 }
6681
6782}
0 commit comments