How to Share UI across multiple client
i am implementing Unity3d Networking i want a UI across same to All Clients not All client has their own UI.Same UI Sharing all client.
When i change the UI parameter so my UI Update all Client are updated .
When i am trying to do this Unity3d Network Manager create a Separate UI for ALL Clients means if i have 10 Clients then Unity3d Create 10 UI For my All Client like MMO games. but scenerio is different.
For Illustratration :- When i connect my client to host unity3d hirarchy has 2 UI's. when 3 client the hirarchy has 3 UI's so on .. but i want to Share a single UI to Across network and change it from Across network is there any method or some thing
Thanks in Advance :)
The UI (as far as I know) should not be spawned with the player if it's set up correctly.
Can you tell me more about how you've set up your UI and your GamePlayer Prefabs?
I am also having the same issue, I have an enemy prefab that spawns on the server and is shown on all connected clients, however if it is not the server machine then the UI does not update.
Answer by Reachz · May 29, 2016 at 12:59 AM
U can put 1 UI (canvas) in the scene, which could be activated and synchronized with the other cliets if a client connects and then: Assuming there is a Button that does something, which u would then want to happen on all clients, u could do the following:
The button could call a [Command] function, which is executed on the Server. In that function u can then call a [ClientRpc] function from the server and do something in it. This function is executed on all clients, so its basically as if u would press the button on all clients.
I can also imagine another Method maybe more elegant scenario, where the UI updates itself in certain intervalls with [SyncVars] in ur Scripts. U could then call all functions as [Commands] when changing variables. The Change of the variable on the Server would then be synchronized with all clients, and their UIs would update themselves.
Those are just some quick Suggestions, there may be A LOT more ways to achieve this.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.UI;
public class scoreUIControls : NetworkBehaviour {
public Text waveCount;
public Text waveText;
public Text getReady;
public enemySpawn enemyspawn;
// Use this for initialization
void Start () {
waveCount.color = new Color(waveCount.color.r, waveCount.color.b, waveCount.color.g, 0.0f);
waveText.color = new Color(waveText.color.r, waveText.color.b, waveText.color.g, 0.0f);
getReady.color = new Color(getReady.color.r, getReady.color.b, getReady.color.g, 0.0f);
StartCoroutine(FadeTextToFullAlpha(2, getReady));
}
// Update is called once per frame
void Update () {
enemyspawn = GameObject.FindGameObjectWithTag("spawn$$anonymous$$anager").GetComponent<enemySpawn>();
if (enemyspawn.firstWave == true) {
waveCount.text = "1";
StartCoroutine(FadeTextToFullAlpha(2, waveText));
StartCoroutine(FadeTextToFullAlpha(2, waveCount));
}
if (enemyspawn.secondWave == true) {
waveCount.text = "2";
StartCoroutine(FadeTextToFullAlpha(2, waveText));
StartCoroutine(FadeTextToFullAlpha(2, waveCount));
}
}
public IEnumerator FadeTextToFullAlpha(float t, Text text) {
text.color = new Color (text.color.r, text.color.g, text.color.b, 0);
while (text.color.a < 1.0f)
{
text.color = new Color(text.color.r, text.color.g, text.color.b, text.color.a + (Time.deltaTime / t));
yield return null;
}
}
public IEnumerator FadeTextToZeroAlpha(float t, Text text) {
text.color = new Color (text.color.r, text.color.g, text.color.b, 1);
while (text.color.a > 0.0f)
{
text.color = new Color(text.color.r, text.color.g, text.color.b, text.color.a - (Time.deltaTime / t));
yield return null;
}
}
}
Here is my code and the variables up the top public Text waveCount, public Text waveText, public Text getReady are what I am trying to sync. I hope this makes sense.
I do not know how ur spawn$$anonymous$$anager works, but i think its only doing its work on the server, so its not used on the clientside. $$anonymous$$aybe because of that, the code inside ur if-statements if (enemyspawn.firstWave == true) is never executed on clientside. But the server could tell all clients to execute the code if u use a [ClientRpc] function, $$anonymous$$aybe that could work?:
[ServerCallback]
void Update () {
enemyspawn = GameObject.FindGameObjectWithTag("spawn$$anonymous$$anager").GetComponent<enemySpawn>();
if (enemyspawn.firstWave == true) {
Rpc_FirstWave();
}
if (enemyspawn.secondWave == true) {
Rpc_SecondWave();
}
}
[ClientRpc]
void Rpc_FirstWave(){
waveCount.text = "1";
StartCoroutine(FadeTextToFullAlpha(2, waveText));
StartCoroutine(FadeTextToFullAlpha(2, waveCount));
}
[ClientRpc]
void Rpc_SecondWave(){
waveCount.text = "2";
StartCoroutine(FadeTextToFullAlpha(2, waveText));
StartCoroutine(FadeTextToFullAlpha(2, waveCount));
}
I am also very new to Networking, i dont if this is right, and maybe there are more elegant ways.
Your answer
