iFactory.Cutting.Unity/Assets/BITKit/Unity/Scripts/Location/LocationAdditiveElement.cs

70 lines
1.8 KiB
C#
Raw Normal View History

2024-01-23 02:56:26 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-03-04 18:45:21 +08:00
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using UnityEditor;
2024-01-23 02:56:26 +08:00
using UnityEngine;
namespace BITKit
{
public class LocationAdditiveElement : MonoBehaviour
{
[SerializeField] private bool useLateUpdate;
[SerializeField] private LocationAdditive locationAdditive;
[SerializeField] private Vector3 positionWeight = Vector3.one;
2024-03-04 18:45:21 +08:00
[SerializeReference,SubclassSelector] private IReference autoReference;
//[SerializeField] private Vector3 rotationAdditive = Vector3.one;
2024-01-23 02:56:26 +08:00
private Transform Transform;
private void Start()
{
Transform = transform;
2024-03-04 18:45:21 +08:00
var components =
GetComponentsInParent<LocationAdditive>(true)
.SelectMany(x => x.GetComponentsInChildren<LocationAdditive>(true)).Distinct().ToArray();
try
{
if (!locationAdditive)
locationAdditive =components
.First(x => x.autoReference is not null && x.autoReference.Value.Equals(autoReference.Value))!;
}
catch (Exception e)
{
StringBuilder reportBuilder = new();
foreach (var x in components)
{
reportBuilder.AppendLine(x.autoReference is not null ? $"{x.autoReference.Value}@{x.transform.name}" : x.name);
}
BIT4Log.Log<LocationAdditiveElement>($"找不到对应的LocationAdditive:{autoReference.Value} \n{reportBuilder}");
throw;
}
2024-01-23 02:56:26 +08:00
}
private void Update()
{
if (useLateUpdate) return;
Tick();
}
private void LateUpdate()
{
if (!useLateUpdate) return;
Tick();
}
2024-03-04 18:45:21 +08:00
2024-01-23 02:56:26 +08:00
private void Tick()
{
var localPosition = Transform.localPosition;
2024-03-04 18:45:21 +08:00
locationAdditive.AddPosition(Vector3.Scale(localPosition, positionWeight));
//locationAdditive.AddRotation(Transform.localRotation * Quaternion.Euler(rotationAdditive));
locationAdditive.AddRotation(Transform.localRotation);
2024-01-23 02:56:26 +08:00
}
}
}