小屋創作

日誌2020-07-08 16:32

Cocos Creator 2.3.3 多平台動態螢幕轉向教學

作者:Majitoo

Cocos Creator有個好處是可以呼叫Andriod(Java)與IOS(Object-C),原生專案的靜態方法.

官方文檔寫的蠻清楚的可以參考下面兩篇文章
呼叫Java參考文章
http://docs.cocos.com/creator/manual/zh/advanced-topics/java-reflection.html
呼叫Object-C參考文章
http://docs.cocos.com/creator/manual/zh/advanced-topics/oc-reflection.html

Cocos Creator端:
首先我有寫一個函數,可於JS中呼叫來達到動態轉螢幕的效果,
參數傳入true時,會強制把畫面轉成豎屏,
參數傳入false時,會強制把畫面轉成橫屏.
    // true 豎屏
    // false 横屏
    changeOrientation(isPortrait){
        console.log("ts changeOrientation:",isPortrait)
        //網頁端
        let w = cc.view.getFrameSize().width;
        let h = cc.view.getFrameSize().height;
        if(isPortrait)
            cc.view.setFrameSize(w>h?h:w,w>h?w:h);
        else
            cc.view.setFrameSize(w>h?w:h,w>h?h:w);
    
        //原生端
        if (cc.sys.os == cc.sys.OS_IOS)
            jsb.reflection.callStaticMethod("AppController","setOrientation:",isPortrait.toString());
        else if (cc.sys.os == cc.sys.OS_ANDROID)
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "changeOrientation", "(Z)V", isPortrait);
    }

接著先說明Android Java專案要加的靜態函數,
首先打開Build資料夾下得proj.android-studio可利用Android Studio打開,
若沒有出現Build資料夾,請先在專案發布的地方,點擊構建按鈕一次.

Android端:
首先打開Android Studio
點開 : 專案名/java/org.cocos2dx.javascript/AppActivity.java Class中貼上
public static void changeOrientation(boolean isPortrait) {
    if (isPortrait)
        instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    else
        instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

接著在檔案最上方添加
    import android.content.pm.ActivityInfo;

並且在Class中宣告成員變數  
    public static AppActivity instance;

接著在onCreate函數結尾貼上
    instance=this;

Android版本即可完成動態轉螢幕效果


IOS端:
找到build資料夾下的proj.ios_mac,並且用xcode打開專案,
xcode打開後找到AppController.mm並添加上下列代碼.
static bool g_bIsPortrait = false;
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
    if (g_bIsPortrait == false) {
        //横屏
        return UIInterfaceOrientationMaskLandscape;
    }else{
        //竖屏
        return UIInterfaceOrientationMaskPortrait;
        
    }
}
+(void)setOrientation:(NSString *)isPortrait{
    bool bIsPortrait=nullptr;
    if([isPortrait isEqualToString:@"true"])
        bIsPortrait=true;
    else
        bIsPortrait=false;
    
    if(g_bIsPortrait == bIsPortrait){
        return ;
    }
    g_bIsPortrait = bIsPortrait;
    UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationUnknown;
    if(bIsPortrait){
        interfaceOrientation =UIInterfaceOrientationPortrait;
    }
    else{
        interfaceOrientation =UIInterfaceOrientationLandscapeRight;
    }
    NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

    [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

    NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];

    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
IOS版本即可完成動態轉螢幕效果,
若IOS轉向時出現自動放大螢幕的問題時,
可參考我之前寫的文章.
https://home.gamer.com.tw/creationDetail.php?sn=4842383


最後附上此項目此Demo項目範例
https://github.com/jimmyy512/Cocos233NativeOrientation

此Demo我已經在build-templates資料夾添加要改的原生專案的Code,
只要點擊構建就能看到我更改的Java與Object-C的程式碼.


1

0

LINE 分享

相關創作

Cocos Creator 2.3.2 SDKBOX FB登入 第三方支付 Android打包失敗紀錄串

Cocos Creator 2.3.3 IOS螢幕轉向後會放大的問題

Cocos Creator 2.3.2 SDKBOX FaceBook第三方登入串接教學

留言

開啟 APP

face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】