using System; using System.Collections.Generic; using UnityEngine; namespace X.Rendering.Scene { [ExecuteAlways] [DefaultExecutionOrder(200)] public class SceneEffectCharacter : MonoBehaviour { public uint capsuleAOSettingMask; [Serializable] class BoneCapsuleSetting { public string BoneName; public float Radius; public float Height; public int Direction; } [SerializeField] private BoneCapsuleSetting[] boneSettings = new BoneCapsuleSetting[] { new() { BoneName = "Forearm01".ToLowerInvariant(), // 前臂 Radius =0.05f, Height = 0.2f, Direction = 0 }, new() { BoneName ="UpperArm01".ToLowerInvariant(), // 上臂 Radius =0.05f, Height = 0.2f, Direction = 0 }, new() { BoneName ="Spine1".ToLowerInvariant(), // 脊柱 Radius =0.15f, Height = 0.7f, Direction = 0 }, new() { BoneName = "Calf01".ToLowerInvariant(), // 小腿 Radius =0.03f, Height = 0.6f, Direction = 0 }, new() { BoneName ="Thigh01".ToLowerInvariant(), // 大腿 Radius =0.1f, Height = 0.4f, Direction = 0 } }; [SerializeField] private List capsuleTransforms = new List(); private void Awake() { #if UNITY_EDITOR AddCapsule(); #endif if (capsuleTransforms?.Count == 0) { AddCapsule(); } } private void AddCapsule() { var nodes = this.GetComponentsInChildren(); foreach (var item in nodes) { if(item.childCount > 0) { continue; } var goName = item.name.ToLowerInvariant(); for (int i = 0; i < boneSettings.Length; i++) { var boneSetting = boneSettings[i]; if(goName.EndsWith(boneSetting.BoneName)) { if (!item.TryGetComponent(out var capsuleCollider)) { capsuleCollider = item.gameObject.AddComponent(); } capsuleCollider.enabled = false; capsuleCollider.direction = boneSetting.Direction; capsuleCollider.radius = boneSetting.Radius; capsuleCollider.height = boneSetting.Height; if(!capsuleTransforms.Contains(item)) { capsuleTransforms.Add(item); } } } } } #if UNITY_EDITOR private void OnValidate() { capsuleTransforms.Clear(); AddCapsule(); } #endif private void OnEnable() { SceneEffect.Instance?.AddSceneEffectCharacter(this); } private void OnDisable() { SceneEffect.Instance?.RmSceneEffectCharacter(this); } } }