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 JimmyCushnie · Jan 20, 2018 at 04:09 AM · fileextension

Can a unity game be set to open files of a given extension?

I'm using a custom file extension for my game's saves and it would be nice if you could jump directly to a save by double clicking the file. Is such a thing possible?

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
5
Best Answer

Answer by Lysander · Jan 20, 2018 at 08:28 AM

Absolutely. Create a custom editor for the DefaultAsset type exactly the same way you make one for any other class- this overrides the inspector that's shown for anything that isn't handled explicitly by Unity already. By override OnInspectorGUI, you can write any editor code you like to determine the kind of object being inspected and display an appropriate inspector for that object type.

If you don't want to make a custom editor, but rather react to the file being double clicked in the project files in the editor, then just put the OnOpenAsset attribute on any static method with the signature "static bool OnOpenAsset(int instanceID, int line)" (the function name doesn't have to match, just the parameter types and return type). Within that method, you then have to determine if the object being opened is something you want to handle, which will probably involve turning the instance ID into the object itself with EditorUtility.InstanceIDToObject(), then AssetDatabase.GetAssetPath() on that object to get its path on the disk, and finally Path.GetExtension() with that path to gets its extension. If the extension matches what you want to handle, do what you need and return true to say "it's been handled", and if not then just return false.

Let me know if you need any additional information. I think I have a class laying around that lets you make custom editors for extensions explicitly and bypassing the need to add a bunch of additional clauses to the DefaultAsset inspector each time you want to be able to inspect additional asset types- it might be overkill for your needs since it uses custom attributes and finds all types in the loaded assemblies with that attribute, adds them to an internal collection, and sort of defers assets to those if they register to handle the extension(s). If you want it, just say the word and I'll go rip it out of whatever library I put it in.

UPDATE: It occurs to me I may have misunderstood your intent. If you're talking about a game build being able to open files in Windows that are double-clicked, I believe you can manage this with a combination of "file associations" (a simple registry key saying "open this file type with my application") and CommandLineArguments. You can find links and more information here.

However, this only works if you want to START your Unity game by double clicking the asset. If you're looking to pass an argument into an already running instance of your game, there's no real support for that. But, you can possibly cheat and pull it off (sort of) by allowing multiple instances of your game in the PlayerSettings, but having a startup script that immediately checks if another instance is running, and quits if one is. You can use the CommandLineArguments, applied to your file associations, to silently open a second instance of Unity in the background in this manner, change a PlayerPrefs value or assign a value to a local save file of some sort, and then quit. Your primary instance would have a function constantly checking that saved value to see if it's changed, then act appropriately when it is (check in OnApplicationFocus if you like, to cut down processing there). In this way, you may be able to pass a message to an open Unity game with the filepath of a save file.

Long story short- it's not trivial, and I'm not 100% sure it would work.

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 JimmyCushnie · Feb 02, 2018 at 05:15 AM 0
Share

Wow, thank you so much for all the information. It is indeed the latter thing I am trying to get to work, I shall report back here with more information when I get around to this feature.

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

74 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the file extension for PC PlayerPrefs? 1 Answer

How to correctly use polygons in DXF files 0 Answers

Showing file extension on Project tab 2 Answers

.unity3d extension becomes Default Asset 1 Answer

OBB fails to load in 2017.2/2017.3, same project build with Unity 5.5.2f1 works fine 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