Unity's cross-platform ultimate solution (Pc,ios,android-C + +) and platform support

Source: Internet
Author: User

47865741

PC actually do not have to say, after all, C # and C + + interactive articles have enough, of course, I think after a few toss, almost all the game needs to the operation I have mastered (all kinds of methods, various pits, do not understand the message can be asked, although basically no one to see, haha)

Needless to say, we're mainly talking about two big platforms--ios and android--interacting with Unity's native code

Here wordy is to go online search are all kinds of egg pain, such as if you want to call Unity C # function, almost exclusively is to give Unitysendmessage method, in the project with this is simply to die, so many functions so complex parameters you this broken function top fart use AH. iOS is good to say, Android is a pit, actually want you to go and Java code to interact, simply speaking C + +-"Java-" C #, and in fact, most of the time you do not need such a pain, direct C + +-"C # is OK, because C + + can operate almost all the underlying resources , of course, individual requirements exceptions

Business

[CPP]View PlainCopy
  1. typedef struct Parameter {
  2. int A;
  3. int b;
  4. } Param;
  5. typedef VOID (*callback) (param* p);
  6. void TestFunc (CallBack cb) {
  7. Param p;
  8. P.A = 10;
  9. P.B = 20;
  10. CB (&P);
  11. }


extern "C" this detail is not much to say, because I directly built a. C file so do not need this tag, here directly with the typical callback function as an example, because there is a callback, you do not have to consider how to call C/C + + or vice versa, because this example actually contains the exchange of information

[CSharp]View PlainCopy
  1. Public class Newbehaviourscript:monobehaviour {
  2. [StructLayout (LayoutKind.Sequential)]
  3. struct Parameter {
  4. public int A;
  5. public int B;
  6. }
  7. delegate void CallBack (IntPtr param);
  8. [DllImport ("TestLib")]
  9. static extern void TestFunc (CallBack CB);
  10. [Monopinvokecallback (typeof (CallBack))]
  11. static void Callbackfunc (IntPtr param) {
  12. var p = (Parameter) marshal.ptrtostructure (param, typeof (Parameter));
  13. Debug.Log ("A:" + P.a + "B:" + p.b);
  14. }
  15. // Use this for initialization
  16. void Start () {
  17. TestFunc (Callbackfunc);
  18. }
  19. //Update is called once per frame
  20. void Update () {
  21. }
  22. }


The above is the unity script, output A, a, B. Notice the key.

[CSharp]View PlainCopy
    1. [Monopinvokecallback (typeof (CallBack))]

This tag cannot be successfully recalled without this tag.

With this method you can guarantee coding efficiency and execution efficiency, you do not need to carry out a variety of middle-tier packaging, do not need to turn the string, thanks to Mono's cross-platform mechanism, unity is only a few simple operations

It is also important to note that Android may need to compile a variety of corresponding. So, in fact, with androidstudio all of a sudden to compile and throw it to unity.

There is no need for this tag under the PC platform!

In fact, if not to protect the code, almost all unity development can be done in C #, C # functionality is strong enough, for Android Protection C # also mentioned in the previous article, although the individual does not know the security if the only hint is to use coroutine, Anti-compilation can't see its process, but I don't know if it's just moved somewhere else, and I'm personally putting a little bit of sensitive information in coroutine to prevent anti-compilation (though it might be spat)

Unity's cross-platform ultimate solution (Pc,ios,android-C + +) and platform support

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.