public void OnNeedFullObjectInfo(P2PNetworkMessage netMsg) { byte[] array = netMsg.m_Reader.ReadGuidBytesTemporary(); GameObject gameObject = GuidManager.ResolveGuid(array); if (!gameObject) { if (P2PLogFilter.logError) { Debug.LogError("Cant send full object info - no object! " + new Guid(array)); } return; } ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(gameObject, true); if (P2PLogFilter.logPedantic) { Debug.Log(string.Format("OnNeedFullObjectInfo received for {0} guid: {1}", gameObject.name, replComponentForGameObject.GetGuidComponent.GetGuid())); } if (replComponentForGameObject != null && replComponentForGameObject.IsTransferringOwnershipTo(netMsg.m_Connection.m_Peer)) { this.SpawnForPeerAndGiveOwnership(gameObject, netMsg.m_Connection.m_Peer); return; } this.SpawnForPeer(gameObject, netMsg.m_Connection.m_Peer); }
public GameObject ReadGameObject() { GameObject gameObject = GuidManager.ResolveGuid(this.ReadGuidBytesInternal()); gameObject == null; return(gameObject); }
public GameObject ReadGameObjectAndGuid(out byte[] guid) { GameObject gameObject = GuidManager.ResolveGuid(this.ReadGuidBytesInternal()); if (gameObject == null && P2PLogFilter.logDebug) { Debug.Log("[ReadGameObject] guid not found!"); } guid = P2PNetworkReader.s_GuidBytesBuffer; return(gameObject); }
public Transform ReadTransform() { GameObject gameObject = GuidManager.ResolveGuid(this.ReadGuidBytesInternal()); if (gameObject == null) { if (P2PLogFilter.logDebug) { Debug.Log("[ReadTransform] guid not found"); } return(null); } return(gameObject.transform); }
public void OnSpawnMessage(P2PNetworkMessage net_msg, bool take_ownership) { net_msg.ReadMessage <P2PObjectSpawnMessage>(Replicator.s_ObjectSpawnMessage); GameObject gameObject = GuidManager.ResolveGuid(Replicator.s_ObjectSpawnMessage.guid_bytes); if (P2PLogFilter.logDev) { Debug.Log(string.Format("OnSpawnMessage {0} channel: {1} take_ownership: {2}", new Guid(Replicator.s_ObjectSpawnMessage.guid_bytes), net_msg.m_ChannelId, take_ownership)); } if (!gameObject) { GameObject prefabById = P2PAssetIdCache.Instance.GetPrefabById(Replicator.s_ObjectSpawnMessage.assetId); if (prefabById) { bool activeSelf = prefabById.activeSelf; prefabById.SetActive(false); gameObject = UnityEngine.Object.Instantiate <GameObject>(prefabById, Replicator.s_ObjectSpawnMessage.position, Quaternion.identity); ReplicationComponent component = gameObject.GetComponent <ReplicationComponent>(); component.ReplOnChangedOwner(net_msg.m_Connection.m_Peer); gameObject.GetComponent <GuidComponent>().ForceGuid(Replicator.s_ObjectSpawnMessage.guid_bytes); prefabById.SetActive(activeSelf); gameObject.SetActive(activeSelf); component.ReplOnSpawned(); component.Deserialize(Replicator.s_ObjectSpawnMessage.payload, true); if (take_ownership) { component.ReplRequestOwnership(); return; } } else if (P2PLogFilter.logError) { Debug.LogError(string.Format("OnSpawnMessage no asset found {0}", Replicator.s_ObjectSpawnMessage.assetId)); return; } } else { if (P2PLogFilter.logWarn) { Debug.LogWarning(string.Format("OnSpawnMessage found target for supposedly new object to spawn {0} {1}", gameObject.name, new Guid(Replicator.s_ObjectSpawnMessage.guid_bytes))); } ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(gameObject, true); replComponentForGameObject.Deserialize(Replicator.s_ObjectSpawnMessage.payload, true); if (take_ownership) { replComponentForGameObject.ReplRequestOwnership(); } } }
public GameObject ResolveGameObject() { if (this.IsNull()) { return null; } if (this.m_Resolved) { return this.m_ResolvedObj; } this.m_ResolvedObj = GuidManager.ResolveGuid(this.m_GameObjectBytes); if (this.m_ResolvedObj != null) { this.m_Resolved = true; } return this.m_ResolvedObj; }
public void OnReplicationMessage(P2PNetworkMessage net_msg) { byte[] array = net_msg.m_Reader.ReadGuidBytesTemporary(); GameObject gameObject = GuidManager.ResolveGuid(array); if (gameObject) { this.GetReplComponentForGameObject(gameObject, true).ReplicationReceive(net_msg.m_Reader, false); return; } if (P2PLogFilter.logWarn) { Debug.LogWarning("Did not find target for replication update message for " + new Guid(array)); } P2PNetworkWriter p2PNetworkWriter = new P2PNetworkWriter(); p2PNetworkWriter.StartMessage(8); p2PNetworkWriter.Write(array, GuidComponent.GUID_BYTES_CNT); p2PNetworkWriter.FinishMessage(); P2PSession.Instance.SendWriterTo(net_msg.m_Connection.m_Peer, p2PNetworkWriter, 0); }
public void Add_RaisesAddedCallback() { var guid = Guid.NewGuid(); var gameObject = new GameObject("GuidManagerTests GO"); int addedCallbackRaiseCount = 0; GameObject addedCallbackResolveObject = null; guidManager.ResolveGuid(guid, o => { addedCallbackResolveObject = o; addedCallbackRaiseCount++; }, null); guidManager.Add(guid, gameObject); Assert.AreEqual(1, addedCallbackRaiseCount); Assert.AreEqual(gameObject, addedCallbackResolveObject); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { guidProp = property.FindPropertyRelative("serializedGuid"); nameProp = property.FindPropertyRelative("cachedName"); sceneProp = property.FindPropertyRelative("cachedScene"); // Using BeginProperty / EndProperty on the parent property means that // prefab override logic works on the entire property. EditorGUI.BeginProperty(position, label, property); position.height = EditorGUIUtility.singleLineHeight; // Draw prefix label, returning the new rect we can draw in var guidCompPosition = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); System.Guid currentGuid; GameObject currentGO = null; // working with array properties is a bit unwieldy // you have to get the property at each index manually byte[] byteArray = new byte[16]; int arraySize = guidProp.arraySize; for (int i = 0; i < arraySize; ++i) { var byteProp = guidProp.GetArrayElementAtIndex(i); byteArray[i] = (byte)byteProp.intValue; } currentGuid = new System.Guid(byteArray); currentGO = GuidManager.ResolveGuid(currentGuid); GuidComponent currentGuidComponent = currentGO != null?currentGO.GetComponent <GuidComponent>() : null; GuidComponent component = null; if (currentGuid != System.Guid.Empty && currentGuidComponent == null) { // if our reference is set, but the target isn't loaded, we display the target and the scene it is in, and provide a way to clear the reference float buttonWidth = 55.0f; guidCompPosition.xMax -= buttonWidth; bool guiEnabled = GUI.enabled; GUI.enabled = false; EditorGUI.LabelField(guidCompPosition, new GUIContent(nameProp.stringValue, "Target GameObject is not currently loaded."), EditorStyles.objectField); GUI.enabled = guiEnabled; Rect clearButtonRect = new Rect(guidCompPosition); clearButtonRect.xMin = guidCompPosition.xMax; clearButtonRect.xMax += buttonWidth; if (GUI.Button(clearButtonRect, clearButtonGUI, EditorStyles.miniButton)) { ClearPreviousGuid(); } } else { // if our object is loaded, we can simply use an object field directly component = EditorGUI.ObjectField(guidCompPosition, currentGuidComponent, typeof(GuidComponent), true) as GuidComponent; } if (currentGuidComponent != null && component == null) { ClearPreviousGuid(); } // if we have a valid reference, draw the scene name of the scene it lives in so users can find it if (component != null) { nameProp.stringValue = component.name; string scenePath = component.gameObject.scene.path; sceneProp.objectReferenceValue = AssetDatabase.LoadAssetAtPath <SceneAsset>(scenePath); // only update the GUID Prop if something changed. This fixes multi-edit on GUID References if (component != currentGuidComponent) { byteArray = component.GetGuid().ToByteArray(); arraySize = guidProp.arraySize; for (int i = 0; i < arraySize; ++i) { var byteProp = guidProp.GetArrayElementAtIndex(i); byteProp.intValue = byteArray[i]; } } } EditorGUI.indentLevel++; position.y += EditorGUIUtility.singleLineHeight; bool cachedGUIState = GUI.enabled; GUI.enabled = false; EditorGUI.ObjectField(position, sceneLabel, sceneProp.objectReferenceValue, typeof(SceneAsset), false); GUI.enabled = cachedGUIState; EditorGUI.indentLevel--; EditorGUI.EndProperty(); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { _guidProp = property.FindPropertyRelative("_serializedGuid"); _nameProp = property.FindPropertyRelative("_cachedName"); _sceneProp = property.FindPropertyRelative("_cachedScene"); _lockedReference = property.FindPropertyRelative("_sceneReference"); _sceneRefInstance = _lockedReference.FindPropertyRelative("_instance"); // Using BeginProperty / EndProperty on the parent property means that // prefab override logic works on the entire property. EditorGUI.BeginProperty(position, label, property); // Draw prefix label, returning the new rect we can draw in var guidCompPosition = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); guidCompPosition.height = EditorGUIUtility.singleLineHeight; System.Guid currentGuid; GameObject currentGO = null; // working with array properties is a bit unwieldy // you have to get the property at each index manually byte[] byteArray = new byte[16]; int arraySize = _guidProp.arraySize; for (int i = 0; i < arraySize; ++i) { var byteProp = _guidProp.GetArrayElementAtIndex(i); byteArray[i] = (byte)byteProp.intValue; } currentGuid = new System.Guid(byteArray); currentGO = GuidManager.ResolveGuid(currentGuid); GateBase currentGate = currentGO != null?currentGO.GetComponent <GateBase>() : null; GateBase component = null; if (currentGuid != System.Guid.Empty && currentGate == null) { // if our reference is set, but the target isn't loaded, we display the target and the scene it is in, and provide a way to clear the reference Rect buttonRect; Rect newPosition = IMGUIUtils.SnipRectH(guidCompPosition, guidCompPosition.width - 70, out buttonRect, 6f); bool guiEnabled = GUI.enabled; GUI.enabled = false; EditorGUI.LabelField(newPosition, new GUIContent(_nameProp.stringValue, "Target GameObject is not currently loaded."), EditorStyles.objectField); GUI.enabled = guiEnabled; if (GUI.Button(buttonRect, _clearButtonGUI, EditorStyles.miniButton)) { ClearPreviousGuid(); } } else { // if our object is loaded, we can simply use an object field directly component = EditorGUI.ObjectField(guidCompPosition, currentGate, typeof(GateBase), true) as GateBase; } if (currentGate != null && component == null) { // clear out an old Guid after the user has changed/removed a reference in inspector ClearPreviousGuid(); } // if we have a valid reference, draw the scene name of the scene it lives in so users can find it if (component != null) { _nameProp.stringValue = component.name; string scenePath = component.gameObject.scene.path; SceneAsset sceneAsset = AssetDatabase.LoadAssetAtPath <SceneAsset>(scenePath); _sceneProp.objectReferenceValue = sceneAsset; Scene scene = EditorSceneManager.GetSceneByName(((SceneAsset)_sceneProp.objectReferenceValue).name); _sceneRefInstance.FindPropertyRelative("_sceneAsset").objectReferenceValue = sceneAsset; _sceneRefInstance.FindPropertyRelative("_sceneName").stringValue = scene.name; _sceneRefInstance.FindPropertyRelative("_scenePath").stringValue = scene.path; _sceneRefInstance.FindPropertyRelative("_buildIndex").intValue = scene.buildIndex; // only update the GUID Prop if something changed. This fixes multi-edit on GUID References if (component != currentGate) { byteArray = component.GetGuid().ToByteArray(); arraySize = _guidProp.arraySize; for (int i = 0; i < arraySize; ++i) { var byteProp = _guidProp.GetArrayElementAtIndex(i); byteProp.intValue = byteArray[i]; } } } position.y += EditorGUIUtility.singleLineHeight + 2; Rect sceneFieldPosition = new Rect(position); sceneFieldPosition.height = EditorGUI.GetPropertyHeight(_lockedReference, true); EditorGUI.PropertyField(sceneFieldPosition, _lockedReference, true); EditorGUI.EndProperty(); }