73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
[ExecuteInEditMode]
|
|
public class SetRolls : MonoBehaviour
|
|
{
|
|
// 移除 numberOfObjects 字段
|
|
[Header("生成设置")]
|
|
public float angle;
|
|
public float radius;
|
|
public float XRotation;
|
|
|
|
// 添加参数变化检测
|
|
private float lastAngle;
|
|
private float lastRadius;
|
|
private float lastXRotation;
|
|
|
|
void Update()
|
|
{
|
|
// 检测参数变化或物体变换
|
|
if (ParametersChanged() || transform.hasChanged)
|
|
{
|
|
GenerateRing();
|
|
transform.hasChanged = false;
|
|
UpdateLastParameters();
|
|
}
|
|
}
|
|
|
|
bool ParametersChanged()
|
|
{
|
|
return !Mathf.Approximately(angle, lastAngle)
|
|
|| !Mathf.Approximately(radius, lastRadius)
|
|
|| !Mathf.Approximately(XRotation, lastXRotation);
|
|
}
|
|
|
|
void UpdateLastParameters()
|
|
{
|
|
lastAngle = angle;
|
|
lastRadius = radius;
|
|
lastXRotation = XRotation;
|
|
}
|
|
void GenerateRing()
|
|
{
|
|
int childCount = transform.childCount;
|
|
if (childCount == 0) return;
|
|
|
|
// 记录预制体修改(仅在编辑器模式)
|
|
#if UNITY_EDITOR
|
|
if (!Application.isPlaying)
|
|
{
|
|
UnityEditor.Undo.RecordObjects(transform.Cast<Transform>().ToArray(), "Adjust Roll Positions");
|
|
UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this);
|
|
}
|
|
#endif
|
|
|
|
float angleStep = angle / childCount;
|
|
|
|
for (int i = 0; i < childCount; i++) // 遍历所有子物体
|
|
{
|
|
Transform child = transform.GetChild(i);
|
|
float currentAngle = i * angleStep * Mathf.Deg2Rad;
|
|
Vector3 position = new Vector3(Mathf.Cos(currentAngle), 0, Mathf.Sin(currentAngle)) * radius;
|
|
|
|
child.localPosition = position;
|
|
child.localRotation = Quaternion.Euler(90, 0, currentAngle * Mathf.Rad2Deg + 90);
|
|
child.Rotate(XRotation, 0, 0, Space.Self);
|
|
child.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
}
|