My friend and I have created a small waypoint that reads the name + the number of the waypoint and adds it accordingly to an array. You have to place it according on the map and the object will move accordingly on the path.
I like this setup, but it gets irritating to have to create/duplicate an object and change the number, so I was wondering if I could pull the route that some programs do and automatically add/increment a number to that object.
i.e There is a WaypointSetA1. I duplicate it and it names it WaypointSetA2 and so on.
I haven’t ever written an Editor script before, but this seems pretty much feasible. Could I get assistance in writing this script, or direct me to an already existing solution?
What you want to do is write a simple custom inspector. You’ll then give it a button with which you add a new waypoint. First the normal script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WaypointContainer : MonoBehaviour
{
public List<Transform> waypoints;
}
This is nothing but a script with a single public list, containing the waypoints. Attach this to a gameobject in your scene which will contain a set of waypoints. Now create a script named WaypointContainerEditor and place it in a folder named Editor. This script will not be included in your build, it will only tell unity what to show when you have a gameobject selected that has the script posted above attached to it. The script bellow is a very simple example, which could be expanded with a lot of things.
using UnityEngine;
using UnityEditor; //always remember to add this
using System.Collections;
using System.Collections.Generic;
[CustomEditor( typeof( WaypointContainer ) )]
public class WaypointContainerEditor : Editor
{
List<Transform> waypoints; //the list
void OnEnable()
{
waypoints = (target as WaypointContainer ).waypoints; //reference the list of the instance we are editing
}
public override void OnInspectorGUI() //this is where the inspectorgui gets handled
{
if( GUILayout.Button( "Reset" ) )
{
foreach( Transform wayPoint in waypoints )
DestroyImmediate( wayPoint.gameObject, false );
waypoints.Clear();
}
if( GUILayout.Button( "Add waypoint" ) )
{
Transform newWaypoint = new GameObject( "Waypoint"+waypoints.Count.ToString() ).transform;
newWaypoint.parent = (target as WaypointContainer).transform;
newWaypoint.position = SceneView.lastActiveSceneView.pivot;
waypoints.Add( newWaypoint );
}
}
void OnSceneGUI() //and the scene gui
{
Transform[] wayPointTransformArray = waypoints.ToArray();
Vector3[] wayPointPositionArray = new Vector3[ wayPointTransformArray.Length ];
for( int i = 0; i<wayPointTransformArray.Length; i++ )
wayPointPositionArray _= wayPointTransformArray*.position;*_
* Handles.DrawPolyLine( wayPointPositionArray ); //draw a line between each point* * } * }
Then get its .name, parse it backward looking for digits until you hit a non-digit, then convert the number you dug out to an integer integer.Parse(string) Also extract the non-number part. Thus if you have WaypointSetA1, you get ‘1’ and ‘WaypointSetA’ Use Substring or similar String.Substring Method (System) | Microsoft Learn
Then just increment that number, append it to the base name, and set it on the .name of the new object.
LOL, I was doing good until the parsing part :D Could I get a bit more of a start from the coding side? From what get, i need to read the selected objects name (which the selected object will be the most recently duplicated object), check the number, and increment/place the number at the end.
LOL, I was doing good until the parsing part :D Could I get a bit more of a start from the coding side? From what get, i need to read the selected objects name (which the selected object will be the most recently duplicated object), check the number, and increment/place the number at the end.
– SrBilyon