Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SrBilyon · Aug 22, 2011 at 05:42 AM · editorsystemwaypointincrement

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
     }        
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SrBilyon · Aug 22, 2011 at 06:27 AM 0
Share

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.

avatar image
0

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing local system ( File Browser ) 2 Answers

How to create a point/score system based on player performance. 2 Answers

Manually triggering a script from the editor (utility, macro etc.) 1 Answer

Editor script - any callback when target is first instantiated? 1 Answer

How to split animations via script in editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges