Disable / Enable CustomEditor ???

Hello Friends!

I’m working on a editor tool and it seems to conflict with native functions in Unity when adding own script.

I struggle with this problem that i cant drag and drop objects into scene when i use

[CustomEditor(typeof(GameObject))]
3342215--261006--upload_2018-1-4_19-43-5.png

but when i comment it out it works.
//[CustomEditor(typeof(GameObject))]

3342215--261007--upload_2018-1-4_19-45-0.png

So, I need a way do disable/enable the whole script or any other solution so i can toggle to use that code. I only need it at certain times. I hope the question/problem is clear enough?

I’m very thankful for help or suggestions!!! /Peter

1 Like

Need to see more of the code to say if that’s what’s causing the problems, but try namespaces. Whenever you use simple names for classes, isolating them in your own namespace will avoid name collisions.

Never tried making a custom editor for GameObject. I don’t think thats wise either, how are you using it? becouse i don’t think a solution like that is possible, but there might be a workaround.

1 Like

@peterkungen

Hi,

I’m not sure about your case, but maybe in general, something like this might work for “disabling” custom editor and then show the default editor for your script. In your editor script’s OnInspectorGUI method:

// If custom editor not enabled
if (editorEnabled.boolValue == false)
{
  base.OnInspectorGUI();
  return;
}

// Custom gui code here when editorEnabled true
1 Like

So my code is below. What it does is instantiate gameobjects on the y axis. So in editor when looking in otrhographic view from above, you use the numpad for instantiate different blocks to build a level. But when you dont use it, and want to work as usual, it conflict as i described first of.

thanks for pointing out namespace collision orb, i will look in to that. whileBreak no i dont use it on a gameobject. Only as described above. eses I dont really se how I can implement your solution, maby thats only if thats on a gamObject? But I will check in to the code to understand it more.

thanks for reply!

this script is not attached to any gameobject

using UnityEngine;
using UnityEditor;
using System;


[CustomEditor(typeof(GameObject))]

public class PlaceMesh : Editor
{

    public Vector2[] grid2DArray = new Vector2[2];
    public string lastCommand;
    public int gridSize = 6;
    public float xi = 7.0f;
    GameObject meshFromAssetFolder;


        void OnSceneGUI()
        {
            if(!PlaceMeshWindow.navMeshEditMode==true)
            {

            Event e = Event.current;
            //Debug.Log(e.keyCode.ToString());

                switch (e.type)
                {

                    case EventType.KeyUp:
                        string buttonEvent = Event.current.keyCode.ToString();
                        if (buttonEvent != "None")
                            PlacePiece(buttonEvent);
                        break;
                }
            }

        }


        public void PlacePiece(string buttonEvent)
        {

           // Ray rayHit = Camera.current.ScreenPointToRay(Input.mousePosition);

           // rayHit.origin = new Vector3(rayHit.origin.x, 0f, rayHit.origin.z);



            Vector2 guiPosition;
            guiPosition = Event.current.mousePosition;
            Ray ray = HandleUtility.GUIPointToWorldRay(guiPosition);

            //Debug.Log(ray.origin);
            float XValueNormalised = (ray.origin.x % gridSize) / gridSize;
            float ZValueNormalised = (ray.origin.z % gridSize) / gridSize;
            //Debug.Log("XValueNormalised ->" + XValueNormalised + " and as abs" + Math.Abs(XValueNormalised));
            //Debug.Log("ZValueNormalised ->" + ZValueNormalised + " and as abs" + Math.Abs(ZValueNormalised));
            if (Math.Abs(XValueNormalised) < 0.15 || Math.Abs(XValueNormalised) > 0.85)
            {
                Debug.Log("Left-Right axis to close in between two tiles!");
                return;
            }
            if (Math.Abs(ZValueNormalised) < 0.15 || Math.Abs(ZValueNormalised) > 0.85)
            {
                Debug.Log("Up-Down axis to close in between two tiles!");
                return;
            }
            float XCordinateToPlace = 0f;
            float ZCordinateToPlace = 0f;
            if (ray.origin.x > 0)
            {
                //Debug.Log("x" + (ray.origin.x - ray.origin.x % gridSize + gridSize / 2));
                XCordinateToPlace = (ray.origin.x - ray.origin.x % gridSize + gridSize / 2);
            }
            if (ray.origin.x < 0)
            {
                //Debug.Log("x" + (ray.origin.x - ray.origin.x % gridSize - gridSize / 2));
                XCordinateToPlace = (ray.origin.x - ray.origin.x % gridSize - gridSize / 2);
            }
            if (ray.origin.z > 0)
            {
                //Debug.Log("z" + (ray.origin.z - ray.origin.z % gridSize + gridSize / 2));
                ZCordinateToPlace = (ray.origin.z - ray.origin.z % gridSize + gridSize / 2);
            }
            if (ray.origin.z < 0)
            {
                //Debug.Log("z" + (ray.origin.z - ray.origin.z % gridSize - gridSize / 2));
                ZCordinateToPlace = (ray.origin.z - ray.origin.z % gridSize - gridSize / 2);
            }

            grid2DArray[0] = grid2DArray[1];
            Vector3 spawnPos = new Vector3(XCordinateToPlace, 0, ZCordinateToPlace);
            grid2DArray[1] = new Vector2(XCordinateToPlace,ZCordinateToPlace);
            //Debug.Log(string.Format("spawnPosition {0}", spawnPos));
            //Debug.Log(string.Format("Position 1 :{0} and Position 2 :{1}",grid2DArray[0],grid2DArray[1]));


            TileToolObjects scriptTileToolObjects = GameObject.Find("ScriptHolder").GetComponent<TileToolObjects>();

            switch (buttonEvent)
            {
                case "Keypad1":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton1;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube1.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad1");
                    break;
                case "Keypad2":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton2;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube2.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad2");
                    break;
                case "Keypad3":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton3;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad3");
                    break;
                case "Keypad4":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton4;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad4");
                    break;
                case "Keypad5":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton5;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad5");
                    break;
                case "Keypad6":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton6;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad6");
                    break;
                case "Keypad7":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton7;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad7");
                    break;
                case "Keypad8":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton8;
                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad8");
                    break;
                case "Keypad9":
                    meshFromAssetFolder = scriptTileToolObjects.NumPadButton9;

                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad9");
                    break;
                case "Keypad0":
                    meshFromAssetFolder = null;

                    //meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
                    //Debug.Log("Keypad9");
                    break;

                case "Keypad5-ButtonNotAccecable":
                     Vector2[] matrix2x2 = new Vector2[2];

                    if (grid2DArray[0].y < grid2DArray[1].y)
                    {
                        matrix2x2[0] = grid2DArray[0];
                        matrix2x2[1] = grid2DArray[1];
                    }
                    else
                    {
                        matrix2x2[0] = grid2DArray[1];
                        matrix2x2[1] = grid2DArray[0];
                    }

                    if (grid2DArray[0].x < grid2DArray[1].x)
                    {
                        matrix2x2[0] = grid2DArray[0];
                        matrix2x2[1] = grid2DArray[1];
                    }
                    else
                    {
                        matrix2x2[0] = grid2DArray[1];
                        matrix2x2[1] = grid2DArray[0];
                    }
                    int xaxis=0;
                    int yaxis=0;

                    int plusx = 0;
                    int plusy = 0;

                    Debug.Log("from x:" + (3 - Math.Abs((int)matrix2x2[0].x)*gridSize));
                    Debug.Log("matrix x:" + (xaxis - Math.Abs((int)matrix2x2[1].x)));
                    Debug.Log("from y:" + (yaxis - Math.Abs((int)matrix2x2[1].y)));
                    Debug.Log("matrix y:" + (3 - Math.Abs((int)matrix2x2[0].y) * gridSize));
                    for (xaxis = 0-Math.Abs((int)matrix2x2[0].x); xaxis < (int)matrix2x2[1].x; xaxis+=6)
                    {
                        Debug.Log("inne i x");
                        for (yaxis = 0 - Math.Abs((int)matrix2x2[0].y); yaxis < (int)matrix2x2[1].y; yaxis+=6)
                        {
                            Debug.Log("inne i y");
                            Vector3 matrixSpawnPos = new Vector3(matrix2x2[0].x+plusx, 0f, matrix2x2[0].y+plusy);
                            Debug.Log(matrixSpawnPos);

                            float radiusInMatrix = 1f;
                            Collider[] hitCollidersInMatrix = Physics.OverlapSphere(matrixSpawnPos, radiusInMatrix);
                            int iInMatrix = 0;
                            while (iInMatrix < hitCollidersInMatrix.Length)
                            {
                                Debug.Log("hitCollider location " + matrixSpawnPos);
                                Debug.Log("hitColliders :" + hitCollidersInMatrix[iInMatrix]);
                                UnityEngine.Object.DestroyImmediate(hitCollidersInMatrix[iInMatrix].transform.gameObject);
                                iInMatrix++;
                            }
                            Instantiate(meshFromAssetFolder, matrixSpawnPos, Quaternion.identity);
                            plusy =+ gridSize;
                        }
                        plusx = +gridSize;
                        plusy = 0;
                    }
                    Debug.Log("Spawnvector from"+matrix2x2[0] + " to " + matrix2x2[1]);
                    return;
                default:
                    Debug.Log("keycode not assigned :"+buttonEvent);
                    return;

            }

            float radius = 1f;
            Collider[] hitColliders = Physics.OverlapSphere(spawnPos, radius);
            int i = 0;
            while (i < hitColliders.Length)
            {
                Debug.Log("hitColliders :" + hitColliders);
                UnityEngine.Object.DestroyImmediate(hitColliders.transform.gameObject);
                i++;
            }


            //Debug.Log("meshFromAssetFolder -->"+ meshFromAssetFolder);
            //Instantiate(meshFromAssetFolder, spawnPos, Quaternion.identity);
            //Instantiate(Resources.Load(meshFromAssetFolder.name), spawnPos,meshFromAssetFolder.transform.rotation);
            Debug.Log(meshFromAssetFolder.name);
            //GameObject go = Instantiate(Resources.Load(meshFromAssetFolder.name)) as GameObject;
            GameObject go =  PrefabUtility.InstantiatePrefab(meshFromAssetFolder) as GameObject;
            go.transform.position = spawnPos;




            lastCommand = buttonEvent;
        }
}

There is also a gameobject in scene that “ScriptHolder” that holds this script.

*_ <em>*using System.Collections; using System.Collections.Generic; using UnityEngine; public class TileToolObjects : MonoBehaviour { public GameObject NumPadButton1; public GameObject NumPadButton2; public GameObject NumPadButton3; public GameObject NumPadButton4; public GameObject NumPadButton5; public GameObject NumPadButton6; public GameObject NumPadButton7; public GameObject NumPadButton8; public GameObject NumPadButton9; }*</em> _*

First things first… learn to use code tags:
Using code tags properly page-2#post-3334556

Also you seem to want to handle a given Event… so why not just have a static class with the InitializeOnLoad:

Then in the static constructor for that class register for the update event:

And do it there.

If you still need to toggle it at this point… maybe also have a hotkey, or a MenuItem, that toggles a static bool in this script. And in your update method you don’t do anything if the bool is flagged in whatever manner.

If you REALLY need it as this editor script in your post (maybe you do, I don’t know… I can’t read that horrible paste of code). Again, have this toggle, and call DrawDefaultInspector from your script if you want it to behave the default way unity does.

[CustomEditor(typeof(GameObject))]
public class PlaceMesh : Editor

I don’t know if you knew, but this is for creating a custom editor for a component(in this case a game object, so in a sense it is attached to all of your game objects), that’s why i could not understand why you would use it with a game object. Since what you want is a game object manager rather than a different way to handle each game object.

You would need to make an Editor Script to handle your scene instead.

1 Like

So I dont fully understand the consept of CustomEditor.

When I moved my stuff to OnSceneGui() and it worked as i wanted. Thanks for input, it helped me find the way! :slight_smile:

2 Likes

Good to hear, just for the record a Custom Editor is primarily for handling how a component is shown in the Inspector Tab