Guest User

Untitled

a guest
Jan 11th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using Mirror;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class HookTest : NetworkBehaviour
  7. {
  8.     static int s_num;
  9.  
  10.     [SyncVar(hook = nameof(SetMyField))]
  11.     bool _myField;
  12.  
  13.     int _runCount;
  14.  
  15.     int _myPlayerNumber;
  16.  
  17.     void Start()
  18.     {
  19.         _myPlayerNumber = s_num;
  20.  
  21.         s_num++;
  22.     }
  23.  
  24.     void SetMyField(bool oldValue, bool newValue)
  25.     {
  26.         _runCount++;
  27.         Debug.Log("I got run: " + newValue);
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update()
  32.     {
  33.         if (base.isLocalPlayer)
  34.         {
  35.             if (Input.GetKeyDown(KeyCode.Y))
  36.             {
  37.                 _myField = !_myField;
  38.             }
  39.         }
  40.     }
  41.  
  42.     void OnGUI()
  43.     {
  44.         if (base.isLocalPlayer)
  45.             GUI.Label(new Rect(200, 200, 100, 100), "Local: " + _runCount.ToString());
  46.         else
  47.             GUI.Label(new Rect(300, 200 + _myPlayerNumber * 50, 100, 100), "Remote: " + _runCount.ToString());
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment