Skip to content

Commit 91e5bcb

Browse files
Added gameobject target support for automatic disabling on freecam, and made the lookup gameobject more robust. Also improved error messages around it.
1 parent a08a299 commit 91e5bcb

File tree

1 file changed

+86
-17
lines changed

1 file changed

+86
-17
lines changed

src/UI/Panels/FreeCamPanel.cs

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,38 @@ public FreeCamPanel(UIBase owner) : base(owner)
8181
static bool disabledCinemachine;
8282
static bool disabledOrthographic;
8383
static List<string> stringComponentsToDisable = new();
84-
static List<Behaviour> componentsToDisable = new();
84+
85+
private class DisableTarget
86+
{
87+
public UnityEngine.Object Target { get; private set; }
88+
public bool IsGameObject { get; private set; }
89+
90+
public DisableTarget(Behaviour component)
91+
{
92+
Target = component;
93+
IsGameObject = false;
94+
}
95+
96+
public DisableTarget(GameObject gameObject)
97+
{
98+
Target = gameObject;
99+
IsGameObject = true;
100+
}
101+
102+
public void SetEnabled(bool enable)
103+
{
104+
if (IsGameObject)
105+
{
106+
((GameObject)Target).SetActive(enable);
107+
}
108+
else
109+
{
110+
((Behaviour)Target).enabled = enable;
111+
}
112+
}
113+
}
114+
115+
static List<DisableTarget> componentsToDisable = new();
85116

86117
public static bool supportedInput => InputManager.CurrentType == InputType.Legacy;
87118

@@ -600,7 +631,7 @@ protected override void ConstructPanelContent()
600631

601632
AddSpacer(5);
602633

603-
AddInputField("ComponentsToDisable", "Components To Disable:", "CinemachineBrain", out componentsToDisableInput, ComponentsToDisableInput_OnEndEdit, 175);
634+
AddInputField("ComponentsToDisable", "Disable Components/GameObjects:", "CinemachineBrain", out componentsToDisableInput, ComponentsToDisableInput_OnEndEdit, 250);
604635
componentsToDisableInput.Text = ConfigManager.Custom_Components_To_Disable.Value;
605636
stringComponentsToDisable = ConfigManager.Custom_Components_To_Disable.Value.Split(',').Select(c => c.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToList();
606637

@@ -897,9 +928,9 @@ void ComponentsToDisableInput_OnEndEdit(string input)
897928
stringComponentsToDisable = input.Split(',').Select(c => c.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToList();
898929
}
899930

900-
static List<Behaviour> GetComponentsToDisable()
931+
static List<DisableTarget> GetComponentsToDisable()
901932
{
902-
List<Behaviour> components = new();
933+
List<DisableTarget> components = new();
903934
if (stringComponentsToDisable == null || stringComponentsToDisable.Count == 0)
904935
{
905936
return components;
@@ -928,7 +959,41 @@ static List<Behaviour> GetComponentsToDisable()
928959

929960
if (!foundNextPathStep)
930961
{
931-
ExplorerCore.LogWarning($"Couldn't find {stringComponent} component to disable it.");
962+
var test = new GameObject("Test CUE object to get DontDestroyOnLoad scene");
963+
UnityEngine.Object.DontDestroyOnLoad(test);
964+
Scene scene = test.scene;
965+
GameObject.Destroy(test);
966+
967+
foreach (GameObject obj in scene.GetRootGameObjects()) {
968+
if (obj.name == pathStep)
969+
{
970+
foundNextPathStep = obj;
971+
break;
972+
}
973+
}
974+
}
975+
976+
if (!foundNextPathStep)
977+
{
978+
for (int j = 0; j < SceneManager.sceneCount; j++)
979+
{
980+
Scene scene = SceneManager.GetSceneAt(j);
981+
if (scene == SceneManager.GetActiveScene() || !scene.isLoaded) continue;
982+
983+
foreach (GameObject obj in scene.GetRootGameObjects()) {
984+
if (obj.name == pathStep)
985+
{
986+
foundNextPathStep = obj;
987+
break;
988+
}
989+
}
990+
if (foundNextPathStep) break;
991+
}
992+
}
993+
994+
if (!foundNextPathStep)
995+
{
996+
ExplorerCore.LogWarning($"Couldn't find root {foundNextPathStep} gameobject on {stringComponent} path to disable it.");
932997
break;
933998
}
934999
currentGameObject = foundNextPathStep;
@@ -938,31 +1003,35 @@ static List<Behaviour> GetComponentsToDisable()
9381003
if (pathStep == "..") {
9391004
if (!currentGameObject.transform.parent)
9401005
{
941-
ExplorerCore.LogWarning($"Couldn't find {stringComponent} component to disable it.");
1006+
ExplorerCore.LogWarning($"{currentGameObject.name} doesn't have a parent, so can't go up the parent hierarchy on {stringComponent}.");
9421007
break;
9431008
}
9441009

9451010
currentGameObject = currentGameObject.transform.parent.gameObject;
9461011
continue;
9471012
}
9481013

949-
// Last member of the path, should be a component
1014+
// Last member of the path, could be either a component or a GameObject
9501015
if (i == pathToComponent.Count - 1) {
9511016
Behaviour comp = GetComponentByName(currentGameObject, pathStep);
952-
if (!comp)
953-
{
954-
// Should we allow to disable entire GameObjects here if it can't find the right component?
955-
ExplorerCore.LogWarning($"Couldn't find {stringComponent} component to disable it.");
956-
break;
1017+
if (comp) {
1018+
components.Add(new DisableTarget(comp));
9571019
}
1020+
else {
1021+
Transform nextGameObjectTransform = currentGameObject.transform.Find(pathStep);
1022+
if (!nextGameObjectTransform) {
1023+
ExplorerCore.LogWarning($"Couldn't find {pathStep} component or gameobject on {stringComponent} path to disable it.");
1024+
break;
1025+
}
9581026

959-
components.Add(comp);
1027+
components.Add(new DisableTarget(nextGameObjectTransform.gameObject));
1028+
}
9601029
}
9611030
else {
9621031
Transform nextGameObjectTransform = currentGameObject.transform.Find(pathStep);
9631032
if (!nextGameObjectTransform)
9641033
{
965-
ExplorerCore.LogWarning($"Couldn't find {stringComponent} component to disable it.");
1034+
ExplorerCore.LogWarning($"Couldn't find {pathStep} gameobject on {stringComponent} path to disable it.");
9661035
break;
9671036
}
9681037

@@ -981,10 +1050,10 @@ static void ToggleCustomComponents(bool enable)
9811050
componentsToDisable = GetComponentsToDisable();
9821051
}
9831052

984-
foreach(Behaviour comp in componentsToDisable)
1053+
foreach(DisableTarget target in componentsToDisable)
9851054
{
986-
// We could outright delete the components if on Cloned freecam mode
987-
comp.enabled = enable;
1055+
// We could outright delete the components/gameobjects if on Cloned freecam mode
1056+
target.SetEnabled(enable);
9881057
}
9891058
}
9901059

0 commit comments

Comments
 (0)