Crossplatform code - How?

My project is for iPhone and PC/Web. The iPhoneKeyboard class is not define on PC/Web. Even though I can still run in the editor, it won’t let me build PC/Web version unless I remove iPhoneKeyboard.

What is the best way around this so that I don’t have to switch back and forth between two set of codes?

Thanks
cybie

There are a couple options I know of:

one is the Preprocessor commands:

		static void SomeFunction()
		{
#if (UNITY_IPHONE)
				DoThisOnIphone();
#else
				OtherwiseDoThis();
#endif
		}

You can wrap #Define around anything and when compiling it ignores it as if nothing was written there.

Your available Unity Defines are here:
http://unity3d.com/support/documentation/Manual/Platform%20Dependent%20Compilation.html

And your available Preprocessor commands:

The second usable method is to use System.Diagnostics.ConditionalAttribute

[System.Diagnostics.Conditional("UNITY_IPHONE")]
	public static void DoIPhoneThing()
	{
		//This only works on IPhone
	}
	
	[System.Diagnostics.Conditional("UNITY_EDITOR")]
	public static void DoEditorThing()
	{
		//Only called if Editor exists
	}

The Conditional attribute is interesting in that it will remove the Function declaration AND remove each individual call, meaning that if you don’t want the function you don’t actually have to #if define out each individual call to that function.

The ConditionalAttribute reference:

Thanks Ntero.

I did not know about the .Net Conditional.
Have you noticed any Negatives about using the Conditional Method vs the Define Method?

With Conditional you cannot use !, so I cannot say NOT Unity_Engine.
Overall I prefer it, as it can make sure I handle all platform specific stripping from the function declaration as opposed to scattered about the project in the function calls. But sometimes I have to do some preprocessor stuff at the top of that particular file

#if (!Unity_IPhone || Unity_Engine)
#define UseCalls
#endif
//Namespace and Class Declarations here:
[System.Diagnostics.Conditional("UseCalls")]
public static void DoNonIPhoneThing()
{
	//These calls exist on all platforms but on Device on iPhone (iPhone editor will still use this)
}

C# defines within the scripts are per script, but it does mean for more complex conditionals you need an extra define to be able to use Conditional Properly.

But I still prefer it to having #if scattered all willy nilly around the code base, it’s just asking for one place to be forgotten.

Interesting stuff!

Thanks Ntero!

Hi all,

This sounds exactly like what I am looking for but one newbie question! :slight_smile:

What happens if I wish to run in the editor but use unity remote to control the input i.e. simulate iPhone play but without downloading too the iPhone. I hope that’s clear :slight_smile:
How do I use conditionals for that??

i.e. in editor but ‘using’ remote (hence need iPhone input)…

Cheers

I think this was fixed as far as I know, for 3.4

Hi Hippocoder,

Thanks for that. So what do I #define to get what I need? - I guess that’s what I am asking.
It can’t be #if UNITY_EDITOR and it can’t be #if UNITY_IPHONE (unless I guess the UNITY_EDITOR automatically takes input from remote if present…??

Cheers

Nope it doesn’t :slight_smile:

I guess I ‘want’ an #if UNITY_EDITOR UNITY_REMOTE …

How can i achieve this?

Thanks

Hey all,

No-one else having these issues with Remote?. i.e. how to compile an ‘editor’ version but use remote as though it’s on a real iPhone.
Is there no way to do a build for this?, or at the very least in a script be able to detect the remote - then of course the script would be able to go into ‘iPhone’ input mode.
Otherwise it’s download to the real device each time which is a pain :-(((

Cheers

EDIT:
I guess the only solution is to #define USING_UNITY_REMOTE and recompile each time depending on where I wish to run in the editor (with no remote) or run in the editor but use remote as though a real iPhone input.
Will try :slight_smile: