How do you update an input field text component that can be edited by two players?

Options
I am trying to use OnPhotonSerializeView() or RPCs to update the text that is entered into a UI Input field. These input fields will be instantiated into the game as scene objects and will be able to be edited by either player. I am having difficulty finding resources on how to do this well. All input would be really appreciated! Thanks!

Answers

  • Hi @dwalch,

    since you are instantiating this UI element as a scene object, it already has a PhotonView component, which means that you can use RPCs on this object. To use a RPC, you need a reference to the PhotonView component which you can get by using GetComponent<PhotonView>(). On this component you can use the RPC function and add the text from the UI element as parameter, e.g.: photonView.RPC("UpdateText", PhotonTargets.All, text);. Next you'll need a receiving function which is tagged with the [PunRPC] attribute, e.g.:
    [PunRPC]
    public void UpdateText(string text) { }
    Inside this function you have to update the text of the UI element, e.g.: uiElementReference.text = text;.

    You know when to fire a RPC by using the EndEdit event. Don't use the OnValueChanged event in this case. Please check the Unity Scripting API for more information about the Input Field.
  • dwalch
    dwalch
    edited July 2018
    Options
    Hi @Christian_Simon

    Thank you so much for your response! You have been really helpful. I apologize if this is an easy question, but I am confused on where you are getting the "uiElementReference"?

    This is the script that I currently have. Would you mind taking .a look at it and seeing what I can change to it? Thanks, man!
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class UpdateInput : Photon.PunBehaviour
    {
        public InputField text;
    
        private void Update()
        {
            GetComponent<PhotonView>();
            photonView.RPC("UpdateText", PhotonTargets.All, text);
        }
    
    
        [PunRPC]
        public void UpdateText(string text)
        {
            uiElementReference.text = text;
        }
    }
  • f03n1x
    Options
    Hi @dwalch

    @Christian_Simon just meant that as an example in your case it'd be text.text since you named your input field variable text.