• 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
2
Question by vexe · Dec 28, 2013 at 07:46 AM · gameobjecteventevent-handlingrename

Fire an event when gameObject has been renamed?

I was wondering if I could have a MonoBehaviour listen to an event, that would get fired when its gameObject gets renamed.

First, I have to detect that 'something' is being renamed. And then get the selected gameObject from Selection - to get to the GO that's being renamed, and maybe then send a msg to its children (or raise an event) - where my MB would be sitting there listening for that signal.

I was just checking out Event.current.commandName, like so (just like in the doc's example):

 void OnGUI()
 {
     Event e = Event.current;
     if (e.commandName != "")
         print(e.commandName);
 }

But, it seems that the only thing it's catching is "UndoRedoPerformed" - If I make a Copy, Paste, Duplicate, etc. It's not detecting those.

Any ideas how to catch that Rename?

PS: I only care about this in the editor, not at runtime.

Thanks.

Comment
Add comment · Show 1
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 Benproductions1 · Dec 28, 2013 at 08:35 AM 0
Share

Sorry to tell you, but you can't create a event for a setter without changing the Source Code of Unity. You could create your own setter/getter with a custom Editor and then you can do the event thing.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Jamora · Dec 28, 2013 at 02:45 PM

Unity has a custom callback for changes (renaming, deleting, creating etc.) in the hierarchy window: EditorApplication-hierarchyWindowChanged.

You could have an editor window register for these callbacks and then perform the required actions. You might also consider having a static class [InitializeOnLoad], then register for the callback if such functionality is needed constantly.

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 vexe · Dec 28, 2013 at 02:54 PM 0
Share

Thanks. Good find +1 That's pretty cool but, how to know the type of "change" (rename, delete, etc)? - Couldn't find anything in the docs. For deleting/creating, maybe there would be some window that lives in the editor, that counts the number of objects in the scene, and compares with a prevNumObjs - it's brutal, but works. How about renaming though?

avatar image
0

Answer by Amitloaf · Sep 18, 2018 at 07:40 AM

This is possible although a bit awkward. If you make a class inherit from AssetPostprocessor, when you rename an object, OnPostprocessAllAssets will be called twice. The first time will have the new name of the file (or files in case you changed a folder) in movedAssets and the old name in movedFromAssetPaths. This also happens on move, not just on rename but with quite a simple path check you should be able to discern the two. The second time will have the new object in importedAssets (again, this will happen for every imported object so it's less useful for you). Hope that helps!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make events in my unity game? i.e a x2 exp per event? 0 Answers

Scoring System for Pinball 0 Answers

Unity UI 4.6 - Programmatically adding events - EventTrigger.delegates is null 0 Answers

UI: Canvas size changed Event? 1 Answer

How do I have an event be triggered on an audio clip finishing playing? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges