I am trying to write a script very similar to how the terrain editor works. but instead of editing the terrain it is marking spots in 3d space on the terrain and the script will eventually create a curve along the path of the points. My trouble is coming from getting the script to run smoothly in edit mode since it doesn’t seem to have an “update” function or anything that gets called nearly as often as it so I was wondering if anyone knows how the terrain editor works.
Here is one example of my script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class VertexScript : MonoBehaviour {
public GameObject Marker;
public Vector3 markerPosition;
private List<Vector3> _vertexPoints = new List<Vector3>();
private List<GameObject> _markerPoints = new List<GameObject>();
private GameObject _placementMarker;
private bool CollectVectorPositions = true;
RaycastHit hit;
Ray ray;
void OnRenderObject() {
//If CollectVectorPositions is enabled then this function runs
//and the user is able to select positions in 3d space using
//mouse pointer in edit mode. Positions are added to a list to later
//create a curve or spline.
if(CollectVectorPositions) {
CollectVectors();
}
}
void CollectVectors () {
if (Input.GetMouseButton(1))
{
ray = Camera.current.ScreenPointToRay(Input.mousePosition);
markerPosition = hit.point;
Debug.Log("This code block exectued in Collect Vectors!");
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Terrain")
{
Debug.Log("Collect Vectors in StreetBuilder Script " + hit.point);
}
}
}
}
//This gets called when the button "Begin Point Collection" is pressed on custom Inspector
//Basically this just allows the process of collecting points to begin.
public void BeginVertexCollection() {
CollectVectorPositions = true;
}
// returns private bool if we are collecting vector positions
public bool Collecting(){
return CollectVectorPositions;
}
}
I also have tried setting up the collection process from the editor version of this script like:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(VertexScript))]
public class VertexScriptEditor : Editor {
public override void OnInspectorGUI() {
DrawDefaultInspector();
VertexScript myScript = (VertexScript)target;
if(GUILayout.Button("Begin Point Collection")) {
if (!myScript.Collecting())
{
myScript.BeginVertexCollection();
}
else {
Debug.Log("!Error: Currently Collecting Vertex Points!");
}
}
}
public void OnSceneGUI() {
VertexScript myScript = (VertexScript)target;
//Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Debug.DrawRay(ray.origin, ray.direction, Color.green, 5f);
//myScript.markerPosition = hit.point;
//Debug.Log("This code block exectued in OnSceneGUI!");
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Terrain")
{
Debug.Log("OnSceneGUI " + hit.point);
myScript.markerPosition = hit.point;
}
}
}
}
So basically I’ve attempted to set this up from both OnRenderObject as well as OnSceneGUI and neither are working properly or even collecting the point in scene view where the ray cast has intersected with the terrain. I have gotten some very odd and short debug ray draws to appear but they are scattered and seemingly pretty random. just not what I am expecting. The marker position was for an aura type effect again similar to the terrain editor to show up at the end of the ray where it hits the terrain. the ray is intended to be projected from mouse over scene view onto the terrain.