public void CaptureRectTransform() { if (RectTransformBroadcaster == null) { RectTransformBroadcaster = new RectTransformBroadcaster(); rectTransform = GetComponent <RectTransform>(); } RectTransformBroadcaster.Copy(rectTransform); }
protected override void EnsureTextComponent() { if (TextMeshObserver == null) { RectTransformBroadcaster srt = new RectTransformBroadcaster(); RectTransform rectTransform = GetComponent <RectTransform>(); srt.Copy(rectTransform); TextMeshObserver = ComponentExtensions.EnsureComponent <TextMeshProUGUI>(gameObject); srt.Apply(rectTransform); } }
protected override void SendDeltaChanges(IEnumerable <INetworkConnection> connections, TransformBroadcasterChangeType changeFlags) { using (StateSynchronizationPerformanceMonitor.Instance.MeasureEventDuration(PerformanceComponentName, "SendDeltaChanges")) using (MemoryStream memoryStream = new MemoryStream()) using (BinaryWriter message = new BinaryWriter(memoryStream)) { TransformBroadcasterService.Instance.WriteHeader(message, this); message.Write(this.Id); message.Write((byte)changeFlags); if (HasFlag(changeFlags, TransformBroadcasterChangeType.Name)) { message.Write(this.CachedName); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Layer)) { message.Write(previousLayer); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Parent)) { message.Write(this.ParentId); message.Write(this.transform.GetSiblingIndex()); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Position)) { message.Write(previousPosition); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Rotation)) { message.Write(previousRotation); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Scale)) { message.Write(previousScale); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.IsActive)) { message.Write(this.SynchronizedIsActive); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.RectTransform)) { RectTransform rectTrans = this.gameObject.GetComponent <RectTransform>(); RectTransformBroadcaster.Send(rectTrans, message); } message.Flush(); var data = memoryStream.ToArray(); StateSynchronizationSceneManager.Instance.Send(connections, ref data); } }
protected override void SendDeltaChanges(IEnumerable <SocketEndpoint> endpoints, TransformBroadcasterChangeType changeFlags) { using (MemoryStream memoryStream = new MemoryStream()) using (BinaryWriter message = new BinaryWriter(memoryStream)) { TransformBroadcasterService.Instance.WriteHeader(message, this); message.Write(this.Id); message.Write((byte)changeFlags); if (HasFlag(changeFlags, TransformBroadcasterChangeType.Name)) { message.Write(this.CachedName); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Layer)) { message.Write(previousLayer); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Parent)) { message.Write(this.ParentId); message.Write(this.transform.GetSiblingIndex()); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Position)) { message.Write(previousPosition); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Rotation)) { message.Write(previousRotation); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.Scale)) { message.Write(previousScale); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.IsActive)) { message.Write(this.SynchronizedIsActive); } if (HasFlag(changeFlags, TransformBroadcasterChangeType.RectTransform)) { RectTransform rectTrans = this.gameObject.GetComponent <RectTransform>(); RectTransformBroadcaster.Send(rectTrans, message); } message.Flush(); StateSynchronizationSceneManager.Instance.Send(endpoints, memoryStream.ToArray()); } }
/// <summary> /// Reads a network message and updates local state data /// </summary> /// <param name="sendingEndpoint">Sender endpoint</param> /// <param name="message">Received payload</param> public override void Read(SocketEndpoint sendingEndpoint, BinaryReader message) { short id = message.ReadInt16(); TransformBroadcasterChangeType changeType = (TransformBroadcasterChangeType)message.ReadByte(); GameObject mirror = null; RectTransform rectTransform = null; if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.RectTransform)) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); rectTransform = mirror.GetComponent <RectTransform>(); if (rectTransform == null) { rectTransform = mirror.AddComponent <RectTransform>(); } } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.Name)) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); mirror.name = message.ReadString(); } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.Layer)) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); mirror.layer = message.ReadInt32(); } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.Parent)) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); short parentId = message.ReadInt16(); int siblingIndex = message.ReadInt32(); Transform newParent; if (parentId == TransformBroadcaster.NullTransformId) { newParent = StateSynchronizationSceneManager.Instance.RootTransform; } else { newParent = StateSynchronizationSceneManager.Instance.GetOrCreateMirror(parentId).transform; } if (siblingIndex < newParent.childCount) { Transform existingChildAtIndex = newParent.GetChild(siblingIndex); if (existingChildAtIndex.gameObject.name == temporaryGameObjectName) { Destroy(existingChildAtIndex.gameObject); } } else { for (int i = newParent.childCount; i < siblingIndex; i++) { GameObject temp = new GameObject(temporaryGameObjectName); temp.SetActive(false); temp.transform.SetParent(newParent); temp.transform.SetSiblingIndex(i); } } mirror.transform.SetParent(newParent, worldPositionStays: false); mirror.transform.SetSiblingIndex(siblingIndex); } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.Position)) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); mirror.transform.localPosition = message.ReadVector3(); } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.Rotation)) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); mirror.transform.localRotation = message.ReadQuaternion(); } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.Scale)) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); mirror.transform.localScale = message.ReadVector3(); } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.IsActive)) { bool isActive = message.ReadBoolean(); if (isActive) { mirror = mirror ?? StateSynchronizationSceneManager.Instance.GetOrCreateMirror(id); mirror.SetActive(true); } else { mirror = StateSynchronizationSceneManager.Instance.FindGameObjectWithId(id); if (mirror != null) { mirror.SetActive(false); } } } if (TransformBroadcaster.HasFlag(changeType, TransformBroadcasterChangeType.RectTransform)) { RectTransformBroadcaster.Read(rectTransform, message); ComponentExtensions.EnsureComponent <RectTransformObserver>(mirror).CaptureRectTransform(); } }
protected override TransformBroadcasterChangeType CalculateDeltaChanges() { TransformBroadcasterChangeType changeType = 0; using (StateSynchronizationPerformanceMonitor.Instance.MeasureEventDuration(PerformanceComponentName, "CalculateDeltaChanges")) { short newParentId = ParentId; Vector3 newPosition; Quaternion newRotation; Vector3 newScale; int newLayer = Layer; GetLocalPose(out newPosition, out newRotation, out newScale); bool newIsActive = SynchronizedIsActive; if (previousIsActive != newIsActive) { previousIsActive = newIsActive; changeType |= TransformBroadcasterChangeType.IsActive; } // If this component is not active and enabled in the hierarchy, we only need // to synchronize the isActive state. This is a performance win for large dynamic hierarchies, which don't // need to keep the other Transform-related properties in sync. if (isActiveAndEnabled) { if (previousName != CachedName) { previousName = CachedName; changeType |= TransformBroadcasterChangeType.Name; } if (previousLayer != newLayer) { previousLayer = newLayer; changeType |= TransformBroadcasterChangeType.Layer; } if (previousParentId != newParentId) { previousParentId = newParentId; changeType |= TransformBroadcasterChangeType.Parent; } if (previousPosition != newPosition) { previousPosition = newPosition; changeType |= TransformBroadcasterChangeType.Position; } if (previousRotation != newRotation) { previousRotation = newRotation; changeType |= TransformBroadcasterChangeType.Rotation; } if (previousScale != newScale) { previousScale = newScale; changeType |= TransformBroadcasterChangeType.Scale; } RectTransform rectTrans = CachedRectTransform; if (rectTrans && rectCache == null) { rectCache = new RectTransformBroadcaster(); } else if (rectTrans == null && rectCache != null) { rectCache = null; } if (rectCache != null && rectCache.UpdateChange(rectTrans)) { changeType |= TransformBroadcasterChangeType.RectTransform; } } } return(changeType); }
protected override TransformBroadcasterChangeType CalculateDeltaChanges() { TransformBroadcasterChangeType changeType = 0; short newParentId = ParentId; Vector3 newPosition; Quaternion newRotation; Vector3 newScale; int newLayer = Layer; GetLocalPose(out newPosition, out newRotation, out newScale); bool newIsActive = SynchronizedIsActive; if (previousName != CachedName) { previousName = CachedName; changeType |= TransformBroadcasterChangeType.Name; } if (previousLayer != newLayer) { previousLayer = newLayer; changeType |= TransformBroadcasterChangeType.Layer; } if (previousParentId != newParentId) { previousParentId = newParentId; changeType |= TransformBroadcasterChangeType.Parent; } if (previousPosition != newPosition) { previousPosition = newPosition; changeType |= TransformBroadcasterChangeType.Position; } if (previousRotation != newRotation) { previousRotation = newRotation; changeType |= TransformBroadcasterChangeType.Rotation; } if (previousScale != newScale) { previousScale = newScale; changeType |= TransformBroadcasterChangeType.Scale; } if (previousIsActive != newIsActive) { previousIsActive = newIsActive; changeType |= TransformBroadcasterChangeType.IsActive; } RectTransform rectTrans = CachedRectTransform; if (rectTrans && rectCache == null) { rectCache = new RectTransformBroadcaster(); } else if (rectTrans == null && rectCache != null) { rectCache = null; } if (rectCache != null && rectCache.UpdateChange(rectTrans)) { changeType |= TransformBroadcasterChangeType.RectTransform; } return(changeType); }