Package Installer Plugin
The Unity engine is built with native C/C++ internally, however it has a C# wrapper that you use to interact with it. As such, you need to be familiar with some key concepts of scripting in C#. This section of the User Manual contains information on how Unity implements .NET and C#, and any exceptions you might encounter as you code.
In Unity, you normally use scripts
to create functionality, but you can also include code created outside Unity in the form of a plug-in. You can use two different kinds of plug-in in Unity:
Native plugin
Managed code is accessible to the standard .NET tools that Unity uses to compile scripts. The only difference between managed plug-in code and Unity script code is that the plug-ins are compiled outside of Unity and so Unity might not be able to access the source. When using native plug-ins, Unity’s tools can’t access third-party code libraries in the same way that they can access the managed libraries. For example, if you forget to add a managed plug-in file to the project, you will get standard compiler error messages. Whereas, if you forget to add a native plug-in file to the project, you will only see an error report when you try to run the project.Platform-specific native code libraries. They can access features like operating system calls and third-party code libraries that would otherwise be unavailable to Unity.
Unity supports native plug-ins , which are libraries of native code you can write in languages such as C, C++, and Objective-C. Plug-ins allow the code you write in C# to call functions from these libraries. This feature allows Unity to integrate with middleware libraries or existing C/C++ code. The native plug-in provides a simple C interface, which the C# script then exposes to your other scripts . Unity can also call functions that the native plug-in exports when certain low-level rendering events happen
const string PluginName = "com.dinesh.contacts.FetchContacts";
static AndroidJavaClass _pluginClass;
static AndroidJavaObject _pluginInstance;
static AndroidJavaClass _unityClass;
To call Java code from C# scripts, Unity provides C# APIs that communicate with the Android Java Native Interface (JNI) through C++. Unity provides both a low level and a high level API that you can use to interact with Java code using JNI.
private static AndroidJavaClass PluginClass => _pluginClass ??= new AndroidJavaClass(PluginName);
private static AndroidJavaClass UnityClass => _unityClass ??= new AndroidJavaClass("com.unity3d.player.UnityPlayer");
private static AndroidJavaObject UnityPlayerActivity => UnityClass.GetStatic >AndroidJavaObject>("currentActivity");
Check for Permissions "android.permission.READ_CONTACTS" in case the permission is authorized fetch contact else request for permission and wait for callbacks.
public static void CheckPermission()
{
if (Permission.HasUserAuthorizedPermission("android.permission.READ_CONTACTS"))
{
DoReadContact();
}
else
{
var callbacks = new PermissionCallbacks();
callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
Permission.RequestUserPermission("android.permission.READ_CONTACTS", callbacks);
}
}
Subscribe to the callbacks and in case if the permission is given trigger the PluginClass to read contacts and get contact counts, subscribe to the contact count callbacks
private static void DoReadContact()
{
PluginInstance.Call("ReadContact", UnityPlayerActivity);
_contactCount = PluginInstance.Call>int>("FetchContactsCount");
FetchedContactCountCallbacks?.Invoke(new object(),_contactCount);
FetchContactByCount();
}
private static void FetchContactByCount()
{
List>Tuple>string, string>> contacts = new List>Tuple>string, string>>();
char[] charsToSplit = {','};
for (int i = 0; i < _contactCount; i++)
{
string contactInfo = PluginInstance.Call>string>("FetchContactsCount", i);
string[] contactSplit = contactInfo.Split(charsToSplit);
contacts.Add(new Tuple>string, string>(contactSplit[0], contactSplit[1]));
}
FetchedContactsCallbacks?.Invoke(new object(), contacts);
}
You can download the plugin by clicking Download
Download Source Code from Download
Thank you for using the plugin.
Please do contact me through din82958@gmail.com or dineshmrx@icloud.com