Skip to content

Commit

Permalink
[Colliders] Reduce FindObjects (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
takahiro0327 authored Aug 28, 2024
1 parent 498841b commit 26f7eb8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
33 changes: 29 additions & 4 deletions src/Colliders.Core/Core.Colliders.Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,25 @@ public partial class ColliderController : CharaCustomFunctionController

private bool didSetStates;

private static HashSet<ColliderController> allControllers = new HashSet<ColliderController>();

private float BreastSize => ChaControl.chaFile.custom.body.shapeValueBody[(int)BodyShapeIdx.BustSize];
public bool BreastCollidersEnabled { get; set; }
public bool SkirtCollidersEnabled { get; set; }
public bool FloorColliderEnabled { get; set; }

protected override void Awake()
{
base.Awake();
allControllers.Add(this);
}

protected override void OnDestroy()
{
allControllers.Remove(this);
base.OnDestroy();
}

internal void Main()
{
//Add the floor collider
Expand Down Expand Up @@ -263,12 +277,13 @@ private void UpdateArmCollidersBreastDB(DynamicBone_Ver02 dynamicBone, List<Dyna
}
private void UpdateArmCollidersBreastDBAll()
{
var controllers = FindObjectsOfType<ColliderController>();
var dynamicBones = GetComponentsInChildren<DynamicBone_Ver02>(true);

for (var i = 0; i < controllers.Length; i++)
for (var j = 0; j < dynamicBones.Length; j++)
UpdateArmCollidersBreastDB(dynamicBones[j], controllers[i].ArmColliders);
foreach ( var controller in GetAllColliderControllers() )
{
for (var i = 0; i < dynamicBones.Length; i++)
UpdateArmCollidersBreastDB(dynamicBones[i], controller.ArmColliders);
}
}

/// <summary>
Expand Down Expand Up @@ -380,6 +395,16 @@ private void UpdateLegCollidersSkirtDB(DynamicBone dynamicBone)
}
}
#endif

/// <summary>
/// Get all active controllers
/// </summary>
public static IEnumerable<ColliderController> GetAllColliderControllers()
{
foreach (var controller in allControllers)
if (controller && controller.gameObject.activeInHierarchy)
yield return controller;
}
}
}
}
5 changes: 2 additions & 3 deletions src/Colliders.Core/Core.Colliders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ public class ColliderSceneController : SceneCustomFunctionController
protected override void OnSceneSave() { }
protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictionary<int, ObjectCtrlInfo> loadedItems)
{
var controllers = FindObjectsOfType<ColliderController>();
for (var i = 0; i < controllers.Length; i++)
controllers[i].ApplyColliders();
foreach( var controller in ColliderController.GetAllColliderControllers())
controller.ApplyColliders();
}
}
}
Expand Down

0 comments on commit 26f7eb8

Please sign in to comment.