Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 59 additions & 55 deletions com.unity.cinemachine/Editor/Obsolete/CinemachineFreeLookEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ protected override void OnEnable()
{
base.OnEnable();
Target.UpdateInputAxisProvider();

CinemachineFreeLook.CreateRigOverride += CreateRigOverride;
}

protected override void OnDisable()
Expand Down Expand Up @@ -172,65 +174,67 @@ void UpdateRigEditor()
}
}

/// <summary>
/// Register with CinemachineFreeLook to create the pipeline in an undo-friendly manner
/// </summary>
[InitializeOnLoad]
class CreateRigWithUndo
static CinemachineVirtualCamera CreateRigOverride(CinemachineFreeLook vcam, string name, CinemachineVirtualCamera copyFrom)
{
static CreateRigWithUndo()
// Recycle the game object if it exists
GameObject go = null;
foreach (Transform child in vcam.transform)
{
CinemachineFreeLook.CreateRigOverride
= (CinemachineFreeLook vcam, string name, CinemachineVirtualCamera copyFrom) =>
if (child.gameObject.name == name)
{
// Recycle the game object if it exists
GameObject go = null;
foreach (Transform child in vcam.transform)
{
if (child.gameObject.name == name)
{
go = child.gameObject;
break;
}
}

CinemachineVirtualCamera rig = null;
if (go == null)
{
// Create a new rig - can't do it if prefab instance
if (PrefabUtility.IsPartOfAnyPrefab(vcam.gameObject))
return null;
go = ObjectFactory.CreateGameObject(name);
Undo.RegisterCreatedObjectUndo(go, "created rig");
Undo.SetTransformParent(go.transform, vcam.transform, "parenting pipeline");
}

// Create a new rig with default components
rig = Undo.AddComponent<CinemachineVirtualCamera>(go);
rig.CreatePipeline(copyFrom);
if (copyFrom != null)
ReflectionHelpers.CopyFields(copyFrom, rig);
else
{
// Defaults
go = rig.GetComponentOwner().gameObject;
Undo.AddComponent<CinemachineOrbitalTransposer>(go);
Undo.AddComponent<CinemachineComposer>(go);
rig.InvalidateComponentPipeline();
}
return rig;
};

CinemachineFreeLook.DestroyRigOverride = (GameObject rig) =>
{
var vcam = rig.GetComponent<CinemachineVirtualCamera>();
if (vcam != null)
{
vcam.DestroyPipeline();
Undo.DestroyObjectImmediate(vcam);
}
};
go = child.gameObject;
break;
}
}

CinemachineVirtualCamera rig = null;
if (go == null)
{
// Create a new rig - can't do it if prefab instance
if (PrefabUtility.IsPartOfAnyPrefab(vcam.gameObject))
return null;
go = ObjectFactory.CreateGameObject(name);
Undo.RegisterCreatedObjectUndo(go, "created rig");
Undo.SetTransformParent(go.transform, vcam.transform, "parenting pipeline");
}

// Create a new rig with default components
rig = Undo.AddComponent<CinemachineVirtualCamera>(go);
rig.CreatePipeline(copyFrom);
if (copyFrom != null)
ReflectionHelpers.CopyFields(copyFrom, rig);
else
{
// Defaults
go = rig.GetComponentOwner().gameObject;
Undo.AddComponent<CinemachineOrbitalTransposer>(go);
Undo.AddComponent<CinemachineComposer>(go);
rig.InvalidateComponentPipeline();
}
return rig;
}

static void DestroyRigOverride(GameObject rig)
{
var vcam = rig.GetComponent<CinemachineVirtualCamera>();
if (vcam != null)
{
vcam.DestroyPipeline();
Undo.DestroyObjectImmediate(vcam);
}
}

[UnityEditor.InitializeOnLoad]
class EditorInitialize { static EditorInitialize() { ResetStaticsOnLoad(); } }

/// <summary>
/// Register with CinemachineFreeLook to create the pipeline in an undo-friendly manner
/// </summary>
[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
CinemachineFreeLook.CreateRigOverride = CreateRigOverride;
CinemachineFreeLook.DestroyRigOverride = DestroyRigOverride;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,66 +162,68 @@ void ResetTargetOnUndo()
(targets[i] as CinemachineVirtualCamera).InvalidateComponentPipeline();
}

/// <summary>
/// Register with CinemachineVirtualCamera to create the pipeline in an undo-friendly manner
/// </summary>
[InitializeOnLoad]
class CreatePipelineWithUndo
static Transform CreatePipelineOverride(CinemachineVirtualCamera vcam, string name, CinemachineComponentBase[] copyFrom)
{
static CreatePipelineWithUndo()
// Recycle existing pipeline child (if any)
GameObject go = null;
foreach (Transform child in vcam.transform)
{
CinemachineVirtualCamera.CreatePipelineOverride =
(CinemachineVirtualCamera vcam, string name, CinemachineComponentBase[] copyFrom) =>
if (child.GetComponent<CinemachinePipeline>() != null)
{
// Recycle existing pipeline child (if any)
GameObject go = null;
foreach (Transform child in vcam.transform)
{
if (child.GetComponent<CinemachinePipeline>() != null)
{
go = child.gameObject;
break;
}
}
if (go == null)
{
// Create a new pipeline - can't do it if prefab instance
if (PrefabUtility.IsPartOfAnyPrefab(vcam.gameObject))
return null;
go = ObjectFactory.CreateGameObject(name);
Undo.RegisterCreatedObjectUndo(go, "created pipeline");
Undo.SetTransformParent(go.transform, vcam.transform, "parenting pipeline");
Undo.AddComponent<CinemachinePipeline>(go);
}

var oldStuff = go.GetComponents<CinemachineComponentBase>();
foreach (var c in oldStuff)
Undo.DestroyObjectImmediate(c);
go = child.gameObject;
break;
}
}
if (go == null)
{
// Create a new pipeline - can't do it if prefab instance
if (PrefabUtility.IsPartOfAnyPrefab(vcam.gameObject))
return null;
go = ObjectFactory.CreateGameObject(name);
Undo.RegisterCreatedObjectUndo(go, "created pipeline");
Undo.SetTransformParent(go.transform, vcam.transform, "parenting pipeline");
Undo.AddComponent<CinemachinePipeline>(go);
}

// If copying, transfer the components
if (copyFrom != null)
{
foreach (Component c in copyFrom)
{
Component copy = Undo.AddComponent(go, c.GetType());
Undo.RecordObject(copy, "copying pipeline");
ReflectionHelpers.CopyFields(c, copy);
}
}
return go.transform;
};
var oldStuff = go.GetComponents<CinemachineComponentBase>();
foreach (var c in oldStuff)
Undo.DestroyObjectImmediate(c);

CinemachineVirtualCamera.DestroyPipelineOverride = (GameObject pipeline) =>
// If copying, transfer the components
if (copyFrom != null)
{
foreach (Component c in copyFrom)
{
var oldStuff = pipeline.GetComponents<CinemachineComponentBase>();
foreach (var c in oldStuff)
Undo.DestroyObjectImmediate(c);
// Cannot create or destroy child objects if prefab.
// Just leave it there in that case, it will get discovered and recycled.
if (!PrefabUtility.IsPartOfAnyPrefab(pipeline))
Undo.DestroyObjectImmediate(pipeline);
};
Component copy = Undo.AddComponent(go, c.GetType());
Undo.RecordObject(copy, "copying pipeline");
ReflectionHelpers.CopyFields(c, copy);
}
}
return go.transform;
}

static void DestroyPipelineOverride(GameObject pipeline)
{
var oldStuff = pipeline.GetComponents<CinemachineComponentBase>();
foreach (var c in oldStuff)
Undo.DestroyObjectImmediate(c);
// Cannot create or destroy child objects if prefab.
// Just leave it there in that case, it will get discovered and recycled.
if (!PrefabUtility.IsPartOfAnyPrefab(pipeline))
Undo.DestroyObjectImmediate(pipeline);
}

[UnityEditor.InitializeOnLoad]
class EditorInitialize { static EditorInitialize() { ResetStaticsOnLoad(); } }

/// <summary>
/// Register with CinemachineVirtualCamera to create the pipeline in an undo-friendly manner
/// </summary>
[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
CinemachineVirtualCamera.CreatePipelineOverride = CreatePipelineOverride;
CinemachineVirtualCamera.DestroyPipelineOverride = DestroyPipelineOverride;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ protected override void WritePrefs(bool value)
CinemachineDebug.GameViewGuidesEnabled = value;
}
}

[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
CinemachineDebug.GameViewGuidesEnabled = ShowInGameGuides.Value;
}

static CinemachineSettings.BoolItem s_SettingsFoldedOut = new("CNMCN_Core_Folded", true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public struct TerrainSettings
static float[] s_ColliderDistanceBuffer = new float[kColliderBufferSize];
static int[] s_ColliderOrderBuffer = new int[kColliderBufferSize];

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
Array.Fill(s_ColliderBuffer, null);
Array.Fill(s_ColliderDistanceBuffer, 0f);
Array.Fill(s_ColliderOrderBuffer, 0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI i am fixing the analyzer to consider calls to Array.Fill as successful initialization

}
#endif

// Farthest stuff comes first
static readonly IComparer<int> s_ColliderBufferSorter = Comparer<int>.Create((a, b) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,14 @@ static float ClampRayToBounds(Ray ray, float distance, Bounds bounds)

static Collider[] s_ColliderBuffer = new Collider[5];

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
Array.Fill(s_ColliderBuffer, null);
}
#endif

Vector3 RespectCameraRadius(Vector3 cameraPos, Vector3 lookAtPos)
{
var result = Vector3.zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ public static void AddCanvas(UnityEngine.Object canvas, UnityEngine.Object owner
s_CanvasesAndTheirOwners.Add(canvas, owner);
}
}

[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
s_StoryboardGlobalMute = false;
}
#endif
}
}
Expand Down
2 changes: 1 addition & 1 deletion com.unity.cinemachine/Runtime/Core/CameraState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct CameraState
/// <summary>
/// This constant represents "no point in space" or "no direction".
/// </summary>
public static Vector3 kNoPoint = new Vector3(float.NaN, float.NaN, float.NaN);
public static readonly Vector3 kNoPoint = new Vector3(float.NaN, float.NaN, float.NaN);

/// <summary>
/// Raw (un-corrected) world space position of this camera
Expand Down
13 changes: 13 additions & 0 deletions com.unity.cinemachine/Runtime/Core/CameraUpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,18 @@ public static UpdateTracker.UpdateClock GetVcamUpdateStatus(CinemachineVirtualCa
return UpdateTracker.UpdateClock.Late;
return status.lastUpdateMode;
}

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
s_RoundRobinIndex = 0;
s_RoundRobinSubIndex = 0;
s_LastFixedUpdateContext = null;
s_LastUpdateTime = 0f;
s_FixedFrameCount = 0;
s_CurrentUpdateFilter = default;
}
#endif
}
}
9 changes: 9 additions & 0 deletions com.unity.cinemachine/Runtime/Core/CinemachineBlend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ public CinemachineBlendDefinition(Styles style, float time)
public AnimationCurve CustomCurve;

static AnimationCurve[] s_StandardCurves;

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod]
private static void ResetStaticsOnLoad()
{
s_StandardCurves = null;
}
#endif

void CreateStandardCurves()
{
s_StandardCurves = new AnimationCurve[(int)Styles.Custom];
Expand Down
Loading