- Home /
Javascript in Unity, Including iPhone API's (Objective C?)
Hi guys,
I'm just getting started in unity, have been experimenting/following tutorials etc, and was just wondering something.
I'd like to get started in making iPhone games using Unity, but I have been learning to script in JavaScript. Apparently iPhone uses Objective C for its API's..?
I was wondering how it would work if I want to access iPhone specific features such as the new Gaming Network through unity script. Can these functions still be called?
Also, I've only got Unity Basic, and was intending on starting development on a game using it, and then purchase the Unity iPhone licence and port it to iPhone. Is there anything I should not include in my scripting or in the game in general, to make porting later easier?
Sorry if these seem like dumb/n00b questions! I'm learning all I can through searching out on my own, but some things like these I'm still unclear on since I can't test out myself at this point.
Thanks in advance! This community is fantastic. Been such a big help to me so far.
Answer by Eric5h5 · Apr 10, 2010 at 01:32 PM
The classes for some iPhone APIs (touch/accelerometer/keyboard/etc.) are included in both Unity iPhone Basic and Advanced. Also you can use any iPhone API, not just those that Unity supports. But for those you have to program in XCode, and make some kind of bridge to get the results to the Unity side. In this case it's fastest and most convenient to use plugins with iPhone Advanced, but there are other ways (typically passing data using PlayerPrefs).
As far as Javascript programming goes, Unity iPhone doesn't allow dynamic typing. If you put "#pragma strict" at the top of your scripts in regular Unity, you get the same effect, so it's a good idea to always include this if you intend to use Unity iPhone. 99% of your code won't change, but there are a few areas where you have to explicitly type things, where normally you're probably using dynamic typing (usually with GetComponent).
Oh, I see. I'll read up on PlayerPrefs. Is there anything else I should check out before diving into iPhone?
Thanks!
@$$anonymous$$ooseTrap: $$anonymous$$ake sure you read the iPhone-specific parts of the Unity iPhone docs. They aren't available on-line, so you have to read them locally...they come with Unity iPhone and initially look the same as the standard Unity docs, but there's some important stuff in them. Also I edited my answer to include some info about Javascript.
@Eric5h5 Thanks Eric! Just a question about Typing. I'm learning these program$$anonymous$$g concepts as I go, have I got this correct?
Static Typing: Declare variables and their type at the start of the script Dynamic Typing: Declare the variables and their type as they are needed
Is this correct, or have I got it all wrong? I just did some reading and that's what I got from it... Have I misunderstood?
Thank you for your help.
@$$anonymous$$ooseTrap: Actually no, dynamic typing means that the type is figured out at runtime. For example, var my$$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh;
will work with regular Unity because it figures the type for you at runtime, but it won't work with #pragma strict or Unity iPhone, because "mesh" isn't a member of Component (which is what GetComponent returns). Ins$$anonymous$$d you have to do var mesh = (GetComponent($$anonymous$$eshFilter) as $$anonymous$$eshFilter).mesh;
in order to cast the GetComponent call to the correct type. Or two lines: var mf : $$anonymous$$eshFilter = GetComponent($$anonymous$$eshFilter); var my$$anonymous$$esh = mf.mesh;
actually this does not seem to work anymore. nowadays you have to use GetComponent.<$$anonymous$$eshFilter>().mesh
Answer by Ashkan_gc · Apr 10, 2010 at 01:02 PM
you can only use iphone apis that unity supports. unity has some classes for iphone apis in it's iphone version but they are not available in the license that you currently have. try to write fast code and read th iphone tutorial in this page also take a look at this page. at the end unity gets your project and compiles it into ARM assembly code with Xcode. if you have a mac computer just download the 30 day trial of unity iphone and see how things work. reading the tutorial can help even if you don't have access to a mac computer. the most important things that you should think about them are your assets. they should be low resolution/polygon. don't use too many rigidbodies or too many particles.
Awesome, cheers Ashkan! I'll check out those links and trial of Unity iPhone.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Unity iPhone and Unity Desktop Scripting Differences 2 Answers
TPS Camera with Joystick (Android) 0 Answers
Flipping textures 0 Answers