Npc.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // idle object that rarely gets dirty
  2. using UnityEngine;
  3. namespace Mirror.Examples.BenchmarkIdle
  4. {
  5. public class Npc : NetworkBehaviour
  6. {
  7. // component to assign in inspector
  8. public Renderer rend;
  9. // the value to set dirty
  10. [SyncVar] ulong value;
  11. [Tooltip("Probability that this object just sleeps the whole time without ever getting dirty. (Npcs, Item drops, etc.)")]
  12. [Range(0, 1)] public float sleepingProbability = 0.80f; // 80% of the objects are sleeping
  13. bool sleeping;
  14. [Header("Colors")]
  15. public Color activeColor = Color.white;
  16. public Color sleepingColor = Color.red;
  17. public override void OnStartServer()
  18. {
  19. sleeping = Random.value < sleepingProbability;
  20. // color coding
  21. // can't do this in update, it's too expensive
  22. rend.material.color = sleeping ? sleepingColor : activeColor;
  23. }
  24. [ServerCallback]
  25. void Update()
  26. {
  27. // set dirty if not sleeping.
  28. // only counts as dirty every 'syncInterval'.
  29. if (!sleeping) ++value;
  30. }
  31. }
  32. }