-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathruntimeScript.cs
212 lines (148 loc) · 6.56 KB
/
runtimeScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* EasyRoads3D Runtime API example
*
* Important:
*
* It has occured that the terrain was not restored leaving the road shape in the terrain
* after exiting Play Mode. This happened after putting focus on the Scene View
* window while in Play Mode! In another occasion this consistently happened until the
* Scene View window got focus!
*
* Please check the above using a test terrain and / or backup your terrains before using this code!
*
* You can backup your terrain by simply duplicating the terrain object
* in the project folder
*
* Also, check the OnDestroy() code, without this code the shape of the generated roads
* will certainly remain in the terrain object after you exit Play Mode!
*
*
*
*
* */
using UnityEngine;
using System.Collections;
using EasyRoads3Dv3;
public class runtimeScript : MonoBehaviour {
public ERRoadNetwork roadNetwork;
public ERRoad road;
public GameObject go;
public int currentElement = 0;
public float distance = 0;
public float speed = 5f;
void Start () {
Debug.Log("Please read the comments at the top of the runtime script (/Assets/EasyRoads3D/Scripts/runtimeScript) before using the runtime API!");
// Create Road Network object
roadNetwork = new ERRoadNetwork();
// Create road
// ERRoad road = roadNetwork.CreateRoad(string name);
// ERRoad road = roadNetwork.CreateRoad(string name, Vector3[] markers);
// ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType);
// ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType, Vector3[] markers);
// get exisiting road types
// ERRoadType[] roadTypes = roadNetwork.GetRoadTypes();
// ERRoadType roadType = roadNetwork.GetRoadTypeByName(string name);
// create a new road type
ERRoadType roadType = new ERRoadType();
roadType.roadWidth = 6;
roadType.roadMaterial = Resources.Load("Materials/roads/road material") as Material;
// optional
roadType.layer = 1;
roadType.tag = "Untagged";
// roadType.hasMeshCollider = false; // default is true
// roadType = roadNetwork.GetRoadTypeByName("Train Rail");
// Debug.Log(roadType.roadMaterial);
// create a new road
Vector3[] markers = new Vector3[4];
markers[0] = new Vector3(200, 5, 200);
markers[1] = new Vector3(250, 5, 200);
markers[2] = new Vector3(250, 5, 250);
markers[3] = new Vector3(300, 5, 250);
road = roadNetwork.CreateRoad("road 1", roadType, markers);
// road.SetResolution(float value):void;
// Add Marker: ERRoad.AddMarker(Vector3);
road.AddMarker(new Vector3(300, 5, 300));
// Add Marker: ERRoad.InsertMarker(Vector3);
road.InsertMarker(new Vector3(275, 5, 235));
// road.InsertMarkerAt(Vector3 pos, int index): void;
// Delete Marker: ERRoad.DeleteMarker(int index);
road.DeleteMarker(2);
// Set the road width : ERRoad.SetWidth(float width);
// road.SetWidth(10);
// Set the road material : ERRoad.SetMaterial(Material path);
// Material mat = Resources.Load("Materials/roads/single lane") as Material;
// road.SetMaterial(mat);
// add / remove a meshCollider component
// road.SetMeshCollider(bool value):void;
// set the position of a marker
// road.SetMarkerPosition(int index, Vector3 position):void;
// road.SetMarkerPositions(Vector3[] position):void;
// road.SetMarkerPositions(Vector3[] position, int index):void;
// get the position of a marker
// road.GetMarkerPosition(int index):Vector3;
// get the position of a marker
// road.GetMarkerPositions():Vector3[];
// Set the layer
// road.SetLayer(int value):void;
// Set the tag
// road.SetTag(string value):void;
// set marker control type
// road.SetMarkerControlType(int marker, ERMarkerControlType type) : bool; // Spline, StraightXZ, StraightXZY, Circular
// find a road
// public static function ERRoadNetwork.GetRoadByName(string name) : ERRoad;
// get all roads
// public static function ERRoadNetwork.GetRoads() : ERRoad[];
// snap vertices to the terrain (no terrain deformation)
// road.SnapToTerrain(true);
// Build Road Network
roadNetwork.BuildRoadNetwork();
// remove script components
// roadNetwork.Finalize();
// Restore Road Network
// roadNetwork.RestoreRoadNetwork();
// Show / Hide the white surfaces surrounding roads
// public function roadNetwork.HideWhiteSurfaces(bool value) : void;
// road.GetConnectionAtStart(): GameObject;
// road.GetConnectionAtStart(out int connection): GameObject; // connections: 0 = bottom, 1= tip, 2 = left, 3 = right (the same for T crossings)
// road.GetConnectionAtEnd(): GameObject;
// road.GetConnectionAtEnd(out int connection): GameObject; // connections: 0 = bottom, 1= tip, 2 = left, 3 = right (the same for T crossings)
// Snap the road vertices to the terrain following the terrain shape (no terrain deformation)
// road.SnapToTerrain(bool value): void;
// road.SnapToTerrain(bool value, float yOffset): void;
// get the road length
// road.GetLength() : float;
// create dummy object
go = GameObject.CreatePrimitive(PrimitiveType.Cube);
}
void Update () {
if(roadNetwork != null){
float deltaT = Time.deltaTime;
float rSpeed = (deltaT * speed);
distance += rSpeed;
// pass the current distance to get the position on the road
// Debug.Log(road);
Vector3 v = road.GetPosition(distance, ref currentElement);
v.y += 1;
go.transform.position = v;
}
// spline point info center of the road
// public function ERRoad.GetSplinePointsCenter() : Vector3[];
// spline point info center of the road
// public function ERRoad.GetSplinePointsRightSide() : Vector3[];
// spline point info center of the road
// public function ERRoad.GetSplinePointsLeftSide() : Vector3[];
// Get the selected road in the Unity Editor
// public static function EREditor.GetSelectedRoad() : ERRoad;
}
void OnDestroy(){
// Restore road networks that are in Build Mode
// This is very important otherwise the shape of roads will still be visible inside the terrain!
if(roadNetwork != null){
if(roadNetwork.isInBuildMode){
roadNetwork.RestoreRoadNetwork();
Debug.Log("Restore Road Network");
}
}
}
}