浏览代码

My take on a simple Player Spawn System

/main
Wodopo 4 年前
当前提交
1eef189c
共有 2 个文件被更改,包括 190 次插入0 次删除
  1. 86
      UOP1_Project/Assets/Prefabs/SpawnSystem.prefab
  2. 104
      UOP1_Project/Assets/Scripts/SpawnSystem.cs

86
UOP1_Project/Assets/Prefabs/SpawnSystem.prefab


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2125786285293829329
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2125786285293829335}
- component: {fileID: 2125786285293829334}
m_Layer: 0
m_Name: SpawnSystem
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2125786285293829335
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2125786285293829329}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 2125786286893897154}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2125786285293829334
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2125786285293829329}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4f134b859b2acc54d95c5cdac4348075, type: 3}
m_Name:
m_EditorClassIdentifier:
defaultSpawnIndex: 0
playerPrefab: {fileID: 211818859182309264, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
inputReader: {fileID: 0}
gameplayCamera: {fileID: 0}
spawnLocations:
- {fileID: 2125786286893897154}
onPlayerSpawnEvent:
m_PersistentCalls:
m_Calls: []
--- !u!1 &2125786286893897213
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2125786286893897154}
m_Layer: 0
m_Name: Location 01
m_TagString: Untagged
m_Icon: {fileID: 3936346786652291628, guid: 0000000000000000d000000000000000, type: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2125786286893897154
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2125786286893897213}
m_LocalRotation: {x: 0, y: 1, z: 0, w: 0}
m_LocalPosition: {x: -14.173, y: 0.69, z: -9.637}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2125786285293829335}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0}

104
UOP1_Project/Assets/Scripts/SpawnSystem.cs


using System;
using System.Linq;
using UnityEditor.PackageManager;
using UnityEngine;
using UnityEngine.Events;
[Serializable]
public class PlayerSpawnEvent : UnityEvent<Transform> { }
public class SpawnSystem : MonoBehaviour
{
private static int requestedSpawnIndex = -1;
[Header("Settings")]
[SerializeField] private int defaultSpawnIndex = 0;
[Header("Project References")]
#pragma warning disable 649
[SerializeField] private Protagonist playerPrefab;
#pragma warning restore 649
[Header("Scene References")]
[SerializeField] private InputReader inputReader;
[SerializeField] private Transform gameplayCamera;
[SerializeField] private Transform[] spawnLocations;
[Header("Events")]
public PlayerSpawnEvent onPlayerSpawnEvent;
void Awake()
{
try
{
Spawn();
}
catch (Exception e)
{
Debug.LogError($"[SpawnSystem] Failed to spawn player. {e.Message}");
}
}
void Reset()
{
AutoFill();
}
[ContextMenu("Attempt Auto Fill")]
private void AutoFill()
{
if (inputReader == null)
inputReader = FindObjectOfType<InputReader>();
if (gameplayCamera == null)
gameplayCamera = GameObject.FindGameObjectWithTag("MainCamera")?.transform;
if (spawnLocations == null || spawnLocations.Length == 0)
spawnLocations = transform.GetComponentsInChildren<Transform>(true)
.Where(t => t != this.transform)
.ToArray();
}
private void Spawn()
{
int spawnIndex = requestedSpawnIndex < 0 ? defaultSpawnIndex : requestedSpawnIndex;
var spawnLocation = GetSpawnLocation(spawnIndex, spawnLocations);
var playerInstance = InstantiatePlayer(playerPrefab, spawnLocation, inputReader, gameplayCamera);
requestedSpawnIndex = -1;
onPlayerSpawnEvent.Invoke(playerInstance.transform);
}
private static Transform GetSpawnLocation(int index, Transform[] spawnLocations)
{
if (spawnLocations == null || spawnLocations.Length == 0)
throw new Exception("No spawn locations set.");
index = Mathf.Clamp(index, 0, spawnLocations.Length - 1);
return spawnLocations[index];
}
private static Protagonist InstantiatePlayer(Protagonist playerPrefab, Transform spawnLocation, InputReader inputReader, Transform gameplayCamera)
{
if (playerPrefab == null)
throw new Exception("Player Prefab can't be null.");
bool originalState = playerPrefab.enabled;
// Prevents playerInstance's Protagonist.OnEnable from running before we set the inputReader and gameplayCamera references
playerPrefab.enabled = false;
var playerInstance = Instantiate(playerPrefab, spawnLocation.position, spawnLocation.rotation);
playerPrefab.enabled = originalState;
playerInstance.inputReader = inputReader;
playerInstance.gameplayCamera = gameplayCamera;
// Since the prefab's script was disabled it need to be enabled here
playerInstance.enabled = true;
return playerInstance;
}
public static void SetRequestSpawnIndex(int spawnIndex)
{
// Prevent setting negative numbers from external sources
requestedSpawnIndex = Mathf.Max(spawnIndex, 0);
}
}
正在加载...
取消
保存