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:
    15
    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:
    40
    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:
    15
    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:
    15
    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