Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/World/WorldHighlightService.cs
2025-06-24 23:49:13 +08:00

89 lines
2.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using BITKit.Mod;
using HighlightPlus;
using Microsoft.Extensions.Logging;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Net.Project.B.Interaction
{
public class WorldHighlightService:IDisposable
{
private readonly IWorldInteractionService _interactionService;
private readonly ILogger<WorldHighlightService> _logger;
private HighlightProfile _defaultProfile;
private readonly ConcurrentDictionary<IWorldInteractable, HighlightEffect> _dictionary = new();
public WorldHighlightService(IWorldInteractionService interactionService, ILogger<WorldHighlightService> logger)
{
_interactionService = interactionService;
_logger = logger;
_interactionService.OnInteraction += OnInteraction;
_logger.LogInformation("场景互动高亮初始化中");
Initialize();
}
private async void Initialize()
{
_defaultProfile =await ModService.LoadAsset<HighlightProfile>("highlight_interactable");
_logger.LogInformation("场景互动高亮已初始化完成");
}
private void OnInteraction(object arg1, IWorldInteractable arg2, WorldInteractionProcess arg3, object arg4)
{
if (_defaultProfile is null) return;
switch (arg3)
{
case WorldInteractionProcess.Performed:
{
if (_dictionary.TryGetValue(arg2, out var effect) is false) return;
effect.HitFX();
}
break;
case WorldInteractionProcess.Hover:
{
if(arg2.WorldObject is not GameObject gameObject || !gameObject)return;
if (_dictionary.ContainsKey(arg2)) return;
if(gameObject.GetComponentsInChildren<MeshRenderer>().Any() is false)return;
var effect = gameObject.AddComponent<HighlightEffect>();
effect.profile = _defaultProfile;
effect.ProfileReload();
effect.ignoreObjectVisibility = true;
effect.highlighted = true;
_dictionary.TryAdd(arg2, effect);
}
break;
default:
{
if (_dictionary.TryRemove(arg2, out var effect2) is false) return;
if (effect2)
Object.Destroy(effect2);
break;
}
}
}
public void Dispose()
{
_interactionService.OnInteraction -= OnInteraction;
}
}
}