The way it works is you pick a waypoint, click it’s toggle button in the inspector, then you can click other waypoints in the scene view to automatically link them (which involves storing its transform in a variable based on direction).
It works fine but if you save & reload the scene all the variables that were set are empty. Is there something I should be doing with OnSceneGUI() to keep them?
Update to the above editor script. I’ve used the serializedobject functions as shown in the docs & it’s still not working (pressing play or reloading the scene loses the changes made). Also tried using EditorSceneManager.MarkSceneDirty & same thing. Any ideas?
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditor.SceneManagement;
[CustomEditor(typeof(Waypoint))]
[CanEditMultipleObjects]
public class WaypointEditor : Editor
{
SerializedProperty wpForward;
SerializedProperty wpBack;
SerializedProperty wpRight;
SerializedProperty wpLeft;
SerializedProperty wpUp;
SerializedProperty wpDown;
SerializedProperty linkedTP;
private bool toggleButton = false;
void OnEnable()
{
wpForward = serializedObject.FindProperty ("wpForward");
wpBack = serializedObject.FindProperty ("wpBack");
wpRight = serializedObject.FindProperty ("wpRight");
wpLeft = serializedObject.FindProperty ("wpLeft");
wpUp = serializedObject.FindProperty ("wpUp");
wpDown = serializedObject.FindProperty ("wpDown");
linkedTP = serializedObject.FindProperty ("linkedTP");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(wpForward);
EditorGUILayout.PropertyField(wpBack);
EditorGUILayout.PropertyField(wpRight);
EditorGUILayout.PropertyField(wpLeft);
EditorGUILayout.PropertyField(wpUp);
EditorGUILayout.PropertyField(wpDown);
EditorGUILayout.PropertyField(linkedTP);
if (GUILayout.Toggle(toggleButton, "Pick WPs", "Button") != toggleButton)
{
toggleButton = !toggleButton;
}
serializedObject.ApplyModifiedProperties();
}
public void OnSceneGUI()
{
if (toggleButton)
{
Event e = Event.current;
var t = (target as Waypoint);
if (e.type == EventType.MouseDown && e.button == 0)
{
RaycastHit hit;
Ray ray = HandleUtility.GUIPointToWorldRay (e.mousePosition);
if (Physics.Raycast (ray, out hit, 10000))
{
if (hit.transform.tag == "Waypoint" || hit.transform.tag == "Teleport")
{
Vector3 dirToHit = Vector3.Normalize(hit.transform.position - t.transform.position);
if (dirToHit == Vector3.forward)
t.wpForward = hit.transform;
else if (dirToHit == Vector3.back)
t.wpBack = hit.transform;
else if (dirToHit == Vector3.right)
t.wpRight = hit.transform;
else if (dirToHit == Vector3.left)
t.wpLeft = hit.transform;
else if (dirToHit == Vector3.up)
t.wpUp = hit.transform;
else if (dirToHit == Vector3.down)
t.wpDown = hit.transform;
}
}
}
HandleUtility.AddDefaultControl (GUIUtility.GetControlID (FocusType.Passive));
e.Use ();
}
}
}