浏览代码

Introduced RuntimeAnchors

Used in Spawn and CameraSystem for decoupling purposes
/main
Ciro Continisio 4 年前
当前提交
8da148bf
共有 21 个文件被更改,包括 214 次插入89 次删除
  1. 3
      UOP1_Project/Assets/Prefabs/Characters/PigChef.prefab
  2. 2
      UOP1_Project/Assets/Prefabs/GameplayEssentials/CameraSystem.prefab
  3. 4
      UOP1_Project/Assets/Prefabs/GameplayEssentials/SpawnSystem.prefab
  4. 103
      UOP1_Project/Assets/Scenes/TestingGround.unity
  5. 4
      UOP1_Project/Assets/Scripts/CameraManager.cs
  6. 9
      UOP1_Project/Assets/Scripts/CameraManager.cs.meta
  7. 29
      UOP1_Project/Assets/Scripts/Characters/Protagonist.cs
  8. 3
      UOP1_Project/Assets/Scripts/Characters/Protagonist.cs.meta
  9. 26
      UOP1_Project/Assets/Scripts/SpawnSystem.cs
  10. 8
      UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors.meta
  11. 8
      UOP1_Project/Assets/Scripts/RuntimeAnchors.meta
  12. 17
      UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/GameplayCameraTransform.asset
  13. 8
      UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/GameplayCameraTransform.asset.meta
  14. 17
      UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/PlayerTransform.asset
  15. 8
      UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/PlayerTransform.asset.meta
  16. 8
      UOP1_Project/Assets/Scripts/RuntimeAnchors/RuntimeAnchorBase.cs
  17. 11
      UOP1_Project/Assets/Scripts/RuntimeAnchors/RuntimeAnchorBase.cs.meta
  18. 24
      UOP1_Project/Assets/Scripts/RuntimeAnchors/TransformAnchor.cs
  19. 11
      UOP1_Project/Assets/Scripts/RuntimeAnchors/TransformAnchor.cs.meta

3
UOP1_Project/Assets/Prefabs/Characters/PigChef.prefab


m_Name:
m_EditorClassIdentifier:
_inputReader: {fileID: 11400000, guid: 945ec0365077176418488737deed54be, type: 2}
gameplayCamera: {fileID: 0}
gameplayCameraTransform: {fileID: 11400000, guid: bc205269957643647a8b5f98f028f9fb,
type: 2}
jumpInput: 0
extraActionInput: 0
movementInput: {x: 0, y: 0, z: 0}

2
UOP1_Project/Assets/Prefabs/GameplayEssentials/CameraSystem.prefab


mainCamera: {fileID: 8745341642014614481}
freeLookVCam: {fileID: 8745341641394998849}
speed: 2
_cameraTransformAnchor: {fileID: 11400000, guid: bc205269957643647a8b5f98f028f9fb,
type: 2}
_frameObjectChannel: {fileID: 11400000, guid: 2723b3f59f7ede3498fe7e385d2bb6ee,
type: 2}
--- !u!1 &8745341641394998848

4
UOP1_Project/Assets/Prefabs/GameplayEssentials/SpawnSystem.prefab


_defaultSpawnIndex: 0
_playerPrefab: {fileID: 5557640735889932260, guid: 062b3805bf6784e4d9c599ee60eaa002,
type: 3}
_playerInstantiatedChannel: {fileID: 11400000, guid: 2723b3f59f7ede3498fe7e385d2bb6ee,
type: 2}
_frameObjectChannel: {fileID: 11400000, guid: 2723b3f59f7ede3498fe7e385d2bb6ee,
type: 2}
--- !u!1 &2125786286893897213
GameObject:
m_ObjectHideFlags: 0

103
UOP1_Project/Assets/Scenes/TestingGround.unity
文件差异内容过多而无法显示
查看文件

4
UOP1_Project/Assets/Scripts/CameraManager.cs


[SerializeField, Range(1f, 5f)]
private float speed = default;
[SerializeField] private TransformAnchor _cameraTransformAnchor = default;
public void SetupProtagonistVirtualCamera(Transform target)
{

if(_frameObjectChannel != null)
_frameObjectChannel.OnEventRaised += OnFrameObjectEvent;
_cameraTransformAnchor.Transform = mainCamera.transform;
}
private void OnDisable()

9
UOP1_Project/Assets/Scripts/CameraManager.cs.meta


MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
defaultReferences:
- inputReader: {fileID: 11400000, guid: 945ec0365077176418488737deed54be, type: 2}
- mainCamera: {instanceID: 0}
- freeLookVCam: {instanceID: 0}
- _cameraTransformAnchor: {fileID: 11400000, guid: bc205269957643647a8b5f98f028f9fb,
type: 2}
- _frameObjectChannel: {fileID: 11400000, guid: 2723b3f59f7ede3498fe7e385d2bb6ee,
type: 2}
executionOrder: 0
icon: {instanceID: 0}
userData:

29
UOP1_Project/Assets/Scripts/Characters/Protagonist.cs


public class Protagonist : MonoBehaviour
{
[SerializeField] private InputReader _inputReader = default;
public Transform gameplayCamera;
public TransformAnchor gameplayCameraTransform;
private Vector2 _previousMovementInput;

private void RecalculateMovement()
{
//Get the two axes from the camera and flatten them on the XZ plane
Vector3 cameraForward = gameplayCamera.forward;
cameraForward.y = 0f;
Vector3 cameraRight = gameplayCamera.right;
cameraRight.y = 0f;
if(gameplayCameraTransform.isSet)
{
//Get the two axes from the camera and flatten them on the XZ plane
Vector3 cameraForward = gameplayCameraTransform.Transform.forward;
cameraForward.y = 0f;
Vector3 cameraRight = gameplayCameraTransform.Transform.right;
cameraRight.y = 0f;
//Use the two axes, modulated by the corresponding inputs, and construct the final vector
Vector3 adjustedMovement = cameraRight.normalized * _previousMovementInput.x +
cameraForward.normalized * _previousMovementInput.y;
//Use the two axes, modulated by the corresponding inputs, and construct the final vector
Vector3 adjustedMovement = cameraRight.normalized * _previousMovementInput.x +
cameraForward.normalized * _previousMovementInput.y;
movementInput = Vector3.ClampMagnitude(adjustedMovement, 1f);
movementInput = Vector3.ClampMagnitude(adjustedMovement, 1f);
}
else
{
//No CameraManager exists in the scene, so the input is just used absolute in world-space
Debug.LogWarning("No gameplay camera in the scene. Movement orientation will not be correct.");
movementInput = new Vector3(_previousMovementInput.x, 0f, _previousMovementInput.y);
}
}
//---- EVENT LISTENERS ----

3
UOP1_Project/Assets/Scripts/Characters/Protagonist.cs.meta


serializedVersion: 2
defaultReferences:
- _inputReader: {fileID: 11400000, guid: 945ec0365077176418488737deed54be, type: 2}
- gameplayCamera: {instanceID: 0}
- gameplayCameraTransform: {fileID: 11400000, guid: bc205269957643647a8b5f98f028f9fb,
type: 2}
executionOrder: 0
icon: {instanceID: 0}
userData:

26
UOP1_Project/Assets/Scripts/SpawnSystem.cs


using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;
public class SpawnSystem : MonoBehaviour
{

[Header("Project References")]
[SerializeField] private Protagonist _playerPrefab = null;
[Header("Asset References")]
[SerializeField] private Protagonist _playerPrefab = default;
[SerializeField] private TransformEventChannelSO _playerInstantiatedChannel = default;
[SerializeField] private TransformEventChannelSO _frameObjectChannel;
try
{
Spawn(_defaultSpawnIndex);
}
catch (Exception e)
{
Debug.LogError($"[SpawnSystem] Failed to spawn player. {e.Message}");
}
Spawn(_defaultSpawnIndex);
}
void Reset()

{
Transform spawnLocation = GetSpawnLocation(spawnIndex, _spawnLocations);
Protagonist playerInstance = InstantiatePlayer(_playerPrefab, spawnLocation);
SetupCameras(playerInstance);
_playerInstantiatedChannel.RaiseEvent(playerInstance.transform); // The CameraSystem will pick this up to frame the player
}
private Transform GetSpawnLocation(int index, Transform[] spawnLocations)

Protagonist playerInstance = Instantiate(playerPrefab, spawnLocation.position, spawnLocation.rotation);
return playerInstance;
}
private void SetupCameras(Protagonist player)
{
//TODO: update this hacky assignment of mainCamera
player.gameplayCamera = UnityEngine.Object.FindObjectOfType<CameraManager>().mainCamera.transform;
_frameObjectChannel.RaiseEvent(player.transform);
}
}

8
UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors.meta


fileFormatVersion: 2
guid: 350f007900383bb498b0b44aa6129b13
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
UOP1_Project/Assets/Scripts/RuntimeAnchors.meta


fileFormatVersion: 2
guid: 7de16a309da007146a8cc69bcfd88b18
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

17
UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/GameplayCameraTransform.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 25b753e3c82159746919d114a5f44202, type: 3}
m_Name: GameplayCameraTransform
m_EditorClassIdentifier:
description: Transform of the GameplayCamera. The CameraSystem provides this in
its OnEnable.
isSet: 0

8
UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/GameplayCameraTransform.asset.meta


fileFormatVersion: 2
guid: bc205269957643647a8b5f98f028f9fb
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

17
UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/PlayerTransform.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 25b753e3c82159746919d114a5f44202, type: 3}
m_Name: PlayerTransform
m_EditorClassIdentifier:
description: Transform of the player, if currently instantiated. Provided by the
SpawnSystem every time it instantiates the player.
isSet: 0

8
UOP1_Project/Assets/ScriptableObjects/RuntimeAnchors/PlayerTransform.asset.meta


fileFormatVersion: 2
guid: 35fc4039342b6ba458d0d4429e89ee74
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

8
UOP1_Project/Assets/Scripts/RuntimeAnchors/RuntimeAnchorBase.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RuntimeAnchorBase : ScriptableObject
{
[TextArea] public string description;
}

11
UOP1_Project/Assets/Scripts/RuntimeAnchors/RuntimeAnchorBase.cs.meta


fileFormatVersion: 2
guid: ba691968e9e113945b797dee9c1ead0b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

24
UOP1_Project/Assets/Scripts/RuntimeAnchors/TransformAnchor.cs


using UnityEngine;
using System.Collections;
[CreateAssetMenu(menuName = "Runtime Anchors/Transform")]
public class TransformAnchor : RuntimeAnchorBase
{
[HideInInspector] public bool isSet = false; // Any script can check if the transform is null before using it, by just checking this bool
private Transform _transform;
public Transform Transform
{
get { return _transform; }
set {
_transform = value;
isSet = _transform != null;
}
}
public void OnDisable()
{
_transform = null;
isSet = false;
}
}

11
UOP1_Project/Assets/Scripts/RuntimeAnchors/TransformAnchor.cs.meta


fileFormatVersion: 2
guid: 25b753e3c82159746919d114a5f44202
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存