fix(FadeInOut): Now based on duration and framerate independent

This commit is contained in:
MrGadget 2024-07-20 17:27:24 -04:00
parent bd75cf0345
commit bcd47d546e
3 changed files with 84 additions and 66 deletions

View File

@ -13,7 +13,7 @@ OcclusionCullingSettings:
--- !u!104 &2 --- !u!104 &2
RenderSettings: RenderSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 9 serializedVersion: 10
m_Fog: 0 m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3 m_FogMode: 3
@ -44,7 +44,6 @@ RenderSettings:
LightmapSettings: LightmapSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 12 serializedVersion: 12
m_GIWorkflowMode: 1
m_GISettings: m_GISettings:
serializedVersion: 2 serializedVersion: 2
m_BounceScale: 1 m_BounceScale: 1
@ -67,9 +66,6 @@ LightmapSettings:
m_LightmapParameters: {fileID: 0} m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1 m_LightmapsBakeMode: 1
m_TextureCompression: 1 m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2 m_ReflectionCompression: 2
m_MixedBakeMode: 2 m_MixedBakeMode: 2
m_BakeBackend: 1 m_BakeBackend: 1
@ -104,7 +100,7 @@ NavMeshSettings:
serializedVersion: 2 serializedVersion: 2
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_BuildSettings: m_BuildSettings:
serializedVersion: 2 serializedVersion: 3
agentTypeID: 0 agentTypeID: 0
agentRadius: 0.5 agentRadius: 0.5
agentHeight: 2 agentHeight: 2
@ -117,7 +113,7 @@ NavMeshSettings:
cellSize: 0.16666667 cellSize: 0.16666667
manualTileSize: 0 manualTileSize: 0
tileSize: 256 tileSize: 256
accuratePlacement: 0 buildHeightMesh: 0
maxJobWorkers: 0 maxJobWorkers: 0
preserveTilesOutsideBounds: 0 preserveTilesOutsideBounds: 0
debug: debug:
@ -154,9 +150,17 @@ Camera:
m_projectionMatrixMode: 1 m_projectionMatrixMode: 1
m_GateFitMode: 2 m_GateFitMode: 2
m_FOVAxisMode: 0 m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24} m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0} m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect: m_NormalizedViewPortRect:
serializedVersion: 2 serializedVersion: 2
x: 0 x: 0
@ -190,13 +194,13 @@ Transform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 185921126} m_GameObject: {fileID: 185921126}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10} m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1040404844 --- !u!1 &1040404844
GameObject: GameObject:
@ -267,7 +271,6 @@ RectTransform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 1300359894} m_Father: {fileID: 1300359894}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1}
@ -333,7 +336,7 @@ MonoBehaviour:
MaxRetransmit: 40 MaxRetransmit: 40
MaximizeSocketBuffers: 1 MaximizeSocketBuffers: 1
ReliableMaxMessageSize: 297433 ReliableMaxMessageSize: 297433
UnreliableMaxMessageSize: 1195 UnreliableMaxMessageSize: 1194
debugLog: 0 debugLog: 0
statisticsGUI: 0 statisticsGUI: 0
statisticsLog: 0 statisticsLog: 0
@ -351,9 +354,11 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
dontDestroyOnLoad: 1 dontDestroyOnLoad: 1
runInBackground: 1 runInBackground: 1
autoStartServerBuild: 1 headlessStartMode: 1
autoConnectClientBuild: 0 editorAutoStart: 0
sendRate: 30 sendRate: 30
autoStartServerBuild: 0
autoConnectClientBuild: 0
offlineScene: Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsOffline.unity offlineScene: Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsOffline.unity
onlineScene: Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsOnline.unity onlineScene: Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsOnline.unity
transport: {fileID: 1074858615} transport: {fileID: 1074858615}
@ -362,8 +367,7 @@ MonoBehaviour:
disconnectInactiveConnections: 0 disconnectInactiveConnections: 0
disconnectInactiveTimeout: 60 disconnectInactiveTimeout: 60
authenticator: {fileID: 0} authenticator: {fileID: 0}
playerPrefab: {fileID: 7619140271685878370, guid: 9f0094c1325091d42a558274b947221f, playerPrefab: {fileID: 7619140271685878370, guid: 9f0094c1325091d42a558274b947221f, type: 3}
type: 3}
autoCreatePlayer: 0 autoCreatePlayer: 0
playerSpawnMethod: 1 playerSpawnMethod: 1
spawnPrefabs: [] spawnPrefabs: []
@ -379,7 +383,8 @@ MonoBehaviour:
dynamicAdjustment: 1 dynamicAdjustment: 1
dynamicAdjustmentTolerance: 1 dynamicAdjustmentTolerance: 1
deliveryTimeEmaDuration: 2 deliveryTimeEmaDuration: 2
connectionQualityInterval: 3 evaluationMethod: 0
evaluationInterval: 3
timeInterpolationGui: 0 timeInterpolationGui: 0
additiveScenes: additiveScenes:
- Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsSubLevel1.unity - Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsSubLevel1.unity
@ -392,6 +397,7 @@ Transform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1074858613} m_GameObject: {fileID: 1074858613}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
@ -399,7 +405,6 @@ Transform:
m_Children: m_Children:
- {fileID: 1300359894} - {fileID: 1300359894}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1074858618 --- !u!114 &1074858618
MonoBehaviour: MonoBehaviour:
@ -444,9 +449,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 363a8867bb9c7b845a73233566df8c1e, type: 3} m_Script: {fileID: 11500000, guid: 363a8867bb9c7b845a73233566df8c1e, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
fadeImage: {fileID: 1040404845} panelImage: {fileID: 0}
fadeColor: {r: 0, g: 0, b: 0, a: 1} fadeInTime: 2
stepRate: 2 fadeOutTime: 2
--- !u!114 &1300359892 --- !u!114 &1300359892
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -487,7 +492,9 @@ Canvas:
m_OverrideSorting: 0 m_OverrideSorting: 0
m_OverridePixelPerfect: 0 m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0 m_SortingBucketNormalizedSize: 0
m_VertexColorAlwaysGammaSpace: 0
m_AdditionalShaderChannelsFlag: 0 m_AdditionalShaderChannelsFlag: 0
m_UpdateRectTransformForStandalone: 0
m_SortingLayerID: 0 m_SortingLayerID: 0
m_SortingOrder: 0 m_SortingOrder: 0
m_TargetDisplay: 0 m_TargetDisplay: 0
@ -505,10 +512,15 @@ RectTransform:
m_Children: m_Children:
- {fileID: 1040404847} - {fileID: 1040404847}
m_Father: {fileID: 1074858617} m_Father: {fileID: 1074858617}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0} m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0} m_Pivot: {x: 0, y: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 185921129}
- {fileID: 1074858617}

View File

@ -6,66 +6,72 @@ namespace Mirror.Examples.AdditiveLevels
{ {
public class FadeInOut : MonoBehaviour public class FadeInOut : MonoBehaviour
{ {
// set these in the inspector [Header("Components")]
[Tooltip("Reference to Image component on child panel")] [SerializeField] Image panelImage;
public Image fadeImage;
[Tooltip("Color to use during scene transition")] [Header("Settings")]
public Color fadeColor = Color.black; [SerializeField, Range(1, 10)]
[Tooltip("Time in seconds to fade in")]
byte fadeInTime = 2;
[Range(1, 100), Tooltip("Rate of fade in / out: higher is faster")] [SerializeField, Range(1, 10)]
public byte stepRate = 2; [Tooltip("Time in seconds to fade out")]
byte fadeOutTime = 2;
float step; bool isFading;
void OnValidate() void OnValidate()
{ {
if (fadeImage == null) if (panelImage == null)
fadeImage = GetComponentInChildren<Image>(); panelImage = GetComponentInChildren<Image>();
fadeInTime = (byte)Mathf.Max(fadeInTime, 1);
fadeOutTime = (byte)Mathf.Max(fadeOutTime, 1);
} }
void Start() public float GetFadeInTime() => fadeInTime + Time.fixedDeltaTime;
{
// Convert user-friendly setting value to working value
step = stepRate * 0.001f;
}
/// <summary>
/// Calculates FadeIn / FadeOut time.
/// </summary>
/// <returns>Duration in seconds</returns>
public float GetDuration()
{
float frames = 1 / step;
float frameRate = Time.deltaTime;
float duration = frames * frameRate * 0.1f;
return duration;
}
public IEnumerator FadeIn() public IEnumerator FadeIn()
{ {
float alpha = fadeImage.color.a; //Debug.Log($"FadeIn {isFading}");
yield return FadeImage(0f, 1f, fadeInTime);
}
while (alpha < 1) public float GetFadeOutTime() => fadeOutTime + Time.fixedDeltaTime;
{
yield return null;
alpha += step;
fadeColor.a = alpha;
fadeImage.color = fadeColor;
}
}
public IEnumerator FadeOut() public IEnumerator FadeOut()
{ {
float alpha = fadeImage.color.a; //Debug.Log($"FadeOut {isFading}");
yield return FadeImage(1f, 0f, fadeOutTime);
while (alpha > 0)
{
yield return null;
alpha -= step;
fadeColor.a = alpha;
fadeImage.color = fadeColor;
} }
private IEnumerator FadeImage(float startAlpha, float endAlpha, float duration)
{
if (panelImage == null) yield break;
if (isFading) yield break;
// Short circuit if the alpha is already at endAlpha
Color color = panelImage.color;
if (Mathf.Approximately(color.a, endAlpha)) yield break;
isFading = true;
float elapsedTime = 0f;
float fixedDeltaTime = Time.fixedDeltaTime;
while (elapsedTime < duration)
{
elapsedTime += fixedDeltaTime;
float alpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / duration);
panelImage.color = new Color(color.r, color.g, color.b, alpha);
yield return new WaitForFixedUpdate();
}
// Ensure the final alpha value is set
panelImage.color = new Color(color.r, color.g, color.b, endAlpha);
isFading = false;
} }
} }
} }

View File

@ -45,7 +45,7 @@ void OnTriggerEnter(Collider other)
{ {
if (!(other is CapsuleCollider)) return; // ignore CharacterController colliders if (!(other is CapsuleCollider)) return; // ignore CharacterController colliders
Debug.Log($"Portal.OnTriggerEnter {other}"); //Debug.Log($"Portal.OnTriggerEnter {other}");
// tag check in case you didn't set up the layers and matrix as noted above // tag check in case you didn't set up the layers and matrix as noted above
if (!other.CompareTag("Player")) return; if (!other.CompareTag("Player")) return;
@ -68,8 +68,8 @@ IEnumerator SendPlayerToNewScene(GameObject player)
// Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene). // Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene).
conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true }); conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true });
// wait for fader to complete // wait for fader to complete.
yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetDuration()); yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime());
// Remove player after fader has completed // Remove player after fader has completed
NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn); NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn);