using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
public class Manager : NetworkManager
{
[SerializeField] public GameObject buttonPrefab; // Reference to the button prefab you want to spawn
[SerializeField] public Transform canvasTransform; // Reference to the Canvas transform where the button will be spawned
[SerializeField] public Button serverButton;
[SerializeField] public Button hostButton;
[SerializeField] public Button clientButton;
private void Awake()
{
serverButton.onClick.AddListener(() =>
{
Debug.Log("ServerConnected");
NetworkManager.Singleton.StartServer();
});
hostButton.onClick.AddListener(() =>
{
Debug.Log("HostConnected");
NetworkManager.Singleton.StartHost();
});
clientButton.onClick.AddListener(() =>
{
Debug.Log("ClientConnected");
NetworkManager.Singleton.StartClient();
});
}
private void Start()
{
SpawnButtonOnCanvas();
}
public void SpawnButtonOnCanvas()
{
// Instantiate the button prefab and set its parent to the canvas
GameObject newButton = Instantiate(buttonPrefab, canvasTransform);
// Optionally, you can modify the button's properties after instantiating it
// For example, you can change its position, text, or add a click event listener
Button buttonComponent = newButton.GetComponent<Button>();
if (buttonComponent != null)
{
buttonComponent.transform.localPosition = Vector3.zero; // Set the position to the center of the canvas
buttonComponent.GetComponentInChildren<Text>().text = "New Button"; // Set the button's text
buttonComponent.onClick.AddListener(() => OnButtonClick()); // Add a click event listener
}
}
private void OnButtonClick()
{
Debug.Log("Button clicked!");
// Add your custom functionality here
}
}