Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
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
3

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 rena$$anonymous$$g though?

avatar image
1

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

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

Detecting moment when GameObiect is created in hierarchy. 1 Answer

Assign mouse events for gameobject/collider at runtime 1 Answer

Rename gameobject (game not running, via inspector script) 2 Answers

Scoring System for Pinball 0 Answers


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