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 · Feb 23, 2014 at 09:25 AM · sceneassetcallbackrename

How to keep a persistent reference to an asset? (Is there an OnAssetRename callback to update the asset's name if it changes?)

Hello,

I have a SceneTransition MonoBehaviour (which is essentially a trigger) that lets you select a scene from your project, when the player interacts with it, it just loads the scene you selected.

 public class SceneTransition : BaseTrigger, Interaction
 {
     public string scene;

     public void Interact(GameObject actor)
     {
         Interact();
     }
     public void Interact()
     {
         Application.LoadLevel(scene);
     }
     void Update()
     {
         if (isPlayerInSight && Input.GetKeyDown(Keys.action)) {
             Interact(lastCollidingObject);
         }
     }
 }

(The green ones are scenes included in the build, the reds are not)

alt text

The problem with this is that the scene is stored as a string, once you select a scene, the name is set (fixed) - now what happens if you renamed that scene and then tried to load it? you guessed it, it's gonna try and load a scene that doesn't exist and then I'd have to go in and manually reselect the scene to update the string.

So what I tried is, instead of using a string, I used a direct UE.Object reference to the scene file, but even with that, it seems that if I rename the scene file, the link (reference) gets broken! (as if the asset gets a new location in memory)

So that's why I'm looking for a way, a callback that notifies me if there's a scene rename (asset rename in general, I could filter the results) and then get all the SceneTransitions in the current scene, and see if anyone of them was referencing the renamed scene, if so then update the old name, with the new one.

(But even if we get to that point, what about the SceneTransitions that are in the other, unloaded scenes?... :/)

Note that I know about ProjectWindowUtil.StartNameEditingIfProjectWindowExists and the EndNameEditAction but I'm not sure if these could help here...

Any ideas?

Thanks!

macrowindow.png (46.5 kB)
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

1 Reply

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

Answer by Jamora · Feb 23, 2014 at 11:09 AM

You should use the GUID of the asset instead of its name, for the reasons you explained.

  • To get the GUID you need to know the location of the asset: GetAssetOrSenePath or manually iterate the directory tree for scenes

  • Then get the asset GUID: AssetPathToGUID

  • Store the GUID

  • Whenever you need to access that object: LoadAssetAtPath(GUIDToAssetPath(GUID))

You might run into some problems if the asset is moved, because the GUID seems to operate on the path.

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 · Feb 23, 2014 at 12:20 PM 0
Share

Thank you! Worked like charm!

I subscribed to EditorApplication.update with this method:

 private void UpdateSceneIfThereIsChange()
 {
     if (sp_scene == null || sp_guid == null || System.String.IsNullOrEmpty(sp_guid.stringValue)) return;
     string currentPath = AssetDatabase.GUIDToAssetPath(sp_guid.stringValue);
     string currentSceneName = Utils.GetAssetName(currentPath).RemoveExtension();
     if (sp_scene.stringValue != currentSceneName) {
         Debug.Log("there's been a rename, old name: " + sp_scene.stringValue + " new name: " + currentSceneName);
         sp_scene.stringValue = currentSceneName;
         sp_scene.serializedObject.Apply$$anonymous$$odifiedProperties();
     }
 }

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

20 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

Related Questions

why cant i drag my assests into the scene 2 Answers

Extracting scene data from built executable 0 Answers

Managing Scenes and reusable code 1 Answer

Asset Server Team and project management Unity Pro 2 Answers

How to add a character to a scene I've downloaded on Asset Store? 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