分享按钮到社交媒体Unity(Android) [英] Share Button to social Media Unity (Android)

查看:156
本文介绍了分享按钮到社交媒体Unity(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Unity中创建一个共享按钮.只需按下一个按钮,它就会显示手机上安装的社交媒体应用程序,并允许用户共享.我一直在寻找有关如何创建Facebook分享按钮或Twitter分享按钮的教程.但是我只想创建一个简单的共享按钮,使您可以与每个社交媒体应用程序共享.这是一个示例:

I am trying to create a share button in Unity. Just one button that when is pressed it will show the social media applications that is installed on your phone, and allows the user to share. I keep finding tutorials over how to create a facebook share button or a twitter share button. But I just want to create a simple share button that allows you to share with every social media application. Here is an example:

示例

我找到了一些资产,但不确定它们是否能正常工作.

I found a few assets, but not for sure if they will work right.

资产: https://www.assetstore.unity3d.com/en/#!/content/37320
此资产允许您共享图像,我不需要共享图像,仅需要文本即可.但是我认为只需修改文本就可以了.

Assets: https://www.assetstore.unity3d.com/en/#!/content/37320
This asset allows you to share an image, I don't need to share an image, just text. But I thought it wouldn't be hard to modify it and only do text.

推荐答案

有两种方法.
1.创建一个本机插件,在Unity中编写包装器代码以调用本机代码(可能是调用本机函数的最广泛使用的方法)
2.完全用Unity编写代码,并使用AndroidJavaObject调用函数.

There's two ways of doing this.
1. Create a native plugin, write wrapper code in Unity to call the native code (Probably the most widely used way to call native functions)
2. Write the code entirely in Unity, and use AndroidJavaObject to invoke the functions.


选项1-本机Java代码+ Unity包装器
这是我在SO上找到的用于共享代码的链接. 这是链接,指向我有关插件的较早答案之一.您可以在那里修改代码以适合您的需求.


Option 1 - Native Java Code + Unity Wrapper
Here's a link I found on SO for the code for Sharing.
Here's a link to one of my older answers about plugins. You can modify the code there to fit your needs.

选项2-无本地代码.
这种方式更有趣.我们使用Unity的AndroidJavaClass& AndroidJavaObject完全消除了JAR的需要.只需将以下代码粘贴在C#脚本中,然后调用该函数即可. (注意,我没有尝试过此代码,可能有错误.如果有,请告诉我,我将编辑我的回复)

Option 2 - No native code.
This way is a little more interesting. We use Unity's AndroidJavaClass & AndroidJavaObject to eliminate the need of JARs altogether. Just stick the below code in a C# script, and call the function. (NOTE, I haven't tried this code, there may be errors. If there are, let me know and I'll edit my response)

private static AndroidJavaObject activity = null;

private static void CreateActivity () {
    #if UNITY_ANDROID && !UNITY_EDITOR
    if(activity == null)
        activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").
            GetStatic<AndroidJavaObject>("currentActivity");
    #endif

}

public static void ShareActivity (string title, string subject, string body) {
    CreateActivity();
    AndroidJavaObject sharingIntent = new AndroidJavaObject("android.content.Intent", "android.intent.action.SEND")
                      .Call<AndroidJavaObject>("setType", "text/plain")
                      .Call<AndroidJavaObject>("putExtra", "android.intent.extra.TEXT", body)
                      .Call<AndroidJavaObject>("putExtra", "android.intent.extra.SUBJECT", subject);

    AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", activity)
                      .CallStatic<AndroidJavaObject>("createChooser", sharingIntent, title);
    activity.Call("startActivity", intent);
}

别忘了将活动添加到您的AndroidManifest.xml中!

Don't forget to add the activity to your AndroidManifest.xml!

这篇关于分享按钮到社交媒体Unity(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆