How to sync Unity Sprite renderer FlipX or FlipY using photon?

Soikot
Soikot
edited January 2020 in Tutorials and Code Sharing
Hi,

I can't flip the sprite using flipX . Others player doesn't see the flip. I am using this

this.GetComponent<SpriteRenderer>().flipX = true;

How to sync Unity FlipX or FlipY using photon?

Thanks in advance.

Comments

  • Same problem :( can't get it to sync
  • Same problem :( can't get it to sync
  • Hey All,

    Is it possible to get an answer on this? It seems like it would be a relatively simple thing - i tried doing a flip function and then put an PunRPC on the function and called it, but it doesn't seem to do anything.

    Thanks,
  • S_Oliver
    S_Oliver ✭✭✭
    edited January 2020
    HI, you could use an RPC to sync if its fliped or not or you try IPunObservable.
    Here an example.
    using Photon.Pun;
    using UnityEngine;
    
    [RequireComponent(typeof(PhotonView))]
    [RequireComponent(typeof(SpriteRenderer))]
    public class SpriteFlipExample : MonoBehaviour, IPunObservable
    {
    	private SpriteRenderer m_spriteRenderer;
    	private PhotonView m_view;
    
    	private void Start()
    	{
    		m_spriteRenderer = GetComponent<SpriteRenderer>();
    		m_view = GetComponent<PhotonView>();
    		AddObservable();
    	}
    
    	private void AddObservable()
    	{
    		if (!m_view.ObservedComponents.Contains(this))
    		{
    			m_view.ObservedComponents.Add(this);
    		}
    	}
    
    	private void Update()
    	{
    		if (!m_view.IsMine) return;
    
    		if (Input.GetKeyDown(KeyCode.Space))
    		{
    			m_spriteRenderer.flipX = !m_spriteRenderer.flipX;
    		}
    	}
    
    	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    	{
    		if (stream.IsWriting)
    		{
    			stream.SendNext(m_spriteRenderer.flipX);
    		}
    		else
    		{
    			m_spriteRenderer.flipX = (bool) stream.ReceiveNext();
    		}
    	}
    }
    

    You need a PhotonView on the same GameObject with the SpriteRenderer, and the PhotoView should observe this script.