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
1
Question by AmasterAmaster · Jun 25, 2015 at 11:22 AM · editorunityeditorfileaccessopen

How to access Editor Script Functions like a regular Script?

I had a previous script working while having "using UnityEditor;" on the top of the script, but it will not build becuase it is not in the "Editor Folder". I moved the script to that folder and Unity will not run the script from there. Then I tried making an Editor Script that only does Unity system functions, but the regular scripts cannot access that script. What can I do to access the functionality of the Editor script from regular scripts?

Here is what is in the Editor script:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;            //USE: open dialog.
 
 public class SystemOpperations : MonoBehaviour
 {
     public static string OpenFile(string title, string directory, string extention)
     {
         EditorUtility.OpenFilePanel(title, directory, extention);
     }
 }

Here is what is trying to access the function:

 //Some code...
 
 string filename = SystemOpperations.OpenFile("Open File", directory, extention);
 
 //Some code...

Right now, I am only getting an error saying "error CS0103: The name `SystemOpperations' does not exist in the current context", even though I see it in the Editor folder. All I want is to get the file open panel working AND get it building and running.

Comment
Add comment · Show 4
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 Nerevar · Jun 25, 2015 at 11:33 AM 0
Share

You can't do it this way, you have to find work-around, there are enough libraries and functions in the "regular" namespaces to do so. I don't see why you want to use EditorUtility here (only for its built-in UI?).

avatar image AmasterAmaster · Jun 25, 2015 at 11:51 AM 0
Share

Where can one find such libraries (within "namespaces") that handles saving and loading using a similar save/open panel that operating systems have? Does C# handle that natively? (I am guessing in the .NET framework?)

avatar image Nerevar · Jun 25, 2015 at 12:20 PM 0
Share

Well of course no you only have the basic function to open files then it is up to you to make a good interface using Unity UI. To manage files, it is in "System.IO" namespace.

avatar image AmasterAmaster · Jun 25, 2015 at 12:30 PM 0
Share

Ok, thanks for the help, and I will see what I can do on using the namespace.

2 Replies

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

Answer by Cherno · Jun 25, 2015 at 11:35 AM

You can't use any Editor Scripts with a Build. Scripts inside the Editor folder are not included in a Build, and if you put a script outside the Editor folder which uses UnityEditor, you won't be able to build. If you want to include normal, non-Editor folder scripts in your build which use UnityEditor, you can wrap the "offending" parts like this:

 #if UNITY_EDITOR
 using UnityEditor;
 #endif

The if and endif lines must go around every bit of code that uses anything related to UnityEditor. Of course, in the build, those lines don't exist, so in short: You can not use Editor function in a Build.

Hope that helps :)

Comment
Add comment · Show 5 · 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 AmasterAmaster · Jun 25, 2015 at 11:56 AM 0
Share

There has to be a way to do something like saving and loading a simple text file but also making it build. I also know that it is inevitable that I have to get rid of all the references to UnityEditor for this to work, but what should I do to get the desired solution that I seek?

avatar image Cherno · Jun 25, 2015 at 12:08 PM 0
Share

Do you want to open files, create files, write to them? Use System.IO :)

avatar image AmasterAmaster · Jun 25, 2015 at 12:18 PM 0
Share

So, I am guessing that there is no "User-Friendly" way of letting the user pick what file to load (as for creating and deleting is not an issue). I guess I will make a text-field based input or something for it to read and load the file name. Thanks for the help.

avatar image Cherno · Jun 25, 2015 at 12:24 PM 0
Share

Yes, you would need to code your own user interface. If you delve a bit deeper into IO, you will notice that you could just list all files in a specific folder, maybe even as buttons, and so on. Lots of funtionality there, and coupled with Unity's own UI/GUI functions, it shouldn't be too hard.

Edit: Corrected spelling errors ("cuntionality". Heh.)

avatar image AmasterAmaster · Jun 25, 2015 at 12:27 PM 0
Share

Alright, I will see what I can do on delving on that. Thanks again for the help.

avatar image
0

Answer by Bunny83 · Jun 26, 2015 at 01:31 AM

If you target windows you might want to use the windows API (which is also used by the Unity editor for those dialogs, at least the windows-unity-editor). There are many ways to create / display open file dialogs in windows.

I've just thrown together a PInvoke helper class which should enable you to use the GetOpenFileName method from the windows API.

OpenFileDialogWrapper

I've included all possible flags as well. Make sure you read all "notes" / comments if you want to change the method or use it in a different way.

Oh and don't change the "OpenFileName" class. It's a fix structure and the order of elements have to be exact like this.

Keep in mind that not all versions of windows might support this method or all flags. It's also possible that it might interfer with Unity in some way (like the changing working directory, read the comments ^^).

Also keep in mind that when calling this method your application will be blocked as long as the dialog is open. Once you click ok / cancel the method will return and your application will continue.

Here's an example use:

 string fileName = OpenFileDialogWrapper.GetFileName("C:\\", "Select a file", "Textfile(*.txt)", "txt");
 if (fileName != "")
 {
     Debug.Log("Selected file: " + fileName );
 }
 else
 {
     Debug.Log("User has canceled the dialog");
 }

Comment
Add comment · Show 2 · 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 Bunny83 · Jun 26, 2015 at 01:38 AM 0
Share

ps: I just added GetSaveFileName support as well ^^. It uses the same data struct / class so it's just a bit copy&paste.

avatar image AmasterAmaster · Jun 27, 2015 at 06:15 AM 0
Share

Ok, I will try that out as well. I would be so happy if I could get this to work with the project. Thanks in advance.

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

23 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

Related Questions

How do I change the default application used to open logs from the Unity editor? (on Mac) 1 Answer

Working with Unity Editor 1 Answer

EditorSceneManger.SetSceneDirty Problem 1 Answer

Can't able to find Addressable Profiler 0 Answers

Intractable CustomPropertyDrawer 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