Syncing Large Number of Children Transform using OnPhotonSerializeView

I'm attempting to achieve two main objectives:
  1. Sync transform state of root gameobject with many (200+) children
  2. Be able to add and delete children gameobjects from under the root gameobject and sync their states
To achieve this, I've implemented OnPhotonSerializeView that does the following:

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
		if (stream.isWriting) {
			for (int i = 0; i < childrenTransforms.Length; i++) {
				if (childrenTransforms[i] != null) {
					stream.SendNext(childrenTransforms[i].localPosition);
					stream.SendNext(childrenTransforms[i].localRotation);
					stream.SendNext(childrenTransforms[i].localScale);
				}
			}
		} else {
			for (int i = 0; i < childrenTransforms.Length; i++) {
				if (childrenTransforms[i] != null) {
					childrenTransforms[i].localPosition = (Vector3)stream.ReceiveNext();
					childrenTransforms[i].localRotation = (Quaternion)stream.ReceiveNext();
					childrenTransforms[i].localScale = (Vector3)stream.ReceiveNext();
				}
			}
		}
	}
}
Three main questions:
  1. Does this seem like the right approach to handle syncing of large gameobject transform hierarchies?
  2. If the root gameobjects transform changes, does that send update message for all of it's children?
  3. This currently works fine with small transform hierarchies but I've tested with very large ones and I get numerous InvalidCastExceptions on the clients listening for updates and the connection drops.

Answers

  • Hi @talothman,

    Does this seem like the right approach to handle syncing of large gameobject transform hierarchies?


    This seems to be fine so far and it is by far the better approach than adding a PhotonView component to each of the child objects. You only should take care of what you really want to synchronize. If these child objects don't move on their own, you won't have to synchronize their position at all. If they however do, you should only synchronize those objects, that actually have moved (to reduce message size).

    If the root gameobjects transform changes, does that send update message for all of it's children?


    If only the parent object moves and the child objects don't, you just have to synchronize the transform data of the parent object, because they are positioned relatively to their parent object.
Sign In or Register to comment.