Search Unity

Namespace TMPro not found in Test Runner test

Discussion in 'UGUI & TextMesh Pro' started by bluefix, Jul 19, 2018.

  1. bluefix

    bluefix

    Joined:
    Feb 24, 2018
    Posts:
    4
    When using Test Runner for a project containing TextMeshPro field, there is an error in Unity: error CS0246: The type or namespace name `TMPro' could not be found. Are you missing an assembly reference?

    Steps to reproduce:
    1. Create a new project.
    2. Add a TextMeshPro field to SampleScene
    3. Create a PlayMode test in Test Runner window
    4. In Visual Studio, in the test file, add "using TMPro;" statement
    5. It's underlined red.
    6. In Solution Explorer, in the test project, add a reference to Unity.TextMeshPro
    7. It's still underlined red.
    Is there a way to get rid of that error? I have it in 2018.1 and in 2018.2
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    I am assuming that you have installed the latest TMP UPM package (version 1.2.4) via the Package Manager UI and subsequently imported the TMP Essential Resources?

    (1) I just created a new project in Unity 2018.2.
    (2) Made sure the TMP UPM package we already installed which it is for Unity 2018.2.
    (3) Imported the TMP Essential Resources.
    (4) Created a test folder.
    (5) Open the Test Runner and use Create Test Script in current folder (the one I created above)
    (6) Adding the using TMPro;

    The steps above worked as expected on my end.

    Make sure that you are using the latest version of Visual Studio 2017 which is 15.7.5. If not, see if you get the same behavior with this latest release. If you are doing this in the same (new project), make sure you delete any solution created in that project and the Library folder.

    Let me know what happens.
     
  3. bluefix

    bluefix

    Joined:
    Feb 24, 2018
    Posts:
    4
    I have now Unity Personal 2018.2.0f2, VisualStudio Community 15.7.5 and TextMeshPro 1.2.4
    After updating VS to this version, nothing changed, there is the same error.

    (1) I created a brand new project in 2018.2.0f2 (those previous I mentioned before were imported from 2018.1).
    (2) TMPro was already installed in Package Manager.
    (3) I clicked "Import TMP essentials" before adding a TMPro field
    (4) Selected the root (Assets), opened Test Runner and clicked on "Create PlayMode test assembly folder" (with name MyTests2)
    (5) Then I created the script and opened it in VS.
    (6) Tried to add "using TMPro", intellisense doesn't suggest TMPro, after typing manually it's underlined in red.

    I don't know if it matters, but if I click in VS on "MyTests2/References" there is no Unity.TextMeshPro reference. However, in Assembly-CSharp/References there is such reference.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    The step that I missed which is causing the issue is related to the use of an Assembly Definition in this created test folder.

    The solution is to manually edit the .asmdef to add a reference to the Unity.TextMeshPro assembly.

    In order to do so, select the file and use the context menu "Show in Explorer" and then open the file in some text editor to make the following changes..

    Code (csharp):
    1.  
    2. {
    3.     "name": "Tests",
    4.     "references": [ "Unity.TextMeshPro" ],
    5.     "optionalUnityReferences": [ "TestAssemblies" ]
    6. }
    7.  
     
    naz89, jayvatar, oranchad and 4 others like this.
  5. bluefix

    bluefix

    Joined:
    Feb 24, 2018
    Posts:
    4
    This is working now, thanks!
     
    Stephan_B likes this.
  6. SusterDrifter

    SusterDrifter

    Joined:
    Jun 12, 2018
    Posts:
    3
    I am sorry, where do I find the .asmdef file ?
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    When the Test Runner creates the folder, it adds the .asmdef file in that folder.
     
  8. Xavier78

    Xavier78

    Joined:
    Oct 13, 2013
    Posts:
    41
    So better fix then having to edit the file for each script. It looks like you need to update Visual studio, because it is trying to build the references for the project, but is looking for them in the old location(aka not the new "Packages" folder in your project folder, but in your user folder?). Once updated It should rebuild for the project, if not maybe hit rebuild, or take out the "Unity.TextMeshPro.csproj" and "Unity.TextMeshPro.Editor.csproj" files from the project folder. Hope this helped someone else.
     
    Last edited: Oct 10, 2018
    amitsri20 likes this.
  9. caolam

    caolam

    Joined:
    Nov 12, 2018
    Posts:
    2
    work like a charm
    work like a charm, thanks so much
     
  10. hobote

    hobote

    Joined:
    Jan 9, 2016
    Posts:
    1
    I had a similar problem, but when creating the test folder it encouraged me to create a game assembly - so similar steps of embedding the reference to text mesh pro in the game assembly actually fixed my issue (which is not the test runner file.)