1er script pour créer path:
Code : Tout sélectionner
using System.Collections.Generic;
using UnityEngine;
namespace PathCreation {
public class PathCreator : MonoBehaviour {
/// This class stores data for the path editor, and provides accessors to get the current vertex and bezier path.
/// Attach to a GameObject to create a new path editor.
public event System.Action pathUpdated;
[SerializeField, HideInInspector]
PathCreatorData editorData;
[SerializeField, HideInInspector]
bool initialized;
GlobalDisplaySettings globalEditorDisplaySettings;
// Vertex path created from the current bezier path
public VertexPath path {
get {
if (!initialized) {
InitializeEditorData (false);
}
return editorData.GetVertexPath(transform);
}
}
// The bezier path created in the editor
public BezierPath bezierPath {
get {
if (!initialized) {
InitializeEditorData (false);
}
return editorData.bezierPath;
}
set {
if (!initialized) {
InitializeEditorData (false);
}
editorData.bezierPath = value;
}
}
#region Internal methods
/// Used by the path editor to initialise some data
public void InitializeEditorData (bool in2DMode) {
if (editorData == null) {
editorData = new PathCreatorData ();
}
editorData.bezierOrVertexPathModified -= TriggerPathUpdate;
editorData.bezierOrVertexPathModified += TriggerPathUpdate;
editorData.Initialize (in2DMode);
initialized = true;
}
public PathCreatorData EditorData {
get {
return editorData;
}
}
public void TriggerPathUpdate () {
if (pathUpdated != null) {
pathUpdated ();
}
}
#if UNITY_EDITOR
// Draw the path when path objected is not selected (if enabled in settings)
void OnDrawGizmos () {
// Only draw path gizmo if the path object is not selected
// (editor script is resposible for drawing when selected)
GameObject selectedObj = UnityEditor.Selection.activeGameObject;
if (selectedObj != gameObject) {
if (path != null) {
path.UpdateTransform (transform);
if (globalEditorDisplaySettings == null) {
globalEditorDisplaySettings = GlobalDisplaySettings.Load ();
}
if (globalEditorDisplaySettings.visibleWhenNotSelected) {
Gizmos.color = globalEditorDisplaySettings.bezierPath;
for (int i = 0; i < path.NumPoints; i++) {
int nextI = i + 1;
if (nextI >= path.NumPoints) {
if (path.isClosedLoop) {
nextI %= path.NumPoints;
} else {
break;
}
}
Gizmos.DrawLine (path.GetPoint (i), path.GetPoint (nextI));
}
}
}
}
}
#endif
#endregion
}
}
2ème script pour placer objet à intervalle de distance régulier sur le path:
Code : Tout sélectionner
using PathCreation;
using UnityEngine;
namespace PathCreation.Examples {
[ExecuteInEditMode]
public class PathPlacer : PathSceneTool {
public GameObject prefab;
public GameObject holder;
public float spacing = 3;
const float minSpacing = .1f;
void Generate () {
if (pathCreator != null && prefab != null && holder != null) {
DestroyObjects ();
VertexPath path = pathCreator.path;
spacing = Mathf.Max(minSpacing, spacing);
float dst = 0;
while (dst < path.length) {
Vector3 point = path.GetPointAtDistance (dst);
Quaternion rot = path.GetRotationAtDistance (dst);
Instantiate (prefab, point, rot, holder.transform);
dst += spacing;
}
}
}
void DestroyObjects () {
int numChildren = holder.transform.childCount;
for (int i = numChildren - 1; i >= 0; i--) {
DestroyImmediate (holder.transform.GetChild (i).gameObject, false);
}
}
protected override void PathUpdated () {
if (pathCreator != null) {
Generate ();
}
}
}
}
Dans ce script,la classe herite de la classe PathSceneTool.
Voici le scipt PathSceneTool:
Code : Tout sélectionner
using UnityEngine;
namespace PathCreation.Examples
{
[ExecuteInEditMode]
public abstract class PathSceneTool : MonoBehaviour
{
public event System.Action onDestroyed;
public PathCreator pathCreator;
public bool autoUpdate = true;
protected VertexPath path {
get {
return pathCreator.path;
}
}
public void TriggerUpdate() {
PathUpdated();
}
protected virtual void OnDestroy() {
if (onDestroyed != null) {
onDestroyed();
}
}
protected abstract void PathUpdated();
}
}
PathPlacer modifié(PathPlacer_delay.cs)de façon à instancier avec un retard:
Code : Tout sélectionner
using PathCreation;
using UnityEngine;
namespace PathCreation.Examples {
[ExecuteInEditMode]
public class PathPlacer_delay : PathSceneTool {
public GameObject prefab;
public GameObject holder;
public float spacing = 3;
public float retard,delay;
float dst = 0;
const float minSpacing = .1f;
void Start(){
InvokeRepeating ("Generate", retard, delay);}
void Generate () {
if (pathCreator != null && prefab != null){ //&& holder != null)
//DestroyObjects ();
VertexPath path = pathCreator.path;
spacing = Mathf.Max(minSpacing, spacing);
//while (dst < path.length) {
if (dst < path.length) {
Vector3 point = path.GetPointAtDistance (dst);
Quaternion rot = path.GetRotationAtDistance (dst);
Instantiate (prefab, point, rot,holder.transform);
dst += spacing;}
}
}
/*void DestroyObjects () {
int numChildren = holder.transform.childCount;
for (int i = numChildren - 1; i >= 0; i--) {
DestroyImmediate (holder.transform.GetChild (i).gameObject, false);
}
}*/
protected override void PathUpdated () {
if (pathCreator != null) {
Generate ();
}
}
}
}
Instructions:
Dans tous les cas,le script PathSceneTool est à conserver dans l'Asset du projet
Dans le hirarchy,ajouter:
-un prefab
-un holder(pourquoi pas choisir un empty)
-un path crée avec le script PthCreator
Dans l'Inspector:
-mettre tous les scripts
1)pour spawner following path sans retard,utiliser les scripts PathCreator,,PathPlacer
2)pour spawner following path avec un retard à chaque spawn,utiliser les scripts PathCreator,PathPlacer_delay
Pour 2),le résultat fonctionne maos n'est pas très propre.