73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using JetBrains.Annotations;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit
|
|
{
|
|
public class LocationAdditiveElement : MonoBehaviour
|
|
{
|
|
[SerializeField] private bool useLateUpdate;
|
|
[SerializeField] private LocationAdditive locationAdditive;
|
|
[SerializeField] private Vector3 positionWeight = Vector3.one;
|
|
|
|
[SerializeReference,SubclassSelector] private IReference autoReference;
|
|
[SerializeReference,SubclassSelector] private IReference nameReference;
|
|
//[SerializeField] private Vector3 rotationAdditive = Vector3.one;
|
|
private Transform Transform;
|
|
|
|
private string nameCache;
|
|
private void Start()
|
|
{
|
|
nameCache = nameReference is not null?nameReference.Value:gameObject.name;
|
|
Transform = transform;
|
|
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}");
|
|
}
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (useLateUpdate) return;
|
|
Tick();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (!useLateUpdate) return;
|
|
Tick();
|
|
}
|
|
|
|
private void Tick()
|
|
{
|
|
if (!locationAdditive) return;
|
|
var localPosition = Transform.localPosition;
|
|
|
|
locationAdditive.AddPosition(Vector3.Scale(localPosition, positionWeight), nameReference?.Value);
|
|
//locationAdditive.AddRotation(Transform.localRotation * Quaternion.Euler(rotationAdditive));
|
|
locationAdditive.AddRotation(Transform.localRotation, nameCache);
|
|
}
|
|
}
|
|
}
|
|
|