Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion Using UnityEngine.UI fails to be regognised by Visual Studio 2022

Discussion in 'Code Editors & IDEs' started by wuyiji199632, Oct 31, 2023.

  1. wuyiji199632

    wuyiji199632

    Joined:
    Dec 1, 2021
    Posts:
    31
    My unity project already contains the needed libraries that make up UnityEngine.UI but visual studio still threw me this error. Can anyone tell me how to fix this? I tried the Reimport All inside the Assets folder but nothing worked.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Runtime.InteropServices;
    6. using UnityEngine.UIElements;
    7. using UnityEngine.UI;
    8.  
    9. public class NetworkManager : MonoBehaviour
    10. {
    11.  
    12.     /*External C++ Functions for Networking*/
    13.  
    14.     [DllImport("NetworkAccess")]
    15.     private static extern void SetLogCallback(LogCallback callback);// This is the function used for callback of debugging
    16.  
    17.  
    18.     [DllImport("NetworkAccess")]
    19.     private static extern void InitializeServer();
    20.  
    21.  
    22.     [DllImport("NetworkAccess")]
    23.     private static extern void InitializeClient();
    24.  
    25.     [DllImport("NetworkAccess")]
    26.     private static extern void ConnectToServer();
    27.     [DllImport("NetworkAccess")]
    28.     private static extern void BroadcastBanditSelection(string playerID, string banditType);
    29.  
    30.     // This delegate matches the C++ callback signature
    31.     public delegate void LogCallback(string message);
    32.  
    33.     [AOT.MonoPInvokeCallback(typeof(LogCallback))]
    34.     private static void LogFromDLL(string message)
    35.     {
    36.         Debug.Log(message);
    37.     }
    38.  
    39.     public Button selectHeavyBanditBtn, selectLightBanditBtn, confirmBtn;
    40.    
    41.     private string selectedBanditType = "";
    42.     // Start is called before the first frame update
    43.     void Start()
    44.     {
    45.         SetLogCallback(LogFromDLL);
    46.         InitializeServer();
    47.         //InitializeClient();
    48.         //ConnectToServer();
    49.  
    50.      
    51.  
    52.  
    53.     }
    54.  
    55.     // Update is called once per frame
    56.     void Update()
    57.     {
    58.        
    59.     }
    60.  
    61.     public void SelectBandit(string banditType)
    62.     {
    63.         selectedBanditType = banditType;
    64.     }
    65.  
    66.     public void ConfirmSelection()
    67.     {
    68.         if (!string.IsNullOrEmpty(selectedBanditType))
    69.         {
    70.             // Ideally, you'd have a unique playerID for each player.
    71.             // This is a placeholder; replace with your playerID generation method.
    72.             string playerID = "Player_" + UnityEngine.Random.Range(1000, 9999);
    73.  
    74.             BroadcastBanditSelection(playerID, selectedBanditType);
    75.         }
    76.         else
    77.         {
    78.             Debug.LogWarning("No bandit type selected.");
    79.         }
    80.     }
    81. }
    82.  
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,312
    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the package inside of Unity: Window -> Package Manager -> Search for Visual Studio Editor -> Press the Update button

    Depending on flavor and version of Visual Studio, it may also have an installation step that you perform within the actual Visual Studio. This step seems finicky at best and may require multiple openings of VS before it comes up.

    Update: The VSCode extension has been deprecated and abandoned:

    https://forum.unity.com/threads/update-on-the-visual-studio-code-package.1302621/

    Update: the VSCode integration is back... maybe!?

    https://forum.unity.com/threads/microsoft-previews-unity-extension-for-visual-studio-code.1468913/

    There may be a community fork available that is receiving updates.

    https://github.com/Chizaruu/com.tsk.ide.vscode

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874

    Recently (July 2023) I worked on a Windows11 system that required a Microsoft component to be installed from within Visual Studio before it would work properly with all the OTHER software installed under Unity. I have no documentation on that process as I have only seen it once and it surprised me as well.
     
  3. sailro

    sailro

    Microsoft

    Joined:
    Jul 30, 2014
    Posts:
    167
    Hi @wuyiji199632

    Please make sure:

    1- your project is using the latest VS Editor package (2.0.22 as of today) => Unity Package Manager
    2- Visual Studio is set as your preferred editor in Unity => Preferences / External Tools

    Regards