is there any way to sync text data onto a tmp?

is there any way to sync text data onto a tmp?
17 Replies
kapa.ai
kapa.ai2w ago
Hi @stickkdoesstuff. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can sync text data onto a TextMesh Pro (TMP) object in NormalVR. However, you should note that only primitives (int, float, Vector3, string, etc.) can be serialized in NormalVR. Therefore, if you need to sync a specific kind of object like a TextMesh Pro object, you should create a field for each property on the object that you’d like to sync. For example, if you want to sync the text content of a TMP object, you could create a string field in your model and sync that. Then, in your RealtimeComponent, you could update the TMP object's text with the value of the string field whenever it changes. Here is the source (https://normcore.io/documentation/room/common-questions#when-i-try-to-sync-a-specific-object-to-the-datastore-i-get-an-error-that-says-it-cannot-be-serialized-whats-the-issue) for your reference. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@1263374106577993729> kapa.ai is still learning and improving, please let us know how it did by reacting below
stickkdoesstuff
@kapa.ai Does tmp sync its data value when using realtime
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- TextMesh Pro (TMP) objects do not automatically sync their data in NormalVR. However, you can manually synchronize the text data of a TMP object using a custom RealtimeComponent and RealtimeModel. Here's a general idea of how you could do it: 1. Create a new RealtimeModel subclass with a string property to represent the text of the TMP object. 2. Create a new RealtimeComponent subclass that references your TMP object and your new RealtimeModel. 3. In the RealtimeComponent, override the OnRealtimeModelReplaced method. In this method, you should update the TMP object's text with the value from the RealtimeModel, and also subscribe to changes in the RealtimeModel's text property. 4. Whenever the text property of the RealtimeModel changes (either locally or remotely), update the TMP object's text to match. Here's a simplified example of what the RealtimeComponent might look like:
using UnityEngine;
using Normal.Realtime;
using TMPro;

public class TextSync : RealtimeComponent<TextSyncModel> {
private TMP_Text _textMesh;

private void Awake() {
_textMesh = GetComponent<TMP_Text>();
}

protected override void OnRealtimeModelReplaced(TextSyncModel previousModel, TextSyncModel currentModel) {
if (previousModel != null) {
previousModel.textDidChange -= TextDidChange;
}

if (currentModel != null) {
UpdateTextMesh();
currentModel.textDidChange += TextDidChange;
}
}

private void TextDidChange(TextSyncModel model, string value) {
UpdateTextMesh();
}

private void UpdateTextMesh() {
_textMesh.text = model.text;
}

public void SetText(string text) {
model.text = text;
}
}
using UnityEngine;
using Normal.Realtime;
using TMPro;

public class TextSync : RealtimeComponent<TextSyncModel> {
private TMP_Text _textMesh;

private void Awake() {
_textMesh = GetComponent<TMP_Text>();
}

protected override void OnRealtimeModelReplaced(TextSyncModel previousModel, TextSyncModel currentModel) {
if (previousModel != null) {
previousModel.textDidChange -= TextDidChange;
}

if (currentModel != null) {
UpdateTextMesh();
currentModel.textDidChange += TextDidChange;
}
}

private void TextDidChange(TextSyncModel model, string value) {
UpdateTextMesh();
}

private void UpdateTextMesh() {
_textMesh.text = model.text;
}

public void SetText(string text) {
model.text = text;
}
}
In this example, TextSyncModel would be a RealtimeModel subclass with a string property named text. Please note that this is a simplified example and might need to be adjusted to fit your specific needs. For more information, you can refer to the Normcore documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@1263374106577993729> kapa.ai is still learning and improving, please let us know how it did by reacting below
stickkdoesstuff
@kapa.ai Could i do this multiple times for each of my players names?
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, you can use this approach to sync the names of multiple players. Each player would have their own instance of the TextSync component (or whatever you name your custom RealtimeComponent), each with its own associated TextSyncModel (or your custom RealtimeModel). Here's a general idea of how you could set it up: 1. Each player prefab would have a TextSync component attached to it, likely on the same GameObject as their name label. 2. When a player connects and their prefab is instantiated, you would set the text property of their TextSyncModel to their name. 3. The TextSync component would automatically update the TMP object's text whenever the text property of the TextSyncModel changes. Here's an example of how you might set the player's name when they connect:
using UnityEngine;
using Normal.Realtime;

public class PlayerName : MonoBehaviour {
private Realtime _realtime;
private TextSync _textSync;

private void Awake() {
_realtime = GetComponent<Realtime>();
_textSync = GetComponent<TextSync>();

_realtime.didConnectToRoom += DidConnectToRoom;
}

private void DidConnectToRoom(Realtime realtime) {
_textSync.SetText(_realtime.client.name);
}
}
using UnityEngine;
using Normal.Realtime;

public class PlayerName : MonoBehaviour {
private Realtime _realtime;
private TextSync _textSync;

private void Awake() {
_realtime = GetComponent<Realtime>();
_textSync = GetComponent<TextSync>();

_realtime.didConnectToRoom += DidConnectToRoom;
}

private void DidConnectToRoom(Realtime realtime) {
_textSync.SetText(_realtime.client.name);
}
}
In this example, TextSync is the custom RealtimeComponent from the previous example, and _realtime.client.name is the name of the player. You would replace _realtime.client.name with the actual player's name. Please note that this is a simplified example and might need to be adjusted to fit your specific needs. For more information, you can refer to the Normcore documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@1263374106577993729> kapa.ai is still learning and improving, please let us know how it did by reacting below
stickkdoesstuff
@kapa.ai This can sync multiple player names?
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, you can use this approach to synchronize the names of multiple players in NormalVR. Each player would have their own instance of the TextSync component (or whatever you name your custom RealtimeComponent), each with its own associated TextSyncModel (or your custom RealtimeModel). Here's a general idea of how you could set it up: 1. Each player prefab would have a TextSync component attached to it, likely on the same GameObject as their name label. 2. When a player connects and their prefab is instantiated, you would set the text property of their TextSyncModel to their name. 3. The TextSync component would automatically update the TMP object's text whenever the text property of the TextSyncModel changes. Here's an example of how you might set the player's name when they connect:
using UnityEngine;
using Normal.Realtime;

public class PlayerName : MonoBehaviour {
private Realtime _realtime;
private TextSync _textSync;

private void Awake() {
_realtime = GetComponent<Realtime>();
_textSync = GetComponent<TextSync>();

_realtime.didConnectToRoom += DidConnectToRoom;
}

private void DidConnectToRoom(Realtime realtime) {
_textSync.SetText(_realtime.client.name);
}
}
using UnityEngine;
using Normal.Realtime;

public class PlayerName : MonoBehaviour {
private Realtime _realtime;
private TextSync _textSync;

private void Awake() {
_realtime = GetComponent<Realtime>();
_textSync = GetComponent<TextSync>();

_realtime.didConnectToRoom += DidConnectToRoom;
}

private void DidConnectToRoom(Realtime realtime) {
_textSync.SetText(_realtime.client.name);
}
}
In this example, TextSync is the custom RealtimeComponent from the previous example, and _realtime.client.name is the name of the player. You would replace _realtime.client.name with the actual player's name. Please note that this is a simplified example and might need to be adjusted to fit your specific needs. For more information, you can refer to the Normcore documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
stickkdoesstuff
@kapa.ai Just so you know im using the vr player example
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- If you're using the VR Player example from NormalVR, you can still apply the same concept to synchronize player names. The VR Player prefab in the example already includes a RealtimeView component, which is used to synchronize the player's position and rotation. You can add your custom TextSync component to this prefab to also synchronize the player's name. Here's a general idea of how you could set it up: 1. Add a TextMesh Pro object to the VR Player prefab to display the player's name. This could be a child object of the head, for example, so that the name always appears above the player's head. 2. Add your TextSync component to the same GameObject as the name label. 3. When a player connects and their VR Player prefab is instantiated, you would set the text property of their TextSyncModel to their name. 4. The TextSync component would automatically update the TMP object's text whenever the text property of the TextSyncModel changes. Here's an example of how you might set the player's name when they connect:
using UnityEngine;
using Normal.Realtime;

public class PlayerName : MonoBehaviour {
private Realtime _realtime;
private TextSync _textSync;

private void Awake() {
_realtime = GetComponent<Realtime>();
_textSync = GetComponentInChildren<TextSync>(); // Assuming TextSync is on a child object

_realtime.didConnectToRoom += DidConnectToRoom;
}

private void DidConnectToRoom(Realtime realtime) {
_textSync.SetText(_realtime.client.name);
}
}
using UnityEngine;
using Normal.Realtime;

public class PlayerName : MonoBehaviour {
private Realtime _realtime;
private TextSync _textSync;

private void Awake() {
_realtime = GetComponent<Realtime>();
_textSync = GetComponentInChildren<TextSync>(); // Assuming TextSync is on a child object

_realtime.didConnectToRoom += DidConnectToRoom;
}

private void DidConnectToRoom(Realtime realtime) {
_textSync.SetText(_realtime.client.name);
}
}
In this example, TextSync is the custom RealtimeComponent from the previous examples, and _realtime.client.name is the name of the player. You would replace _realtime.client.name with the actual player's name. Please note that this is a simplified example and might need to be adjusted to fit your specific needs. For more information, you can refer to the Normcore documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@1263374106577993729> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit2w ago
Synchronizing Custom Data | Normcore Documenation
In this guide, I’m going to walk you through how to synchronize custom data in your Unity application using custom realtime components.
Mechabit
Mechabit2w ago
follow this tutorial and swap color for string and mesh renderer for tmp
stickkdoesstuff
thanks what do i do to this code? using System.Collections; using System.Collections.Generic; using UnityEngine; using Normal.Realtime; public class ColorSync : RealtimeComponent<ColorSyncModel> { private MeshRenderer _meshRenderer; private void Awake() { // Get a reference to the mesh renderer _meshRenderer = GetComponent<MeshRenderer>(); } protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel) { if (previousModel != null) { // Unregister from events previousModel.colorDidChange -= ColorDidChange; }
if (currentModel != null) { // If this is a model that has no data set on it, populate it with the current mesh renderer color. if (currentModel.isFreshModel) currentModel.color = _meshRenderer.material.color; // Update the mesh render to match the new model UpdateMeshRendererColor(); // Register for events so we'll know if the color changes later currentModel.colorDidChange += ColorDidChange; } } private void ColorDidChange(ColorSyncModel model, Color value) { // Update the mesh renderer UpdateMeshRendererColor(); } private void UpdateMeshRendererColor() { // Get the color from the model and set it on the mesh renderer. _meshRenderer.material.color = model.color; } public void SetColor(Color color) { // Set the color on the model // This will fire the colorChanged event on the model, which will update the renderer for both the local player and all remote players. model.color = color; } } also can you send the script
Mechabit
Mechabit2w ago
press ctrl F, type Color, click replace, type string it's really simple just follow the tutorial in the link and you'll better understand what each step is doing