- Home /
Adding an number to an new object's name automatically via editor scripting
Long title, but whatever :S
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.e: "WaypointSetA" + 1 = WaypointSetA1 "WaypointSetA" + 2 = WaypointSetA2
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?
Answer by Joshua · Aug 22, 2011 at 06:41 AM
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[i] = wayPointTransformArray[i].position;
Handles.DrawPolyLine( wayPointPositionArray ); //draw a line between each point
}
}
Answer by DaveA · Aug 22, 2011 at 06:00 AM
You'll probably be writing your own duplicate using http://unity3d.com/support/documentation/ScriptReference/EditorUtility.CloneComponent.html
Get the selected object http://unity3d.com/support/documentation/ScriptReference/Selection-activeGameObject.html
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 http://msdn.microsoft.com/en-us/library/system.string.substring.aspx
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.
Answer by shieldgenerator7 · Dec 23, 2020 at 10:40 AM
I'm not sure if this answers your question, but it answers mine:
To auto increment a GameObject's name so that it is unique among other GameObjects, just use Objects.GetUniqueName()
: https://docs.unity3d.com/ScriptReference/ObjectNames.GetUniqueName.html
.
Here's an example:
string[] knownNames;
GameObject go;
go.name = ObjectNames.GetUniqueName(knownNames, go.name);