using System; using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using BITKit; using BITKit.Entities; using Cysharp.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using UnityEngine; namespace Net.Project.B.Cosmetics { public class UnityCosmeticsController : MonoBehaviour { [SerializeField] private Transform model; [SerializeField] private Animator animator; private IEntitiesService _entitiesService; private readonly List _activeModels = new(); private async void Start() { var cosmeticsCustomize = BITApp.ServiceProvider.GetRequiredService>(); _entitiesService = BITApp.ServiceProvider.GetRequiredService(); cosmeticsCustomize.OnValueChanged += OnValueChanged; OnValueChanged(null,cosmeticsCustomize.Value); await destroyCancellationToken.WaitUntilCanceled(); cosmeticsCustomize.OnValueChanged -= OnValueChanged; } private async void OnValueChanged(CosmeticsCustomizeComponent arg1, CosmeticsCustomizeComponent arg2) { var cosmetics = _entitiesService.QueryComponents() .ToArray().Where(x => arg2.ComponentIds.Contains(x.ScriptableId)).ToArray(); foreach (var x in _activeModels) { Destroy(x); } _activeModels.Clear(); foreach (var cosmetic in cosmetics) { switch (cosmetic.CosmeticsClass) { case CosmeticsModel: { model.gameObject.DestoryChilds(); var newModel = Instantiate(cosmetic.Model, model); newModel.transform.localPosition = Vector3.zero; newModel.transform.localRotation = Quaternion.identity; animator.Rebind(); animator.Update(1); } break; } } try { await UniTask.NextFrame(destroyCancellationToken); } catch (OperationCanceledException) { return; } animator.Rebind(); animator.Update(1); foreach (var cosmetic in cosmetics) { switch (cosmetic.CosmeticsClass) { case not CosmeticsModel: { var bone = animator.GetBoneTransform(cosmetic.Bone); var newModel = Instantiate(cosmetic.Model, bone); newModel.transform.localPosition = Vector3.zero; newModel.transform.localRotation = Quaternion.identity; _activeModels.Add(newModel.gameObject); } break; } } } } }