架构大更
This commit is contained in:
@@ -1,63 +1,61 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// A simple class used to store fog properties
|
||||
/// A simple class used to store fog properties
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class FogSettings
|
||||
{
|
||||
public bool FogEnabled = true;
|
||||
public Color FogColor = Color.white;
|
||||
public float FogDensity = 0.01f;
|
||||
public UnityEngine.FogMode FogMode = FogMode.ExponentialSquared;
|
||||
}
|
||||
public class FogSettings
|
||||
{
|
||||
public bool FogEnabled = true;
|
||||
public Color FogColor = Color.white;
|
||||
public float FogDensity = 0.01f;
|
||||
public FogMode FogMode = FogMode.ExponentialSquared;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add this class to a camera and it will override fog settings when active
|
||||
/// Add this class to a camera and it will override fog settings when active
|
||||
/// </summary>
|
||||
[ExecuteAlways]
|
||||
public class MMCameraFog : MonoBehaviour
|
||||
{
|
||||
/// the settings to use to override fog settings
|
||||
public FogSettings Settings;
|
||||
public class MMCameraFog : MonoBehaviour
|
||||
{
|
||||
/// the settings to use to override fog settings
|
||||
public FogSettings Settings;
|
||||
|
||||
protected FogSettings _previousSettings;
|
||||
protected FogSettings _previousSettings;
|
||||
|
||||
protected void Awake()
|
||||
{
|
||||
_previousSettings = new FogSettings();
|
||||
}
|
||||
protected void Awake()
|
||||
{
|
||||
_previousSettings = new FogSettings();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// On pre render we store our current fog settings and override them
|
||||
/// </summary>
|
||||
protected virtual void OnPreRender()
|
||||
{
|
||||
_previousSettings.FogEnabled = RenderSettings.fog;
|
||||
_previousSettings.FogColor = RenderSettings.fogColor;
|
||||
_previousSettings.FogDensity = RenderSettings.fogDensity;
|
||||
_previousSettings.FogMode = RenderSettings.fogMode;
|
||||
/// <summary>
|
||||
/// On post render we restore fog settings
|
||||
/// </summary>
|
||||
protected virtual void OnPostRender()
|
||||
{
|
||||
RenderSettings.fog = _previousSettings.FogEnabled;
|
||||
RenderSettings.fogColor = _previousSettings.FogColor;
|
||||
RenderSettings.fogDensity = _previousSettings.FogDensity;
|
||||
RenderSettings.fogMode = _previousSettings.FogMode;
|
||||
}
|
||||
|
||||
RenderSettings.fog = Settings.FogEnabled;
|
||||
RenderSettings.fogColor = Settings.FogColor;
|
||||
RenderSettings.fogDensity = Settings.FogDensity;
|
||||
RenderSettings.fogMode = Settings.FogMode;
|
||||
}
|
||||
/// <summary>
|
||||
/// On pre render we store our current fog settings and override them
|
||||
/// </summary>
|
||||
protected virtual void OnPreRender()
|
||||
{
|
||||
_previousSettings.FogEnabled = RenderSettings.fog;
|
||||
_previousSettings.FogColor = RenderSettings.fogColor;
|
||||
_previousSettings.FogDensity = RenderSettings.fogDensity;
|
||||
_previousSettings.FogMode = RenderSettings.fogMode;
|
||||
|
||||
/// <summary>
|
||||
/// On post render we restore fog settings
|
||||
/// </summary>
|
||||
protected virtual void OnPostRender()
|
||||
{
|
||||
RenderSettings.fog = _previousSettings.FogEnabled;
|
||||
RenderSettings.fogColor = _previousSettings.FogColor;
|
||||
RenderSettings.fogDensity = _previousSettings.FogDensity;
|
||||
RenderSettings.fogMode = _previousSettings.FogMode;
|
||||
}
|
||||
}
|
||||
RenderSettings.fog = Settings.FogEnabled;
|
||||
RenderSettings.fogColor = Settings.FogColor;
|
||||
RenderSettings.fogDensity = Settings.FogDensity;
|
||||
RenderSettings.fogMode = Settings.FogMode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,124 +1,122 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Add this class to an object and it will automatically teleport to the other end of the screen when reaching the screen's edges
|
||||
/// Add this class to an object and it will automatically teleport to the other end of the screen when reaching the
|
||||
/// screen's edges
|
||||
/// </summary>
|
||||
public class MMViewportEdgeTeleporter : MonoBehaviour
|
||||
{
|
||||
[Header("Camera")]
|
||||
/// whether or not to grab the Camera.main and store it on init
|
||||
public bool AutoGrabMainCamera;
|
||||
/// the camera used to compute viewport positions
|
||||
public Camera MainCamera;
|
||||
{
|
||||
[Header("Camera")]
|
||||
/// whether or not to grab the Camera.main and store it on init
|
||||
public bool AutoGrabMainCamera;
|
||||
|
||||
[Header("Viewport Bounds")]
|
||||
/// the origin values of the viewport
|
||||
[MMVector("X","Y")]
|
||||
public Vector2 ViewportOrigin = new Vector2(0f, 0f);
|
||||
/// the dimensions of the viewport
|
||||
[MMVector("W","H")]
|
||||
public Vector2 ViewportDimensions = new Vector2(1f, 1f);
|
||||
|
||||
[Header("Teleport Bounds")]
|
||||
/// the origin of the teleport destination zone
|
||||
[MMVector("X","Y")]
|
||||
public Vector2 TeleportOrigin = new Vector2(0f, 0f);
|
||||
/// the dimensions of the teleport destination zone
|
||||
[MMVector("W","H")]
|
||||
public Vector2 TeleportDimensions = new Vector2(1f, 1f);
|
||||
/// the camera used to compute viewport positions
|
||||
public Camera MainCamera;
|
||||
|
||||
[Header("Events")]
|
||||
/// an event to trigger on teleport
|
||||
public UnityEvent OnTeleport;
|
||||
|
||||
protected Vector3 _viewportPosition;
|
||||
protected Vector3 _newViewportPosition;
|
||||
|
||||
/// <summary>
|
||||
/// On Awake we initialize our teleporter
|
||||
/// </summary>
|
||||
protected virtual void Awake()
|
||||
{
|
||||
Initialization();
|
||||
}
|
||||
[Header("Viewport Bounds")]
|
||||
/// the origin values of the viewport
|
||||
[MMVector("X", "Y")]
|
||||
public Vector2 ViewportOrigin = new(0f, 0f);
|
||||
|
||||
/// <summary>
|
||||
/// Stores the main camera if needed
|
||||
/// </summary>
|
||||
protected virtual void Initialization()
|
||||
{
|
||||
if (AutoGrabMainCamera)
|
||||
{
|
||||
MainCamera = Camera.main;
|
||||
}
|
||||
}
|
||||
/// the dimensions of the viewport
|
||||
[MMVector("W", "H")] public Vector2 ViewportDimensions = new(1f, 1f);
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new camera
|
||||
/// </summary>
|
||||
/// <param name="newCamera"></param>
|
||||
public virtual void SetCamera(Camera newCamera)
|
||||
{
|
||||
MainCamera = newCamera;
|
||||
}
|
||||
[Header("Teleport Bounds")]
|
||||
/// the origin of the teleport destination zone
|
||||
[MMVector("X", "Y")]
|
||||
public Vector2 TeleportOrigin = new(0f, 0f);
|
||||
|
||||
/// <summary>
|
||||
/// On Update we check our position relative to the edges
|
||||
/// </summary>
|
||||
protected virtual void Update()
|
||||
{
|
||||
DetectEdges();
|
||||
}
|
||||
/// the dimensions of the teleport destination zone
|
||||
[MMVector("W", "H")] public Vector2 TeleportDimensions = new(1f, 1f);
|
||||
|
||||
/// <summary>
|
||||
/// Detects edges, compares with our object's position, and moves it if needed
|
||||
/// </summary>
|
||||
protected virtual void DetectEdges()
|
||||
{
|
||||
_viewportPosition = MainCamera.WorldToViewportPoint(this.transform.position);
|
||||
|
||||
bool teleport = false;
|
||||
|
||||
if (_viewportPosition.x < ViewportOrigin.x)
|
||||
{
|
||||
_newViewportPosition.x = TeleportDimensions.x;
|
||||
_newViewportPosition.y = _viewportPosition.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
else if (_viewportPosition.x >= ViewportDimensions.x)
|
||||
{
|
||||
_newViewportPosition.x = TeleportOrigin.x;
|
||||
_newViewportPosition.y = _viewportPosition.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
if (_viewportPosition.y < ViewportOrigin.y)
|
||||
{
|
||||
_newViewportPosition.x = _viewportPosition.x;
|
||||
_newViewportPosition.y = TeleportDimensions.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
else if (_viewportPosition.y >= ViewportDimensions.y)
|
||||
{
|
||||
_newViewportPosition.x = _viewportPosition.x;
|
||||
_newViewportPosition.y = TeleportOrigin.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
[Header("Events")]
|
||||
/// an event to trigger on teleport
|
||||
public UnityEvent OnTeleport;
|
||||
|
||||
if (teleport)
|
||||
{
|
||||
OnTeleport?.Invoke();
|
||||
this.transform.position = MainCamera.ViewportToWorldPoint(_newViewportPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected Vector3 _newViewportPosition;
|
||||
|
||||
protected Vector3 _viewportPosition;
|
||||
|
||||
/// <summary>
|
||||
/// On Awake we initialize our teleporter
|
||||
/// </summary>
|
||||
protected virtual void Awake()
|
||||
{
|
||||
Initialization();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// On Update we check our position relative to the edges
|
||||
/// </summary>
|
||||
protected virtual void Update()
|
||||
{
|
||||
DetectEdges();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores the main camera if needed
|
||||
/// </summary>
|
||||
protected virtual void Initialization()
|
||||
{
|
||||
if (AutoGrabMainCamera) MainCamera = Camera.main;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new camera
|
||||
/// </summary>
|
||||
/// <param name="newCamera"></param>
|
||||
public virtual void SetCamera(Camera newCamera)
|
||||
{
|
||||
MainCamera = newCamera;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detects edges, compares with our object's position, and moves it if needed
|
||||
/// </summary>
|
||||
protected virtual void DetectEdges()
|
||||
{
|
||||
_viewportPosition = MainCamera.WorldToViewportPoint(transform.position);
|
||||
|
||||
var teleport = false;
|
||||
|
||||
if (_viewportPosition.x < ViewportOrigin.x)
|
||||
{
|
||||
_newViewportPosition.x = TeleportDimensions.x;
|
||||
_newViewportPosition.y = _viewportPosition.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
else if (_viewportPosition.x >= ViewportDimensions.x)
|
||||
{
|
||||
_newViewportPosition.x = TeleportOrigin.x;
|
||||
_newViewportPosition.y = _viewportPosition.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
|
||||
if (_viewportPosition.y < ViewportOrigin.y)
|
||||
{
|
||||
_newViewportPosition.x = _viewportPosition.x;
|
||||
_newViewportPosition.y = TeleportDimensions.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
else if (_viewportPosition.y >= ViewportDimensions.y)
|
||||
{
|
||||
_newViewportPosition.x = _viewportPosition.x;
|
||||
_newViewportPosition.y = TeleportOrigin.y;
|
||||
_newViewportPosition.z = _viewportPosition.z;
|
||||
teleport = true;
|
||||
}
|
||||
|
||||
if (teleport)
|
||||
{
|
||||
OnTeleport?.Invoke();
|
||||
transform.position = MainCamera.ViewportToWorldPoint(_newViewportPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user