InteractScript.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. using VRC.Udon;
  5. using System;
  6. [Obsolete("Replaced with Linked Object System")]
  7. [UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
  8. [DisallowMultipleComponent]
  9. [AddComponentMenu("Artyom Scripting System/_DEPRECATED_/Interact Script")]
  10. public class InteractScript : UdonSharpBehaviour
  11. {
  12. public Animator[] animators; // Array to hold Animators
  13. public string[] parameterNames; // Array to hold corresponding parameter names
  14. public InteractionType interactionType = InteractionType.None; // Dropdown for interaction type
  15. public float temporaryDuration = 2.0f; // Duration for temporary interaction
  16. public int isOccupied;
  17. public GameObject audioObjectTrue; // GameObject to enable when the state changes to true
  18. public GameObject audioObjectFalse; // GameObject to enable when the state changes to false
  19. void Start()
  20. {
  21. isOccupied = 0;
  22. }
  23. private void UpdateAnims(bool state)
  24. {
  25. for (int i = 0; i < animators.Length; i++)
  26. {
  27. animators[i].SetBool(parameterNames[i], state);
  28. }
  29. // Enable or disable GameObjects based on the state
  30. if (state)
  31. {
  32. if (audioObjectFalse != null) audioObjectFalse.SetActive(false);
  33. if (audioObjectTrue != null) audioObjectTrue.SetActive(true);
  34. }
  35. else
  36. {
  37. if (audioObjectTrue != null) audioObjectTrue.SetActive(false);
  38. if (audioObjectFalse != null) audioObjectFalse.SetActive(true);
  39. }
  40. }
  41. public override void Interact()
  42. {
  43. Networking.SetOwner(Networking.LocalPlayer, gameObject); // Ensure the local player is the owner
  44. if (interactionType == InteractionType.Temporary)
  45. {
  46. isOccupied = isOccupied + 1;
  47. UpdateAnims(true);
  48. SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, nameof(NetworkUpdateAnims));
  49. SendCustomEventDelayedSeconds(nameof(RevertTemporaryInteraction), temporaryDuration);
  50. }
  51. else if (interactionType == InteractionType.Permanent)
  52. {
  53. isOccupied = isOccupied + 1;
  54. UpdateAnims(true);
  55. SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, nameof(NetworkUpdateAnims));
  56. }
  57. else if (interactionType == InteractionType.Toggle)
  58. {
  59. switch(isOccupied)
  60. {
  61. case 0:
  62. isOccupied = isOccupied + 1;
  63. UpdateAnims(true);
  64. break;
  65. case 1:
  66. isOccupied = 0;
  67. UpdateAnims(false);
  68. break;
  69. }
  70. SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, nameof(NetworkUpdateAnims));
  71. }
  72. }
  73. public void RevertTemporaryInteraction()
  74. {
  75. isOccupied = isOccupied - 1;
  76. if (isOccupied < 1)
  77. {
  78. UpdateAnims(false);
  79. }
  80. SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, nameof(NetworkUpdateAnims));
  81. }
  82. public void NetworkUpdateAnims()
  83. {
  84. bool state = isOccupied > 0;
  85. UpdateAnims(state);
  86. }
  87. }