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
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 9
serializedVersion: 10
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
@ -44,7 +44,6 @@ RenderSettings:
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 12
m_GIWorkflowMode: 1
m_GISettings:
serializedVersion: 2
m_BounceScale: 1
@ -67,9 +66,6 @@ LightmapSettings:
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 1
@ -104,7 +100,7 @@ NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
@ -117,7 +113,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
@ -154,9 +150,17 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
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_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
@ -190,13 +194,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 185921126}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1040404844
GameObject:
@ -267,7 +271,6 @@ RectTransform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1300359894}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
@ -333,7 +336,7 @@ MonoBehaviour:
MaxRetransmit: 40
MaximizeSocketBuffers: 1
ReliableMaxMessageSize: 297433
UnreliableMaxMessageSize: 1195
UnreliableMaxMessageSize: 1194
debugLog: 0
statisticsGUI: 0
statisticsLog: 0
@ -351,9 +354,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
dontDestroyOnLoad: 1
runInBackground: 1
autoStartServerBuild: 1
autoConnectClientBuild: 0
headlessStartMode: 1
editorAutoStart: 0
sendRate: 30
autoStartServerBuild: 0
autoConnectClientBuild: 0
offlineScene: Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsOffline.unity
onlineScene: Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsOnline.unity
transport: {fileID: 1074858615}
@ -362,8 +367,7 @@ MonoBehaviour:
disconnectInactiveConnections: 0
disconnectInactiveTimeout: 60
authenticator: {fileID: 0}
playerPrefab: {fileID: 7619140271685878370, guid: 9f0094c1325091d42a558274b947221f,
type: 3}
playerPrefab: {fileID: 7619140271685878370, guid: 9f0094c1325091d42a558274b947221f, type: 3}
autoCreatePlayer: 0
playerSpawnMethod: 1
spawnPrefabs: []
@ -379,7 +383,8 @@ MonoBehaviour:
dynamicAdjustment: 1
dynamicAdjustmentTolerance: 1
deliveryTimeEmaDuration: 2
connectionQualityInterval: 3
evaluationMethod: 0
evaluationInterval: 3
timeInterpolationGui: 0
additiveScenes:
- Assets/Mirror/Examples/AdditiveLevels/Scenes/MirrorAdditiveLevelsSubLevel1.unity
@ -392,6 +397,7 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1074858613}
serializedVersion: 2
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}
@ -399,7 +405,6 @@ Transform:
m_Children:
- {fileID: 1300359894}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1074858618
MonoBehaviour:
@ -444,9 +449,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 363a8867bb9c7b845a73233566df8c1e, type: 3}
m_Name:
m_EditorClassIdentifier:
fadeImage: {fileID: 1040404845}
fadeColor: {r: 0, g: 0, b: 0, a: 1}
stepRate: 2
panelImage: {fileID: 0}
fadeInTime: 2
fadeOutTime: 2
--- !u!114 &1300359892
MonoBehaviour:
m_ObjectHideFlags: 0
@ -487,7 +492,9 @@ Canvas:
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_VertexColorAlwaysGammaSpace: 0
m_AdditionalShaderChannelsFlag: 0
m_UpdateRectTransformForStandalone: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
@ -505,10 +512,15 @@ RectTransform:
m_Children:
- {fileID: 1040404847}
m_Father: {fileID: 1074858617}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {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
{
// set these in the inspector
[Tooltip("Reference to Image component on child panel")]
public Image fadeImage;
[Header("Components")]
[SerializeField] Image panelImage;
[Tooltip("Color to use during scene transition")]
public Color fadeColor = Color.black;
[Header("Settings")]
[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")]
public byte stepRate = 2;
[SerializeField, Range(1, 10)]
[Tooltip("Time in seconds to fade out")]
byte fadeOutTime = 2;
float step;
bool isFading;
void OnValidate()
{
if (fadeImage == null)
fadeImage = GetComponentInChildren<Image>();
if (panelImage == null)
panelImage = GetComponentInChildren<Image>();
fadeInTime = (byte)Mathf.Max(fadeInTime, 1);
fadeOutTime = (byte)Mathf.Max(fadeOutTime, 1);
}
void Start()
{
// 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 float GetFadeInTime() => fadeInTime + Time.fixedDeltaTime;
public IEnumerator FadeIn()
{
float alpha = fadeImage.color.a;
while (alpha < 1)
{
yield return null;
alpha += step;
fadeColor.a = alpha;
fadeImage.color = fadeColor;
}
//Debug.Log($"FadeIn {isFading}");
yield return FadeImage(0f, 1f, fadeInTime);
}
public float GetFadeOutTime() => fadeOutTime + Time.fixedDeltaTime;
public IEnumerator FadeOut()
{
float alpha = fadeImage.color.a;
//Debug.Log($"FadeOut {isFading}");
yield return FadeImage(1f, 0f, fadeOutTime);
}
while (alpha > 0)
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)
{
yield return null;
alpha -= step;
fadeColor.a = alpha;
fadeImage.color = fadeColor;
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
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
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).
conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true });
// wait for fader to complete
yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetDuration());
// wait for fader to complete.
yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime());
// Remove player after fader has completed
NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn);