iOS and Unity3d simple interaction

Source: Internet
Author: User
Tags instance method
I. Xiao Kee

Recently, the company has a demand, need to use Unity3d to build 3D model, and realize switching function, because do not long development time, and did not contact Unity3d, so can only confused to find information on the Internet, but also saw the rain Pine Momo article, but are a few years ago, some things have not been applied, Can only look at the research, today finally a bit of a prospect, you can unity3d exported iOS engineering files on the basis of the 3D modeling of the page to modify, and can perform some operations.


two. Implementation Details

Unity3d version: 5.3.4

Xcode version: 7.3.1

The first is the need to do unity3d the corresponding model to bind the script, and tell you the model or the name of the object to be manipulated and the name of the method to invoke, here because I want to achieve is to switch the camera angle, so the name is Mycamera (I hope the hump named Mycamera, Unfortunately Unity3d there is not my writing, do not care about these details, their own attention is good, the export time in the playersetting to do a variety of settings, choose to use Simulator debugging, SDK version of what, here is not to repeat.

Export Package If it is compressed, then after the decompression need to wait a little while to see the folder in the Xcodeproj file, do not know what the reason may be too large file bar ...

Open xcodeproj file, first compile a try, very likely to report a mistake
Compile an error

It means that the mapfileparser.sh file does not have permission to execute, so we go to the midrange, giving this file permission
Give mapfileparser.sh permission

In this way, the error can be successfully resolved, run again there will be no processing of the original unity3d of the content of the
Like the 3D model of this cup.

The file compiles and runs, and we can proceed to the next step.

We add our own files in the exported project Myviewinit
Add your own files

Write the code you want to implement in the file
MyViewInit.h
Myviewinit.m
Myviewinit.m

When a simple interface is set up, we need to test if we can add to the U3D model interface, find the Classes folder in the Engineering folder, click Expand, Find Unityappcontroller.mm file, which is the startup file of U3d project.
Find unityappcontroller.mm File

In the top section of the file, import the header file for your own written file

#import "MyViewInit.h"

Find a way
Didfinishlaunching

Add code to lay out the UI at the bottom of the method
Laying out the UI

where Unitygetglviewcontroller () is the root view controller in the U3D model view

(This may need to be changed in older versions of Unity3d)

Then you can click to run the test, no accident, you can display in the model view of the
View after adding a UI control

How, the effect is immediate.

The most important step below is to send a message to the U3D model, return to the MYVIEWINIT.M file, and complete the Click event added for the two button, and don't forget the. h File Declaration
Implement button click event

In order to prevent a small accident can not find the error, you may first print here, to see if the button is responding to click events, if you can successfully print, it is all right.

Here's a point, Momo in one of his articles about this, he is using inherited from Uiviewcontroller files, and using object methods to write UI and click events, do not know why, add UI space can be displayed, But the button does not respond to the Click event, I still do not delve into the cause, pending updates.

In the Click event, use the U3d already written method

Unitysendmessage ("Mycamera", "Changemodel", "");

The first parameter fills in the object or model name of the accepted method.

The second parameter fills in the method name.

The third parameter fill in the parameters to Pass, not to pass the parameter directly write "", here can not write nil, will crash.

Run it again, it's OK, here's what I'm going to do is click the button to switch the model

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------

March 10, 2017 09:49:20 update

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------

A lot of friends will ask, the Click event you added does not respond, the instance method does not respond, but the class method responds. This is actually when the program is running, the view controller you added the button is replaced by the U3d system method, which causes the button to be released, so there is definitely no event response, the part of the annotation below is the U3D system replaces your Viewcontroller method.


So you want to make sure that the view controller is not freed, and you can solve it by adding a static when you create it.


Need demo can add I welcome support

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.