Unity Share Script Android to iOS

时间:2016-08-01 21:10:52

标签: c# ios facebook twitter unity3d

Everything works fine on Android but I dont know how to make it work for iOS, Please Help out.

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;
        }
    }  
}

Can you somehow help me to change this code for ios to let me share to facebook, twitter, whatsapp. Because on android it works very well. I am newbie to programming.

1 个答案:

答案 0 :(得分:1)

我认为应用程序崩溃是因为您在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