aha
This commit is contained in:
@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Mirror.Examples.AdditiveScenes
|
||||
{
|
||||
[AddComponentMenu("")]
|
||||
public class AdditiveNetworkManager : NetworkManager
|
||||
{
|
||||
[Tooltip("Trigger Zone Prefab")]
|
||||
public GameObject Zone;
|
||||
|
||||
[Scene]
|
||||
[Tooltip("Add all sub-scenes to this list")]
|
||||
public string[] subScenes;
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
base.OnStartServer();
|
||||
|
||||
// load all subscenes on the server only
|
||||
StartCoroutine(LoadSubScenes());
|
||||
|
||||
// Instantiate Zone Handler on server only
|
||||
Instantiate(Zone);
|
||||
}
|
||||
|
||||
public override void OnStopServer()
|
||||
{
|
||||
StartCoroutine(UnloadScenes());
|
||||
}
|
||||
|
||||
public override void OnStopClient()
|
||||
{
|
||||
if (mode == NetworkManagerMode.Offline)
|
||||
StartCoroutine(UnloadScenes());
|
||||
}
|
||||
|
||||
IEnumerator LoadSubScenes()
|
||||
{
|
||||
Debug.Log("Loading Scenes");
|
||||
|
||||
foreach (string sceneName in subScenes)
|
||||
yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
|
||||
}
|
||||
|
||||
IEnumerator UnloadScenes()
|
||||
{
|
||||
Debug.Log("Unloading Subscenes");
|
||||
|
||||
foreach (string sceneName in subScenes)
|
||||
if (SceneManager.GetSceneByName(sceneName).IsValid() || SceneManager.GetSceneByPath(sceneName).IsValid())
|
||||
yield return SceneManager.UnloadSceneAsync(sceneName);
|
||||
|
||||
yield return Resources.UnloadUnusedAssets();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34d1daf9e7dbcb64aa647cb332054ea6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/AdditiveScenes/Scripts/AdditiveNetworkManager.cs
|
||||
uploadId: 736421
|
@ -0,0 +1,59 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.AdditiveScenes
|
||||
{
|
||||
// This script demonstrates the NetworkAnimator and how to leverage
|
||||
// the built-in observers system to track players.
|
||||
// Note that all ProximityCheckers should be restricted to the Player layer.
|
||||
public class ShootingTankBehaviour : NetworkBehaviour
|
||||
{
|
||||
[SyncVar]
|
||||
public Quaternion rotation;
|
||||
|
||||
NetworkAnimator networkAnimator;
|
||||
|
||||
[ServerCallback]
|
||||
void Start()
|
||||
{
|
||||
networkAnimator = GetComponent<NetworkAnimator>();
|
||||
}
|
||||
|
||||
[Range(0, 1)]
|
||||
public float turnSpeed = 0.1f;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (isServer && netIdentity.observers.Count > 0)
|
||||
ShootNearestPlayer();
|
||||
|
||||
if (isClient)
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed);
|
||||
}
|
||||
|
||||
[Server]
|
||||
void ShootNearestPlayer()
|
||||
{
|
||||
GameObject target = null;
|
||||
float distance = 100f;
|
||||
|
||||
foreach (NetworkConnectionToClient networkConnection in netIdentity.observers.Values)
|
||||
{
|
||||
GameObject tempTarget = networkConnection.identity.gameObject;
|
||||
float tempDistance = Vector3.Distance(tempTarget.transform.position, transform.position);
|
||||
|
||||
if (target == null || distance > tempDistance)
|
||||
{
|
||||
target = tempTarget;
|
||||
distance = tempDistance;
|
||||
}
|
||||
}
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
transform.LookAt(new Vector3(target.transform.position.x, 0, target.transform.position.z));
|
||||
rotation = transform.rotation;
|
||||
//networkAnimator.SetTrigger("Shoot");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a25c54cd35eb284eb6b8ed19cf60443
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/AdditiveScenes/Scripts/ShootingTankBehaviour.cs
|
||||
uploadId: 736421
|
42
Assets/Mirror/Examples/AdditiveScenes/Scripts/ZoneHandler.cs
Normal file
42
Assets/Mirror/Examples/AdditiveScenes/Scripts/ZoneHandler.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.AdditiveScenes
|
||||
{
|
||||
// AdditiveNetworkManager, in OnStartServer, instantiates the prefab only on the server.
|
||||
// It never exists for clients (other than host client if there is one).
|
||||
// The prefab has a Sphere Collider with isTrigger = true.
|
||||
// These OnTrigger events only run on the server and will only send a message to the
|
||||
// client that entered the Zone to load the subscene assigned to the subscene property.
|
||||
public class ZoneHandler : MonoBehaviour
|
||||
{
|
||||
[Scene]
|
||||
[Tooltip("Assign the sub-scene to load for this zone")]
|
||||
public string subScene;
|
||||
|
||||
[ServerCallback]
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
// ignore collisions with non-Player objects
|
||||
if (!other.CompareTag("Player")) return;
|
||||
|
||||
if (other.TryGetComponent(out NetworkIdentity networkIdentity))
|
||||
{
|
||||
SceneMessage message = new SceneMessage { sceneName = subScene, sceneOperation = SceneOperation.LoadAdditive };
|
||||
networkIdentity.connectionToClient.Send(message);
|
||||
}
|
||||
}
|
||||
|
||||
[ServerCallback]
|
||||
void OnTriggerExit(Collider other)
|
||||
{
|
||||
// ignore collisions with non-Player objects
|
||||
if (!other.CompareTag("Player")) return;
|
||||
|
||||
if (other.TryGetComponent(out NetworkIdentity networkIdentity))
|
||||
{
|
||||
SceneMessage message = new SceneMessage { sceneName = subScene, sceneOperation = SceneOperation.UnloadAdditive };
|
||||
networkIdentity.connectionToClient.Send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 884ed76587eb5854abe6b428b791fdcd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/AdditiveScenes/Scripts/ZoneHandler.cs
|
||||
uploadId: 736421
|
Reference in New Issue
Block a user