In this guide we will create a Unity Plugin for Windows Phone using C++. The plugin itself will have one function: it will add two numbers together and return the result.
First, we will create a new Unity project and add this script to the camera:
using UnityEngine;
public class SceneScript : MonoBehaviour
{
private readonly Rect m_FirstTextAreaLocation = new Rect(Screen.width / 5, Screen.height / 2, Screen.width / 5, Screen.height / 10);
private readonly Rect m_SecondTextAreaLocation = new Rect(3 * Screen.width / 5, Screen.height / 2, Screen.width / 5, Screen.height / 10);
private readonly Rect m_CalculateButtonLocation = new Rect(2 * Screen.width / 5, 4 * Screen.height / 5, Screen.width / 5, Screen.height / 10);
private readonly Rect m_ResultLabelLocation = new Rect(2 * Screen.width / 5, 3 * Screen.height / 5, Screen.width / 5, Screen.height / 10);
private GUIStyle m_GuiStyle;
private string m_FirstNumber, m_SecondNumber;
private string m_Result;
public delegate int AddFunction(int first, int second);
private AddFunction addFunction;
void Start()
{
m_GuiStyle = new GUIStyle { alignment = TextAnchor.MiddleCenter, fontSize = 32 };
}
void OnGUI()
{
m_FirstNumber = GUI.TextArea(m_FirstTextAreaLocation, m_FirstNumber ?? "");
m_SecondNumber = GUI.TextArea(m_SecondTextAreaLocation, m_SecondNumber ?? "");
int first, second;
bool firstParsed = false,
secondParsed = false;
if (int.TryParse(m_FirstNumber, out first))
{
firstParsed = true;
}
if (int.TryParse(m_SecondNumber, out second))
{
secondParsed = true;
}
if (firstParsed && secondParsed)
{
if (GUI.Button(m_CalculateButtonLocation, "Calculate"))
{
if (addFunction != null)
{
m_Result = "Result: " + addFunction(first, second).ToString();
}
else
{
m_Result = "Add function wasn't set!";
}
}
}
GUI.Label(m_ResultLabelLocation, m_Result, m_GuiStyle);
}
public void SetAddFunction(AddFunction func)
{
addFunction = func;
}
}
The script draws two text areas on the screen, and if parsable numbers are filled into both of them, displays a button to calculate the result with an AddFunction. Our addFunction can be set with SetAddFunction method, which will be exposed to use in our generated Visual Studio solution. Don’t forget to attach this script to the main camera in the scene. Export the project and open generated Visual Studio solution. In the generated solution, add a new project of type C++ -> Windows Phone -> Windows Phone Runtime Component:
Next, add a reference of our newly created plugin to your generated Visual Studio project:
It will be listed under Solution category:
Rename class from WindowsPhoneRuntimeComponent to NumberManager, to reflect the purpose of it:
Add a method to add two numbers in NumberManager class:
Finally, set the addFunction to our script in Unity_Loaded() method:
You’re done! Run the project and observe the results:
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.