GitHub
4 年前
当前提交
afc7b64e
共有 7 个文件被更改,包括 288 次插入 和 97 次删除
-
1UOP1_Project/Assets/Prefabs/Pig.prefab
-
189UOP1_Project/Assets/Scenes/CharController.unity
-
16UOP1_Project/Assets/Scripts/CameraManager.cs
-
82UOP1_Project/Assets/Prefabs/SpawnSystem.prefab
-
7UOP1_Project/Assets/Prefabs/SpawnSystem.prefab.meta
-
79UOP1_Project/Assets/Scripts/SpawnSystem.cs
-
11UOP1_Project/Assets/Scripts/SpawnSystem.cs.meta
|
|||
%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: 65dde41d869e66e4d87103c43970a518, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
_defaultSpawnIndex: 0 |
|||
_playerPrefab: {fileID: 211818859182309264, guid: 0fa393e1e37bc9e4e829c25a9452bcd3, |
|||
type: 3} |
|||
_inputReader: {fileID: 0} |
|||
_gameplayCamera: {fileID: 0} |
|||
_spawnLocations: [] |
|||
--- !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} |
|
|||
fileFormatVersion: 2 |
|||
guid: e978d53d440e0814086759404585ac32 |
|||
PrefabImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Linq; |
|||
using UnityEditor.PackageManager; |
|||
using UnityEngine; |
|||
using UnityEngine.Events; |
|||
|
|||
public class SpawnSystem : MonoBehaviour |
|||
{ |
|||
[Header("Settings")] |
|||
[SerializeField] private int _defaultSpawnIndex = 0; |
|||
|
|||
[Header("Project References")] |
|||
[SerializeField] private Protagonist _playerPrefab = null; |
|||
|
|||
[Header("Scene References")] |
|||
[SerializeField] private CameraManager _cameraManager; |
|||
[SerializeField] private Transform[] _spawnLocations; |
|||
|
|||
void Awake() |
|||
{ |
|||
try |
|||
{ |
|||
Spawn(_defaultSpawnIndex); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
Debug.LogError($"[SpawnSystem] Failed to spawn player. {e.Message}"); |
|||
} |
|||
} |
|||
|
|||
void Reset() |
|||
{ |
|||
AutoFill(); |
|||
} |
|||
|
|||
[ContextMenu("Attempt Auto Fill")] |
|||
private void AutoFill() |
|||
{ |
|||
if(_cameraManager == null) |
|||
_cameraManager = FindObjectOfType<CameraManager>(); |
|||
|
|||
if (_spawnLocations == null || _spawnLocations.Length == 0) |
|||
_spawnLocations = transform.GetComponentsInChildren<Transform>(true) |
|||
.Where(t => t != this.transform) |
|||
.ToArray(); |
|||
} |
|||
|
|||
private void Spawn(int spawnIndex) |
|||
{ |
|||
Transform spawnLocation = GetSpawnLocation(spawnIndex, _spawnLocations); |
|||
Protagonist playerInstance = InstantiatePlayer(_playerPrefab, spawnLocation, _cameraManager); |
|||
SetupCameras(playerInstance); |
|||
} |
|||
|
|||
private 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 Protagonist InstantiatePlayer(Protagonist playerPrefab, Transform spawnLocation, CameraManager _cameraManager) |
|||
{ |
|||
if (playerPrefab == null) |
|||
throw new Exception("Player Prefab can't be null."); |
|||
|
|||
Protagonist playerInstance = Instantiate(playerPrefab, spawnLocation.position, spawnLocation.rotation); |
|||
|
|||
return playerInstance; |
|||
} |
|||
|
|||
private void SetupCameras(Protagonist player) |
|||
{ |
|||
player.gameplayCamera = _cameraManager.mainCamera.transform; |
|||
_cameraManager.SetupProtagonistVirtualCamera(player.transform); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 65dde41d869e66e4d87103c43970a518 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue