Search Unity

Type or namespace not found in package only

Discussion in 'Package Manager' started by PistolShrimps, Dec 1, 2018.

  1. PistolShrimps

    PistolShrimps

    Joined:
    Oct 17, 2018
    Posts:
    7
    I've written an Editor script that uses the LibGit2Sharp library. Downloading the lib and the native code dependency from the web and placing it in Assets/Plugins resulted in the familiar error

    The type or namespace name `LibGit2Sharp' could not be found. Are you missing an assembly reference?

    I was able to get rid of the missing type/namespace errors by installing LibGit2Sharp in Visual Studio through nuget, then moving the downloaded folders into Assets/Plugins from there. I've now made a Unity package that includes the Editor script and the Plugins folder with all contents. After importing that package into another project I am once again unable to build because of the missing reference error. How can I fix this without downloading through nuget?

    Here is the script where I use LibGit2Sharp

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.  
    3. using UnityEngine;
    4. using UnityEditor;                  // For BuildTarget
    5. using UnityEditor.Callbacks;        // For PostProcessBuild
    6. using System.IO;                    // For Directory, DirectoryInfo
    7. using System;                       // For Exception, DateTime, SystemInfo, String
    8. using System.Collections.Generic;   // For List
    9. using LibGit2Sharp;                 // For Git repository interaction
    10.  
    11. public class VersionInfoWriter : MonoBehaviour {
    12.  
    13.     private static void WriteVersionFile(string path)
    14.     {
    15.         List<string> versionTextLines = GenerateVersionInfo();
    16.  
    17.         using (StreamWriter writer = new StreamWriter(path, false))
    18.         {
    19.             foreach (string line in versionTextLines)
    20.             {
    21.                 writer.WriteLine(line);
    22.             }
    23.             writer.Close();
    24.         }
    25.     }
    26.  
    27.     private static List<String> GenerateVersionInfo () {
    28.  
    29.         // For storing version info to write
    30.         List<String> versionInfo = new List<string>();
    31.  
    32.         // Add our own build number from Project Setting in the Unity Editor
    33.         versionInfo.Add("Application Version: " + Application.version);
    34.         // Time of build and name of computer it was built on
    35.         versionInfo.Add("Built at: " + DateTime.Now);
    36.         versionInfo.Add("On Computer: " + SystemInfo.deviceName);
    37.  
    38.         // See if project is in a Git repository
    39.         Repository repo = FindRepository();
    40.  
    41.         // If we are in a repo, get the commit info
    42.         // and add it to the versionInfo list
    43.         if (repo != null)
    44.         {
    45.             try
    46.             {
    47.                 // Add info from the Git repository
    48.                 versionInfo.Add("Git Commit SHA: " + repo.Head.Tip.Sha);
    49.                 versionInfo.Add("Git Branch Name: " + repo.Head.CanonicalName);
    50.             }
    51.             catch (Exception e)
    52.             {
    53.                 Debug.Log(e.ToString());
    54.             }
    55.         }
    56.         else
    57.         {
    58.             Debug.Log("No repository found.");
    59.         }
    60.  
    61.         return versionInfo;
    62.  
    63.     }
    64.  
    65.     private static Repository FindRepository()
    66.     {
    67.         // get path to project folder
    68.         DirectoryInfo dir = Directory.GetParent(Application.dataPath);
    69.  
    70.         // Recursively check project parent folders
    71.         // to see if we are inside a repository
    72.         while (!Repository.IsValid(dir.FullName))
    73.         {
    74.             // We are not at the repository level yet
    75.             // switch to parent
    76.             dir = dir.Parent;
    77.             // If dir is null we reached root directory
    78.             // we are not inside a repository
    79.             if (dir == null)
    80.                 return null;
    81.         }
    82.         // Return repo directory
    83.         return new Repository(dir.FullName);
    84.     }
    85.  
    86.  
    87.     // Invoked by the Unity Editor just after building.
    88.     // Generate the version info and write it to a text
    89.     // file in the build directory.
    90.     [PostProcessBuild]
    91.     public static void OnPostprocessBuild(BuildTarget target, string pathToBuild)
    92.     {
    93.         Debug.Log("Creatinig version file in build folder.");
    94.  
    95.         int lastSlash = pathToBuild.LastIndexOf('/');
    96.         pathToBuild = (lastSlash > -1) ? pathToBuild.Substring(0, lastSlash) : pathToBuild;
    97.  
    98.         WriteVersionFile(pathToBuild + "/version-info.txt");
    99.     }
    100.  
    101. }
    102.  
    103. #endif
     
    hopetolive likes this.
  2. PistolShrimps

    PistolShrimps

    Joined:
    Oct 17, 2018
    Posts:
    7
    I've learned that LibGit2Sharp requires scripting runtime 4.x. I was testing my package in a new project with runtime set to 3.5.

    I'll need to notify the users of the package about this in the readme. There isn't a value for runtime version in the package manifest, but I can add it to the description field there as well.
     
    hopetolive likes this.