/// <summary> /// Deletes all existing <see cref="ViewContainer">ViewContainers</see>. /// </summary> /// <param name="syncWithRemote">Indicates whether the containers should also be deleted on remote clients.</param> public void DeleteAllViewContainers(bool syncWithRemote = true) { if (ViewContainers != null) { foreach (var container in ViewContainers) { Destroy(container.Value.gameObject); } ViewContainers.Clear(); } if (syncWithRemote) { var message = new MessageDeleteAllVisContainers(); Services.NetworkManager().SendMessage(message.Pack()); } }
/// <summary> /// Creates a new <see cref="ViewContainer"/> and adds it to the list of containers. /// </summary> /// <param name="container">The <see cref="VisContainer"/> object representing the settings for the new <see cref="ViewContainer"/>.</param> /// <param name="syncWithRemote">Indicates whether the container should also be created on remote clients.</param> public void CreateViewContainer(VisContainer container, bool syncWithRemote = true) { // add to list of containers or update list if (ViewContainers.ContainsKey(container.Id)) { // already in list, update ViewContainers[container.Id].Init(container); } else { // not in list, create and add Transform worldAnchor = GameObject.FindGameObjectWithTag("VisRootAnchor").transform; var placeholder = Instantiate(VisPlaceholderPrefab, worldAnchor); // instantiate placeholder prefab, set the World Anchor as parent, to make sure every client sees the same placeholder.Init(container); ViewContainers.Add(container.Id, placeholder.GetComponent <ViewContainer>()); // add to list } if (syncWithRemote) { var message = new MessageCreateVisContainer(container); Services.NetworkManager().SendMessage(message.Pack()); } }
/// <summary> /// Creates a visualization from a <see cref="VisProperties"/>. /// </summary> /// <param name="settings">The struct containing the settings for the visualization.</param> /// <param name="syncWithRemote">Indicates whether the visualization should also be created on other clients.</param> /// <returns>A <see cref="GameObject"/> with the visualization.</returns> public GameObject CreateVisualization(VisProperties settings, bool syncWithRemote = true) { try { if (Visualizations.ContainsKey(settings.VisId)) { throw new Exception("VisId " + settings.VisId + " has been used twice!"); } // if the vis id is empty, create a new, non-colliding GUID if (settings.VisId == Guid.Empty) { settings.VisId = Guid.NewGuid(); // This is extremely unlikely to ever happen while (Visualizations.ContainsKey(settings.VisId)) { Debug.LogWarning("GUID collision! Probably someone made a mistake. If not, you should start playing the lottery!"); settings.VisId = Guid.NewGuid(); } } // special case: timeline control view & coordinate system GameObject vis; if (settings.VisType == VisType.TimelineControl) { var timelineControl = Instantiate(TimelineControlPrefab); timelineControl.Init(settings); vis = timelineControl.gameObject; timelineController = timelineControl; } else if (settings.VisType == VisType.CoordinateSystem3D) { var coordinateSystem = Instantiate(CoordinateSystemPrefab); coordinateSystem.Init(settings); vis = coordinateSystem.gameObject; coordinateSystemVis = coordinateSystem; } // get correct prefab AbstractView visPrefab = null; foreach (var prefab in VisualizationPrefabs) { if (prefab.VisType == settings.VisType) { visPrefab = prefab; break; } } if (visPrefab == null) { throw new Exception("Attempt to spawn unknown visualization! Type: " + settings.VisType); } // instantiate prefab var visInstance = Instantiate(visPrefab); visInstance.Init(settings); vis = visInstance.gameObject; if (settings.AnchorId != -1 && ViewContainers.ContainsKey(settings.AnchorId)) { // anchor is available ViewContainers[settings.AnchorId].AttachVis(vis.GetComponent <AbstractView>()); } else if (settings.VisType == VisType.TimelineControl) { // anchor not available, put at world anchor Transform worldAnchor = GameObject.FindGameObjectWithTag("RootWorldAnchor").transform; vis.transform.SetParent(worldAnchor, false); } else { // anchor not available, put at world anchor Transform worldAnchor = GameObject.FindGameObjectWithTag("VisRootAnchor").transform; vis.transform.SetParent(worldAnchor, false); } Visualizations[settings.VisId] = vis.GetComponent <AbstractView>(); // at this point, creation was successful; send message to the other clients if needed if (syncWithRemote) { var message = new MessageCreateVisualization(settings); Services.NetworkManager().SendMessage(message.Pack()); } // send notification event that a new vis was created VisualizationCreatedEventBroadcast.Invoke(settings); return(vis); } catch (Exception e) { Debug.LogWarning("Creation of requested Visualization failed."); Debug.LogError(e.Message); Debug.LogError(e.StackTrace); return(new GameObject("Creation Failed")); } }