How to implement Windows Phone code to communicate with Unity (event mode)

Source: Internet
Author: User

  

Some crap

Write an article yesterday to write an article today. Not my idle egg pain, is today a day met several annoying problems, wasted a day to fix. This article will be a useful article, of course, if you want to develop a unity game and publish it on WP. All right. No more nonsense. Enter the text.

There are two ways to implement Windows Phone code to communicate or invoke with unity, in general, by defining an event in unity, registering it in WP code, and then triggering the event in a certain case in unity, executing the method of registering the event in WP code;

Another is the plug-in method, that is, in WP writing DLL plug-in, placed in Unity's ASSETS/PLUGINS/WP8 path, and then directly in unity to invoke the static method in the plug-in.

Today, the first--the way of events.

Things to do in unity

First create a new script in unity, and call it random, like I'm called Eventhanlders.

Then turn on vs to edit it (what?) You don't need vs, you use MonoDevelop? Well, you're willing to make a mess with yourself.

Because the System namespace is not added by default in unity, and we use EventHandler here, first

Using System;

Then define an event and name it fucked;

public event EventHandler fucked;

Define a property to be used in WP's code in the future.

public string Fuckedornot;

Then define two methods, modify the value of the Fuckedornot property, and then trigger the fucked event.

public void befucked ()    {        fuckedornot = "fucked";        if (fucked! = null)        {            fucked (this,eventargs.empty);        }    }    public void notbefucked ()    {        fuckedornot = "notfucked";        if (fucked! = null)        {            fucked (this, eventargs.empty);}    }

Well, the events, attributes, and trigger methods are all written.

Next we draw two buttons in the Ongui to execute the two methods.

void Ongui ()    {        if (GUI. button (new Rect (N, A, Max, Max), "befucked"))        {            befucked ();        }        if (GUI. button (new Rect (+, (), notfucked))        {            notbefucked ();}    }

At this point, the code needed in unity has been written.

Drag the script to the camera below,

Then choose the target publishing platform as WP8, select File->builld Settings in the menu bar, click Windows Phone 8, point to switch Platform

  

Then click Build and select the target folder for the build project:

Then make sure that unity will release us as a WP8 solution. Pretty hanging.

What you need to do in WP

Now open the project you just built. Go to the MainPage.xaml.cs file and find the Unity_loaded method.

Register the Fucked event directly inside.

private void unity_loaded ()        {            setupgeolocator ();            var eventhandlers = (eventhandlers) UnityEngine.Object.FindObjectOfType (typeof (Eventhandlers));            eventhandlers.fucked + = eventhandlers_fucked;        }        void Eventhandlers_fucked (object sender, EventArgs e)        {            Dispatcher.begininvoke (() =            {                MessageBox.Show (sender as Eventhandlers). Fuckedornot);}            );        }

  

It is necessary to note thatunity is not executed in the UI main thread of the WP application, so the code that wants to execute the main thread of the UI needs to use the dispatcher.begininvoke

All right. Run and try it.

Let's Do it

is not very simple.

Here we need to say, Ongui this method in the emulator execution will have some problems, such as just when I wrote this example, in the emulator, I have not clicked button, it will continuously pop up "fucked", or "notfucked" (I rub it what meaning ah, Are you asking me whether it's a day or a few? ), so it is advisable to debug directly in the real machine.

and the simulator in the optimization is not good, will make people feel very card. In fact, even Lumia520, the general Unity 2D, runs without pressure in the real machine .

Summarize

I write this article is not to see, the current unity for WP article is relatively small, Microsoft official and unity are out of the relevant introductory tutorials, but in the actual development process will still find a lot of problems they do not mention. For example, unity on the very popular JSON plug-in can not be used on WP, such as unity on the very popular MD5 algorithm, can not be used on WP, such as unity on the very popular XXX can not be used on WP.

Hey, yo! Finally in the foreign site found some more comprehensive plug-ins, I wipe! 20 American Knives! I just quit my job, okay? I can't afford to write it myself.

Actually today I wrote a plugin for using JSON on WP and using MD5. In the next article to get out, who want to use who. I will also gradually improve the plugin, which day put on git up maybe.

Next I will combine my actual development to write WP and unity communication another way-plug-ins. (It seems that this is more popular, after all, the reusability is very strong).

Okay, no more nonsense. Slept, slept.

Again: Unity is really great. It makes a lot of people's childhood dreams come true, or they're trying to make it happen.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.