Search Unity

Editor For Custom File Type

Discussion in 'Immediate Mode GUI (IMGUI)' started by VigilantePanda, Oct 4, 2012.

  1. VigilantePanda

    VigilantePanda

    Joined:
    Oct 1, 2012
    Posts:
    1
    I am looking for a way to edit and open a custom file type that I have defined. This file type will be used to store information for a 2D sprite and animations with multiple frames. Let's say the file extension is '.mytype'.

    Ideally, I would like to be able to make a new option in the Assets > Create menu for creating a 'MyType' file. and then a custom editor window can come up and allow me to edit the new file. I would also like to be able to double-click or "right click > edit MyType" on one of these files in the Project view and have it open in my custom editor window.

    So, I'm wondering if all or any of my ideas are even possible. I know I asked for a lot of information, but I'm not looking for a complete solution. Just the knowledge if this is possible and a push in the right direction would be great.

    One solution that I think would work is to make my own EditorWindow that will find and display all '.mytype' files in my project. This would be an adequate solution, but I would really like to try to get it to work with my ideal workflows given above.

    Thanks in advance.
     
  2. _Allen

    _Allen

    Joined:
    Jan 14, 2014
    Posts:
    3
    I think we have same problem. Is there any interface to handle "try to open specify files" event?
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    To add a right-click option in the Project view, add a MenuItem static method in a class that's inside an Editor folder. You could put the method inside your EditorWindow class. Here's a short example. It assumes that your EditorWindow class has a static method named Init() that takes an object of MyType.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public static class OpenMyEditorWindow {
    6.  
    7.     // This method opens the editor window. The "Assets/" part adds it to the Project context menu.
    8.     [MenuItem("Assets/edit MyType")]
    9.     static void EditMyType() {
    10.         MyEditorWindow.Init(Selection.activeObject as MyType);
    11.     }
    12.  
    13.     // This method makes sure the menu item above is only active for objects of MyType:
    14.     [MenuItem("Assets/edit MyType", true)]
    15.     static bool EditMyType() {
    16.         return (Selection.activeObject.GetType() == typeof(MyType));
    17.     }
    18. }
    19.  

    If you wanted to open an external program, you could change that first method to something like:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Diagnostics;
    5.  
    6. public static class OpenMyEditorWindow {
    7.  
    8.     // This method opens an external program.
    9.     [MenuItem("Assets/edit MyType")]
    10.     static void EditMyType() {
    11.         Process.Start(<program>, Selection.activeObject.name);
    12.     }
    13.         ...
    14.  
    You need to add the path to your program in Process.Start, and you may need to fiddle with the second argument also depending on how forgiving your external program is.
     
    Last edited: Feb 7, 2014
  4. _Allen

    _Allen

    Joined:
    Jan 14, 2014
    Posts:
    3
    It's a cool way to solve this problem, thanks alot~
    Is there any way to open "MyType" when double-click the MyType file?
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Not that I know of.
     
  6. Dombi-Bence

    Dombi-Bence

    Joined:
    Mar 23, 2021
    Posts:
    4