OnClickInstantiate.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="OnClickInstantiate.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Utilities
  4. // </copyright>
  5. // <summary>A compact script for prototyping.</summary>
  6. // <author>developer@exitgames.com</author>
  7. // --------------------------------------------------------------------------------------------------------------------
  8. namespace Photon.Pun.UtilityScripts
  9. {
  10. using UnityEngine;
  11. using UnityEngine.EventSystems;
  12. /// <summary>
  13. /// Instantiates a networked GameObject on click.
  14. /// </summary>
  15. /// <remarks>
  16. /// Gets OnClick() calls by Unity's IPointerClickHandler. Needs a PhysicsRaycaster on the camera.
  17. /// See: https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.html
  18. /// </remarks>
  19. public class OnClickInstantiate : MonoBehaviour, IPointerClickHandler
  20. {
  21. public enum InstantiateOption { Mine, Scene }
  22. public PointerEventData.InputButton Button;
  23. public KeyCode ModifierKey;
  24. public GameObject Prefab;
  25. [SerializeField]
  26. private InstantiateOption InstantiateType = InstantiateOption.Mine;
  27. void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
  28. {
  29. if (!PhotonNetwork.InRoom || (this.ModifierKey != KeyCode.None && !Input.GetKey(this.ModifierKey)) || eventData.button != this.Button)
  30. {
  31. return;
  32. }
  33. switch (this.InstantiateType)
  34. {
  35. case InstantiateOption.Mine:
  36. PhotonNetwork.Instantiate(this.Prefab.name, eventData.pointerCurrentRaycast.worldPosition + new Vector3(0, 0.5f, 0), Quaternion.identity, 0);
  37. break;
  38. case InstantiateOption.Scene:
  39. PhotonNetwork.InstantiateRoomObject(this.Prefab.name, eventData.pointerCurrentRaycast.worldPosition + new Vector3(0, 0.5f, 0), Quaternion.identity, 0, null);
  40. break;
  41. }
  42. }
  43. }
  44. }