Search Unity

[MenuItem] attribute does not work with custom package

Discussion in 'Package Manager' started by ZammyIsOnFire1, Sep 13, 2019.

  1. ZammyIsOnFire1

    ZammyIsOnFire1

    Joined:
    Sep 29, 2014
    Posts:
    19
    Hello,

    I want to export a simple editor script as a custom package. Unfortunately my custom menu does not appear in Unity.

    I am using embedded package. I have an assembly definition in my Editor folder where my script is.
    Testing on 2018.4.1 with 2.0.7 package manager on Win64 system.

    Here is folder structure:
    upload_2019-9-13_15-51-32.png

    Here is actual script in full
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.Experimental.SceneManagement;
    4. using UnityEditor.SceneManagement;
    5.  
    6. namespace TabTale
    7. {
    8.     public class ObjectReplacer : EditorWindow
    9.     {
    10.         [MenuItem("[TabTale]/Object Replacer")]
    11.         static void Init()
    12.         {
    13.             var window = EditorWindow.GetWindow(typeof(ObjectReplacer));
    14.             window.titleContent = new GUIContent("Object Replacer");
    15.             window.Show();
    16.         }
    17.  
    18.         void OnEnable()
    19.         {
    20.             if (EditorPrefs.HasKey(KEY_PREFAB))
    21.             {
    22.                 var path = EditorPrefs.GetString(KEY_PREFAB);
    23.                 _prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
    24.             }
    25.             if (EditorPrefs.HasKey(KEY_SCALE))
    26.             {
    27.                 _copyScale = EditorPrefs.GetInt(KEY_SCALE) != 0;
    28.             }
    29.         }
    30.  
    31.         void OnDisable()
    32.         {
    33.             EditorPrefs.SetString(KEY_PREFAB, AssetDatabase.GetAssetPath(_prefab));
    34.             EditorPrefs.SetInt(KEY_SCALE, (_copyScale ? 1 : 0));
    35.         }
    36.  
    37.         void OnGUI()
    38.         {
    39.             EditorGUILayout.BeginHorizontal();
    40.             GUILayout.Label("Prefab");
    41.             _prefab = (GameObject)EditorGUILayout.ObjectField(_prefab, typeof(GameObject), allowSceneObjects: false);
    42.             EditorGUILayout.EndHorizontal();
    43.  
    44.             EditorGUILayout.BeginHorizontal();
    45.             GUILayout.Label("Copy Scale");
    46.             _copyScale = EditorGUILayout.Toggle(_copyScale);
    47.             EditorGUILayout.EndHorizontal();
    48.  
    49.             if (GUILayout.Button("Replace"))
    50.             {
    51.                 var selection = Selection.objects;
    52.  
    53.                 foreach (GameObject obj in selection)
    54.                 {
    55.                     var parent = obj.transform.parent;
    56.                     var prefabObj = (GameObject)PrefabUtility.InstantiatePrefab(_prefab, parent);
    57.                     prefabObj.transform.position = obj.transform.position;
    58.                     prefabObj.transform.rotation = obj.transform.rotation;
    59.                     if (_copyScale)
    60.                         prefabObj.transform.localScale = obj.transform.localScale;
    61.                 }
    62.  
    63.                 for (int i = selection.Length - 1; i >= 0; i--)
    64.                 {
    65.                     DestroyImmediate(selection[i]);
    66.                 }
    67.  
    68.                 var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
    69.                 if (prefabStage != null)
    70.                 {
    71.                     EditorSceneManager.MarkSceneDirty(prefabStage.scene);
    72.                 }
    73.             }
    74.         }
    75.  
    76.         const string KEY_PREFAB = "TTObjectReplacer_Prefab";
    77.         const string KEY_SCALE = "TTObjectReplacer_Scale";
    78.  
    79.         [SerializeField] GameObject _prefab;
    80.         [SerializeField] bool _copyScale;
    81.     }
    82. }
     

    Attached Files:

  2. manu73

    manu73

    Unity Technologies

    Joined:
    Aug 22, 2014
    Posts:
    68
    Hi @ZammyIsOnFire1,

    It works perfectly fine for me on 2018.4.8f1,

    Screen Shot 2019-09-13 at 09.36.38.png

    Did you check that in your assembly definition file, Editor is checked in Platforms?

    Regards,
    EmmanuelH
     
  3. ZammyIsOnFire1

    ZammyIsOnFire1

    Joined:
    Sep 29, 2014
    Posts:
    19
    upload_2019-9-16_15-49-28.png

    Here it is. I am going to download version you mentioned and check it out. Otherwise I do not see any issues in my setup, its by the docs.
     
  4. ZammyIsOnFire1

    ZammyIsOnFire1

    Joined:
    Sep 29, 2014
    Posts:
    19
    I am not sure what exactly was the issue but playing with platforms made it work. I simply deselected all and selected them again and menu showed up. There is definitely some issue but I cannot consistently reproduce it.

    I want to thank you for your timely response!

    I left them like that:
    upload_2019-9-16_16-31-44.png
     
  5. pahe4retro

    pahe4retro

    Joined:
    Aug 13, 2019
    Posts:
    33
    I'm currently experiencing the same problem, though I import my own package via git url. In my project where I have the package embedded, it works like a charm though.

    Does anyone have it working for git imported custom packages?
     
  6. ethan_jl_unity

    ethan_jl_unity

    Unity Technologies

    Joined:
    Sep 27, 2018
    Posts:
    104
    Hi @pahe4retro, which version of Unity do you have? Have you tried closing and reopening Unity (that usually fixes some loading issues)?
     
  7. pahe4retro

    pahe4retro

    Joined:
    Aug 13, 2019
    Posts:
    33
    Using 2019.3.0f1. Also submitted a bug for it if you want to check it: Case 1204133.

    Thanks for taking a look into it.
     
    ethan_jl_unity likes this.
  8. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    I'm having the same issue on Using 2019.3.0f1.
    Ya I'm having the same issue on 2019.3.0f1. I'm not getting any compilation errors but it seems as if nothing in my package is accessible. I was trying it with this repo.
     
  9. pahe4retro

    pahe4retro

    Joined:
    Aug 13, 2019
    Posts:
    33
    Ticket is being worked on: link
     
    maximeb_unity likes this.
  10. agustinBFG

    agustinBFG

    Joined:
    Mar 23, 2021
    Posts:
    1
    any news about this I'm having this issue in 2018.4.28f
     
  11. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Judging by the ticket above, this wasn't a bug and was in fact flagged as by design. Can you let us know how this is inhibiting your workflow so I can discuss this internally?
     
  12. dchertoff

    dchertoff

    Joined:
    May 25, 2020
    Posts:
    7
    Well it means we can't have editor tools delivered through packages. For example, we group a series of tools that are used in all of our projects in a single package that can be added as needed. We don't want to be forced to use a git submodule to deliver tool content, nor do we want to individually add all these tools to projects.

    The point of packages is it's easy to just drop the functionality into your project without having to know how git works.

    I don't see how a lack of support for editor tools in packages can be "by design." This literally means that adding editor tools should not work through packages, by design?
     
  13. stanton2020

    stanton2020

    Joined:
    Feb 10, 2018
    Posts:
    1
    I recently had this issue. I resolved it by deleting the package and copying it back into my assets folder.
     
  14. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    Would it be possible to know what the reasoning behind this design is?

    As @dchertoff said above, delivering tools via packages is a very common use case and this is a major inconvenience.
     
  15. maximeb_unity

    maximeb_unity

    Unity Technologies

    Joined:
    Mar 20, 2018
    Posts:
    550
    Hi @dchertoff and @JoNax97,

    That issue turned out to be some problems in the user's .asmdef file. It included the following snippet:

    Code (CSharp):
    1.     "defineConstraints": [
    2.     "UNITY_INCLUDE_TESTS"
    3. ]
    As a result, this marked the assembly as "test-only" and it was not compiled by the script pipeline. When a package is embedded, it is considered "testable" and test-only assemblies all get compiled and loaded. This is why it was considered "by design"; removing this
    defineConstraint
    value fixed if for them.
     
    JoNax97 likes this.
  16. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    Oh ok, that is perfectly reasonable.

    Thank you for taking the time to explain.
     
    maximeb_unity likes this.
  17. maximeb_unity

    maximeb_unity

    Unity Technologies

    Joined:
    Mar 20, 2018
    Posts:
    550
    You're welcome! I can totally understand how you felt the "by design" resolution might not make sense, so it thought that some transparency was in order, not to mention that other people may be running into the same issue.
     
  18. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    In case anyone comes across this, I was having the same issue, but I checked my Assembly Definition and the define constraints were empty.

    The only possible discrepancy I noticed was that in the list of platforms, only Editor was ticked, however "All Platforms" was also ticked at the top. I unchecked that, then unchecked the rest so that only Editor was checked again and then the MenuItems showed up as expected.