The type or namespace name 'NativeShare' could not be found (are you missing a using directive or an assembly reference?)

Viewed 2282

i'm using native share to share unity game. But i'm facing this error. How to resolve this?

Directories

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
using System.IO;
using System.Collections;

Native Share Code

    private IEnumerator TakeScreenshotAndShare()
    {
        yield return new WaitForEndOfFrame();

        Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        ss.Apply();

        string filePath = Path.Combine(Application.temporaryCachePath, "shared img.png");
        File.WriteAllBytes(filePath, ss.EncodeToPNG());

        // To avoid memory leaks
        Destroy(ss);

        new NativeShare().AddFile(filePath).SetSubject("Control The Pandemic").SetText("I found this game most attractive!").Share();
    }
    public void ShareGame()
    {
        StartCoroutine(TakeScreenshotAndShare());
    }

ERROR

CS0246: The type or namespace name 'NativeShare' could not be found (are you missing a using directive or an assembly reference?)
3 Answers

You probably need to add using NativeShare to the top of your script.

If that's not the issue and if NativeShare has its own assembly definition file, those can sometimes freak out for no reason in Unity. In that case close everything (unity visual studio etc.), delete the unity temporary folders (Libray, obj in root of your project) and open the project again.

Sol 1 - Try closing Unity, Unity HUB, VS Code and reopening it.

(Alternate) Sol 2 - After closing VS Code, try Unity -> Preferences -> External Tools -> Regenerate project files.

Make sure that you installed following:

  1. In the Unity Editor, select the Windows > Package Manager menu.
  2. Select the Visual Studio Editor package.
  3. If a new version is available, select Update.

enter image description here

  1. Download the Visual Studio installer, or open it if already installed.
  2. Select Install, or Modify if Visual Studio is already installed.
  3. Select the Workloads tab, then select the Game development with Unity workload.
  4. If Unity isn't already installed, select Unity Hub under Optional.
  5. Select Modify or Install to complete the installation.

enter image description here

  1. In the Unity Editor, select the Edit > Preferences menu.
  2. On the left, select the External Tools tab.

enter image description here

And finally Regenerate project files.

Related