Custom Editor and Visual Studio 'Solutions'
I am attempting to create my first custom editor in unity. This is a script called PlayerEditor.cs
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Player))]
public class PlayerEditor : Editor
{
public override void OnInspectorGUI()
{
}
}
When I open the script in Visual Studio, it gives an error of "The type or namespace name 'Player' could not be found. This is what Visual Studio's Solution Explorer looks like.
As you can see, it loads as two projects. The PlayerEditor
script is in the folder Project/SpaceShooter/Assets/Editor
. The actual folder structure is correct, but VS Solutions thinks they are two projects. If I manually drag the Editor folder to the Assets folder in the VS Solution explorer like so, the error resolves. Player is now found in the namespace. I can create custom controls and it works fine. The only problem here was Visual Studio thinking the scopes were different. The actual folder structure has not changed at all. This would be the end of my troubles, but every time I reload, and sometimes just randomly, VS removes the Editor folder from the first project.
So far I have tried deleting all the CSProj files and all the SLN files and reloading. Visual Studio rebuilds on opening again but does the same thing. I've tried moving the Editor folder to different locations like my script folder. This doesn't work either.
How does VS determine what cs files belong to which projects? Can I tell VS that the folder Editor and its contents are meant to be in the same project as everything else? Does anyone else have this problem?
TIA.
Answer by mikemcdonald83 · May 13, 2020 at 03:45 AM
I figured it out after hours of trying everything. The fix was so easy too. It was some kind of version mismatch between my project and the installed version of Visual Studio. If anyone else has the issue, try the following steps:
Open Unity Hub
Click on Installs in the right panel
Install the latest version of Unity and make sure that Visual Studio is selected in Modules
If your project is older, make sure to change the Unity Version of your project to the most recent one. This might break stuff, but it's always converted fine for me.
If that doesn't work Uninstall the latest version of Unity from the Installs tab and then follow the previous instructions again.
Apparently this was a problem with the way Unity defines Visual Studio's code checking between versions. The VS Solutions window still breaks it into three projects, but the namespaces are correctly referenced between the projects.
Your answer
Follow this Question
Related Questions
How can I display a scriptable object in a custom editor ? 1 Answer
Import .cs file into solution through code 0 Answers
How to compile DLL project using library DLL from Unity 2020 or above 1 Answer
Trim / Cut off parts of a Model in editor 0 Answers
How to change the text of EditorGUILayout.TextField 2 Answers