Search Unity

Third Party Update dropdown for all in Unity with Photon

Discussion in 'Multiplayer' started by AnonyMousey99, May 2, 2021.

  1. AnonyMousey99

    AnonyMousey99

    Joined:
    Oct 10, 2020
    Posts:
    1
    Hi all,

    I'm trying to make an app where when the master client/ host selects an item from the dropdown menu it will be changed for all clients in the server, but I can only get it to update for the master client. This is my current attempt but honestly i've been at this whole project so long its little details like this that are breaking my brain. any help much appreicated!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Vuforia;
    5. using Photon.Pun;
    6. using Photon.Realtime;
    7. using UnityEngine.UI;
    8.  
    9. /// Copy of vuforia default trackable event handler. Customised to support toggled rendering in a shared networked room in conjunction with photon.
    10. public class NewTrackableEventHandler : MonoBehaviourPun, ITrackableEventHandler
    11. {
    12.     public GameObject AR1;
    13.     public GameObject AR2;
    14.     public GameObject dropdownMenu;
    15.  
    16.     #region PROTECTED_MEMBER_VARIABLES
    17.  
    18.     protected TrackableBehaviour mTrackableBehaviour;
    19.     protected TrackableBehaviour.Status m_PreviousStatus;
    20.     protected TrackableBehaviour.Status m_NewStatus;
    21.  
    22.  
    23.  
    24.     #endregion // PROTECTED_MEMBER_VARIABLES
    25.  
    26.     #region UNITY_MONOBEHAVIOUR_METHODS
    27.  
    28.     protected virtual void Start()
    29.     {
    30.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    31.         if (mTrackableBehaviour)
    32.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    33.  
    34.      
    35.     }
    36.  
    37.     protected virtual void OnDestroy()
    38.     {
    39.         if (mTrackableBehaviour)
    40.             mTrackableBehaviour.UnregisterTrackableEventHandler(this);
    41.     }
    42.  
    43.     #endregion // UNITY_MONOBEHAVIOUR_METHODS
    44.  
    45.     #region PUBLIC_METHODS
    46.  
    47.     /// <summary>
    48.     ///     Implementation of the ITrackableEventHandler function called when the
    49.     ///     tracking state changes.
    50.     /// </summary>
    51.     public void OnTrackableStateChanged(
    52.         TrackableBehaviour.Status previousStatus,
    53.         TrackableBehaviour.Status newStatus)
    54.     {
    55.         m_PreviousStatus = previousStatus;
    56.         m_NewStatus = newStatus;
    57.  
    58.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    59.             newStatus == TrackableBehaviour.Status.TRACKED ||
    60.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    61.         {
    62.             Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    63.             OnTrackingFound();
    64.         }
    65.         else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
    66.                  newStatus == TrackableBehaviour.Status.NO_POSE)
    67.         {
    68.             Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    69.             OnTrackingLost();
    70.         }
    71.         else
    72.         {
    73.             // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
    74.             // Vuforia is starting, but tracking has not been lost or found yet
    75.             // Call OnTrackingLost() to hide the augmentations
    76.             OnTrackingLost();
    77.         }
    78.     }
    79.  
    80.     #endregion // PUBLIC_METHODS
    81.  
    82.     #region PROTECTED_METHODS
    83.  
    84.  
    85.     // My custom code
    86.  
    87.     void DropdownHandler()
    88.     {
    89.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    90.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    91.  
    92.         Renderer rend = AR1.GetComponent<Renderer>(); //render mesh for ar models
    93.         Renderer rend2 = AR2.GetComponent<Renderer>();
    94.         rend2.enabled = false; //as option one is default of dropdown, the second option should be hidden on load as extra precaution
    95.  
    96.  
    97.         // Enable colliders:
    98.         foreach (var component in colliderComponents)
    99.             component.enabled = true;
    100.  
    101.         // Enable canvas':
    102.         foreach (var component in canvasComponents)
    103.             component.enabled = true;
    104.  
    105.         int val = dropdownMenu.GetComponent<Dropdown>().value; //get value of dropdown selection,  assign variable
    106.  
    107.         //get all options available within this dropdown menu
    108.         List<Dropdown.OptionData> menuOptions = dropdownMenu.GetComponent<Dropdown>().options;
    109.  
    110.         //get the string value of the selected index
    111.         // string value = menuOptions[val].text;
    112.  
    113.         if (val == 0) //Option 1 in dropdown
    114.         {
    115.             rend.enabled = true; //render first model
    116.             rend2.enabled = false; //don't render second model
    117.         }
    118.  
    119.         if (val == 1) //Option 2 in dropdown
    120.         {
    121.  
    122.             rend.enabled = false; //don't render first model
    123.             rend2.enabled = true;  //render second model
    124.         }
    125.     }
    126.     public void RPCSender() //RPC's can be sent to  all players to make changes
    127.     {
    128.         PhotonView photonView = PhotonView.Get(this);
    129.  
    130.         OnTrackingFound(); // Show the prefab on this client
    131.         photonView.RPC("OnTrackingFound", RpcTarget.All); // This will execute the OnTrackingFound function on remote clients already in the game and new clients when they join (because it is buffered).
    132.  
    133.  
    134.    }
    135.  
    136.     [PunRPC] //RPC must be assigned to the function it is calling
    137.     protected virtual void OnTrackingFound()
    138.     {
    139.         if (PhotonNetwork.IsMasterClient)
    140.         {
    141.             dropdownMenu.SetActive(true);
    142.         }
    143.         else
    144.         {
    145.             dropdownMenu.SetActive(false);
    146.         }
    147.  
    148.         DropdownHandler();
    149.      
    150.     }
    151.  
    152.     // end of my custom code
    153.  
    154.     protected virtual void OnTrackingLost()
    155.     {
    156.         var rendererComponents = GetComponentsInChildren<Renderer>(true);
    157.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    158.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    159.  
    160.         // Disable rendering:
    161.         foreach (var component in rendererComponents)
    162.             component.enabled = false;
    163.  
    164.         // Disable colliders:
    165.         foreach (var component in colliderComponents)
    166.             component.enabled = false;
    167.  
    168.         // Disable canvas':
    169.         foreach (var component in canvasComponents)
    170.             component.enabled = false;
    171.     }
    172.  
    173.     #endregion // PROTECTED_METHODS
    174. }
     
  2. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    Your code that runs on all clients from the RPC doesn't do anything based on the master client selecting something.