成品
课上代码
‘NetworkMangerUI’
‘’‘
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
public class NetworkManagerUI : MonoBehaviour
{
[SerializeField]
private Button hostButton;
[SerializeField]
private Button serverButton;
[SerializeField]
private Button clientButton;
// Start is called before the first frame update
void Start()
{
hostButton.onClick.AddListener(() =>
{
NetworkManager.Singleton.StartHost();
});
serverButton.onClick.AddListener(() =>
{
NetworkManager.Singleton.StartServer();
});
clientButton.onClick.AddListener(() =>
{
NetworkManager.Singleton.StartClient();
});
}
// Update is called once per frame
void Update()
{
}
}
‘’‘
‘PlayerSetUP’
‘’‘
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class PlayerSetup: NetworkBehaviour
{
// Start is called before the first frame update
[SerializeField]
private Behaviour[] componentToEnable;
[SerializeField]
private Camera sceneCamera;
void Start()
{
if(!IsLocalPlayer)
{
for(int i = 0; i < componentToEnable.Length; i ++)
{
componentToEnable[i].enabled = false;
}
}
sceneCamera = GameObject.Find(“/SceneCamera”).GetComponent[HTML_REMOVED]();
if(sceneCamera != null)
{
sceneCamera.gameObject.SetActive(false);
}
}
// Update is called once per frame
private void OnDisable()
{
if(sceneCamera != null)
{
sceneCamera.gameObject.SetActive(true);
}
}
}
‘’‘