110 lines
2.6 KiB
C#
110 lines
2.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace SplineMesh
|
|
{
|
|
[ExecuteInEditMode]
|
|
[SelectionBase]
|
|
[DisallowMultipleComponent]
|
|
public class PrefabPlacer : MonoBehaviour
|
|
{
|
|
private GameObject generated;
|
|
|
|
private Spline spline;
|
|
|
|
private bool toUpdate = true;
|
|
|
|
public GameObject prefab;
|
|
|
|
public float scale = 1f;
|
|
|
|
public float scaleRange;
|
|
|
|
public float spacing = 1f;
|
|
|
|
public float spacingRange;
|
|
|
|
public float offset;
|
|
|
|
public float offsetRange;
|
|
|
|
public bool isRandomYaw;
|
|
|
|
public int randomSeed;
|
|
|
|
private void OnEnable()
|
|
{
|
|
string n = "generated by " + GetType().Name;
|
|
Transform transform = base.transform.Find(n);
|
|
generated = ((transform != null) ? transform.gameObject : UOUtility.Create(n, base.gameObject));
|
|
spline = GetComponentInParent<Spline>();
|
|
spline.NodeListChanged += delegate
|
|
{
|
|
toUpdate = true;
|
|
foreach (CubicBezierCurve curf in spline.GetCurves())
|
|
{
|
|
curf.Changed.AddListener(delegate
|
|
{
|
|
toUpdate = true;
|
|
});
|
|
}
|
|
};
|
|
foreach (CubicBezierCurve curf2 in spline.GetCurves())
|
|
{
|
|
curf2.Changed.AddListener(delegate
|
|
{
|
|
toUpdate = true;
|
|
});
|
|
}
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
toUpdate = true;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (toUpdate)
|
|
{
|
|
Sow();
|
|
toUpdate = false;
|
|
}
|
|
}
|
|
|
|
public void Sow()
|
|
{
|
|
UOUtility.DestroyChildren(generated);
|
|
UnityEngine.Random.InitState(randomSeed);
|
|
if (spacing + spacingRange <= 0f || prefab == null)
|
|
{
|
|
return;
|
|
}
|
|
for (float num = 0f; num <= spline.Length; num += spacing + UnityEngine.Random.Range(0f, spacingRange))
|
|
{
|
|
CurveSample sampleAtDistance = spline.GetSampleAtDistance(num);
|
|
GameObject gameObject = UnityEngine.Object.Instantiate(prefab, generated.transform);
|
|
gameObject.transform.localRotation = Quaternion.identity;
|
|
gameObject.transform.localPosition = Vector3.zero;
|
|
gameObject.transform.localScale = Vector3.one;
|
|
gameObject.transform.localPosition = sampleAtDistance.location;
|
|
float num2 = scale + UnityEngine.Random.Range(0f, scaleRange);
|
|
gameObject.transform.localScale = new Vector3(num2, num2, num2);
|
|
if (isRandomYaw)
|
|
{
|
|
gameObject.transform.Rotate(0f, 0f, UnityEngine.Random.Range(-180, 180));
|
|
}
|
|
else
|
|
{
|
|
gameObject.transform.rotation = sampleAtDistance.Rotation;
|
|
}
|
|
Vector3 normalized = (Quaternion.LookRotation(sampleAtDistance.tangent, sampleAtDistance.up) * Vector3.right).normalized;
|
|
float num3 = offset + UnityEngine.Random.Range(0f, offsetRange * (float)Math.Sign(offset));
|
|
num3 *= sampleAtDistance.scale.x;
|
|
normalized *= num3;
|
|
gameObject.transform.position += normalized;
|
|
}
|
|
}
|
|
}
|
|
}
|