Commit a0fd194e cgx

解决Unity在静音模式下无法播放bug

1 个父辈 28bd1061
......@@ -14,6 +14,7 @@
#pragma mark - Unity
@property (nonatomic, strong) NSDictionary * appLaunchOpts;
@property (nonatomic, strong) UnityFramework* ufw;
/// 显示unity
- (void)showUnityView;
@end
......
......@@ -162,13 +162,10 @@
UnityFramework* UnityFrameworkLoad(void) {
NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Frameworks/UnityFramework.framework"];
NSBundle* bundle = [NSBundle bundleWithPath:bundlePath];
if ([bundle isLoaded] == false) [bundle load];
if ([bundle isLoaded] == false) { [bundle load]; }
UnityFramework* ufw = [bundle.principalClass getInstance];
if (![ufw appController])
{
[ufw setExecuteHeader: &_mh_execute_header];
}
UnityFramework *ufw = [bundle.principalClass getInstance];
if (![ufw appController]) { [ufw setExecuteHeader:&_mh_execute_header]; }
return ufw;
}
......@@ -180,17 +177,16 @@ extern int gArgc;
extern char** gArgv;
- (void)initUnity {
if([self unityIsInitialized]) {
[DSProgressHUD showDetailInfo:@"Unload Unity first"];
// unity如果初始化了,首先需要卸载unity
if ([self unityIsInitialized]) {
[DSProgressHUD showDetailInfo:@"Unity already initialized,please unload unity first"];
return;
}
[self setUfw: UnityFrameworkLoad()];
[self setUfw:UnityFrameworkLoad()];
[[self ufw] setDataBundleId:"com.unity3d.framework"];
[[self ufw] registerFrameworkListener:self];
[[self ufw] runEmbeddedWithArgc:gArgc argv:gArgv appLaunchOpts:self.appLaunchOpts];
[[[self ufw] appController] rootView];
}
- (void)showUnityView {
......@@ -201,15 +197,16 @@ extern char** gArgv;
#pragma mark - UnityFrameworkListener
- (void)unityDidUnload:(NSNotification*)notification {
DSLog(@"unityDidUnloaded called");
DSLog(@"unityDidUnload");
[self.window makeKeyAndVisible];
[[self ufw] unregisterFrameworkListener:self];
[self setUfw: nil];
[self.window makeKeyAndVisible];
}
- (void)unityDidQuit:(NSNotification *)notification {
DSLog(@"========== %s ============", __func__);
}
- (void)applicationWillResignActive:(UIApplication *)application { [[[self ufw] appController] applicationWillResignActive: application]; }
- (void)applicationDidEnterBackground:(UIApplication *)application { [[[self ufw] appController] applicationDidEnterBackground: application]; }
- (void)applicationWillEnterForeground:(UIApplication *)application { [[[self ufw] appController] applicationWillEnterForeground: application]; }
- (void)applicationDidBecomeActive:(UIApplication *)application { [[[self ufw] appController] applicationDidBecomeActive: application]; }
- (void)applicationWillTerminate:(UIApplication *)application { [[[self ufw] appController] applicationWillTerminate: application]; }
@end
......@@ -36,6 +36,7 @@ __attribute__ ((visibility("default")))
id<RenderPluginDelegate> _renderDelegate;
}
@property (strong, nonatomic) UIViewController *vc;
// override it to add your render plugin delegate
- (void)shouldAttachRenderDelegate;
......
......@@ -132,6 +132,7 @@ NSInteger _forceInterfaceOrientationMask = 0;
UnitySetPlayerFocus(1);
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive: YES error: nil];
[audioSession addObserver: self forKeyPath: @"outputVolume" options: 0 context: nil];
UnityUpdateMuteState([audioSession outputVolume] < 0.01f ? 1 : 0);
......@@ -378,11 +379,67 @@ extern "C" void UnityCleanupTrampoline()
{
_startUnityScheduled = true;
[self performSelector: @selector(startUnity:) withObject: application afterDelay: 0];
// 替换startUnity:为startSelfIOSView
// [self performSelector: @selector(startSelfIOSView) withObject: application afterDelay: 0];
}
_didResignActive = false;
}
// 创建原生页面和控件,自己控制唤起Unity时机
- (void)startSelfIOSView
{
UIViewController *vc = [[UIViewController alloc] init];
vc.view.frame = [UIScreen mainScreen].bounds;
vc.view.backgroundColor = [UIColor blackColor];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
btn.backgroundColor = [UIColor blueColor];
[btn setTitle:@"跳转到Unity界面" forState:UIControlStateNormal];
// 按钮被点击时唤起Unity项目
[btn addTarget:self action:@selector(startUnity:) forControlEvents:UIControlEventTouchUpInside];
[vc.view addSubview:btn];
self.vc = vc;
[_window addSubview:vc.view];
[self addGLViewButton];
}
// 在UnityGetGLView上添加按钮
- (void)addGLViewButton
{
// 添加右旋按钮
UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, 100, 30)];
rightBtn.backgroundColor = [UIColor whiteColor];
[rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[rightBtn setTitle:@"向右旋转" forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(turnRight) forControlEvents:UIControlEventTouchUpInside];
// 添加左旋按钮
UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, 100, 30)];
leftBtn.backgroundColor = [UIColor whiteColor];
[leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[leftBtn setTitle:@"向左旋转" forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(turnLeft) forControlEvents:UIControlEventTouchUpInside];
// 在Unity界面添加按钮
[UnityGetGLViewController().view addSubview:rightBtn];
[UnityGetGLViewController().view addSubview:leftBtn];
}
// 左旋按钮响应事件
- (void)turnRight
{
const char* str = [[NSString stringWithFormat:@"向右"] UTF8String];
UnitySendMessage("_bridgeManager", "turnRight", str);
}
// 右旋按钮响应事件
- (void)turnLeft
{
const char* str = [[NSString stringWithFormat:@"向左"] UTF8String];
UnitySendMessage("_bridgeManager", "turnLeft", str);
}
- (void)updateUnityAudioOutput
{
UnityUpdateAudioOutputState();
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!