Build Unity Project within Android and iOS
- Edit -> Project setting -> Other setting -> Target SDK -> Simulator SDK
- Edit -> Project setting -> Player -> uncheck Auto Graphics API add OpenCLES3(Deprecated) or OpenCLES2(Deprecated)
- File -> Build setting -> Add Open Scenes -> Build
iOS Simulator TargetSDK Setting:
- File -> Build setting -> Android platform -> Switch Platform
- File -> Build setting -> Add Open Scenes -> check Export Project -> Export
call UnityFramework
instance method directly.
[[UnityFramework getInstance] sendMessageToGOWithName: @"Game Object"
functionName:@"your function name"
message:@"your message"];
write plugin for iOS by extern "C" {}
expose it in Objective-C++ file (.mm file)
NativeCallProxy.h
#import <Foundation/Foundation.h>
// NativeCallsProtocol defines protocol with methods you want to be called from managed
@protocol NativeCallsProtocol
@required
- (void) showHostMainWindow:(NSString*)color;
// other methods
@end
__attribute__ ((visibility("default")))
@interface FrameworkLibAPI : NSObject
// call it any time after UnityFrameworkLoad to set object implementing NativeCallsProtocol methods
+(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi;
@end
NativeCallProxy.mm
#import <Foundation/Foundation.h>
#import "NativeCallProxy.h"
@implementation FrameworkLibAPI
id<NativeCallsProtocol> api = NULL;
+(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi
{
api = aApi;
}
@end
extern "C" {
void showHostMainWindow(const char* color) { return [api showHostMainWindow:[NSString stringWithUTF8String:color]]; }
}
C# usage example: call showHostMainWindow()
public class NativeAPI {
[DllImport("__Internal")]
public static extern void showHostMainWindow(string lastStringColor);
void showHostMainWindow()
{
#if UNITY_ANDROID
// call android
#elif UNITY_IOS
NativeAPI.showHostMainWindow(lastStringColor);
#endif
}
call OverrideUnityActivity`s instance method UnitySendMessage(String gameObj, String method, String arg){}
directly
UnitySendMessage("Cube", "ChangeColor", "yellow");
OverrideUnityActivity
is public abstract class extends UnityPlayerActivity
in unityLibray, you can define some method that unity call, for example: showMainActivity
public abstract class OverrideUnityActivity extends UnityPlayerActivity
{
public static OverrideUnityActivity instance = null;
protected void UnitySendMessage(String gameObj, String method, String arg) { mUnityPlayer.UnitySendMessage(gameObj,method,arg); }
protected FrameLayout getUnityFrameLayout() { return mUnityPlayer; }
abstract protected void showMainActivity(String setToColor);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
instance = this;
}
@Override
protected void onDestroy() {
super.onDestroy();
instance = null;
}
}
C# usage example: call showHostMainWindow()
try
{
AndroidJavaClass jc = new AndroidJavaClass("com.company.product.OverrideUnityActivity");
AndroidJavaObject overrideActivity = jc.GetStatic<AndroidJavaObject>("instance");
overrideActivity.Call("showMainActivity", lastStringColor);
} catch(Exception e)
{
appendToText("Exception during showHostMainWindow");
appendToText(e.Message);
}