- Home /
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.
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?).
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?)
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.
Ok, thanks for the help, and I will see what I can do on using the namespace.
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 :)
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?
Do you want to open files, create files, write to them? Use System.IO :)
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.
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.)
Alright, I will see what I can do on delving on that. Thanks again for the help.
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.
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");
}
ps: I just added GetSaveFileName support as well ^^. It uses the same data struct / class so it's just a bit copy&paste.
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.