c# - Unity Share 脚本 Android 到 iOS

标签 c# ios facebook twitter unity-game-engine

在 Android 上一切正常,但我不知道如何使其在 iOS 上工作,请帮忙。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System;
using System.Collections.Generic;

public class ShareMenu : MonoBehaviour
{   
    private bool isProcessing = false;
    public string AppLinkURL { get; set; }
    private string shareText = "Download This Game";
    private string gameLink = "Download the game on play store at " + "\nhttps://play.google.com/store/apps/details?id=com.CrazyDrivers";
    public void shareImage()
    { 
        if (!isProcessing)
            StartCoroutine(ShareScreenshot());
    }

    private IEnumerator ShareScreenshot()
    {
        isProcessing = true;
        yield return new WaitForEndOfFrame();

        string destination = Path.Combine(Application.persistentDataPath, System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");
        Debug.Log(destination);

        if (!Application.isEditor)
        {
            AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
            AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
            intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
            AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
            intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareText + gameLink);
            intentObject.Call<AndroidJavaObject>("setType", "text/plain");
            AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
            currentActivity.Call("startActivity", intentObject);

            isProcessing = false;
        }
    }  
}

你能帮我修改一下ios的代码吗 让我分享到 Facebook、Twitter、Whatsapp。因为在安卓上它运行得很好。我是编程新手。

最佳答案

我认为应用程序崩溃是因为您在 iOS 设备上使用了 AndroidJavaClass。 你可以试试这个。

#if !UNITY_EDITOR && UNITY_ANDROID
                AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
                AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
                intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
                AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
                intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareText + gameLink);
                intentObject.Call<AndroidJavaObject>("setType", "text/plain");
                AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
                currentActivity.Call("startActivity", intentObject);

                isProcessing = false;
#elif UNITY_IOS
                //put iOS share code here
#endif

如果您的应用程序停止崩溃(但它不会执行任何操作。因为我们只允许它在 Android 设备上运行)。那么,您可以寻找 iOS 实现。 对于 iOS 代码,您可以在此处查找更多信息 http://forum.unity3d.com/threads/is-it-really-that-hard-to-share-a-simple-score-on-facebook-and-twitter-natively.231390/

对于 facebook(仅限)你应该使用官方 Facebook Unity SDK https://developers.facebook.com/docs/unity/ https://developers.facebook.com/docs/unity/reference/current/FB.FeedShare

关于c# - Unity Share 脚本 Android 到 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708186/

相关文章:

c# - 如何在 C# 中从 MySQL 检索 tinyint 数据类型?

ios - 将目标添加到 UIButton 不起作用

iphone - 将带有动画的矢量图形嵌入到 iOS 原生应用程序中

php - Facebook stream.publish 的先决条件是什么?

c# - vba - 扫描文档中的宏并替换宏文本?

c# - Automapper - 动态类型映射

ios - 如何读取录制的音频(二进制)文件以在 iOS 中上传

c++ - Google 和 Facebook 使用哪些前端和后端技术?

facebook - 在没有 offline_access token 的情况下调用 facebook api

c# - 如何强制Windows Phone 8应用程序以浅色主题运行