1.0 只支持分享文字
1.1支持文字分享和图片文字组合分享
添加Test.cs 调用share的CallShare (stringhandline, stringsubject, stringtext, boolimage)方法
添需要分享截图的时候image为true,并且调用CallShare方法前调用ScreenShot截屏
//-----------------------------------------------------------------------------------------------------
玩过好多android游戏内都有分享功能,今天研究了一下,做成一个简单易用插件分享给大家,按菜单键调出分享窗口
图片:FC3C58782473D514FB034E2E8CC9D47F.png
图片:D7F2C382-F8D4-4C0A-9B2D-B29B2B06F721.png
设备上所有支持ACTION_SEND的应用都会在这个列表中显示,你可以通过这个列表邮件,微博,微信等让玩家分享应用内容
导入插件后,如上图所示一共就这么几个文件,
有一个简单的场景,可以直接使用还可以直接把Share预制直接拖到你 的场景中,在脚本中直接调用share的CallShare方法,设置需要分享的文字和图片//见demo场景中得用法
C# v1.1
using UnityEngine;
using System.Collections;
using System.IO;
public class Share : MonoBehaviour
{
public static string imagePath;
static AndroidJavaClass sharePluginClass;
static AndroidJavaClass unityPlayer;
static AndroidJavaObject currActivity;
private static Share mInstance;
public static Share instance {
get{ return mInstance;}
}
void Awake ()
{
mInstance = this;
}
void Start ()
{
imagePath = Application.persistentDataPath + "/HKeyGame.png";
sharePluginClass = new AndroidJavaClass ("com.ari.tool.UnityAndroidTool");
if (sharePluginClass == null) {
Debug.Log ("sharePluginClass is null");
} else {
Debug.Log ("sharePluginClass is not null");
}
unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
currActivity = unityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
}
public void CallShare (string handline, string subject, string text, bool image)
{
Debug.Log ("share call start : " + imagePath);
if (image) {
sharePluginClass.CallStatic ("share", new object[] {
handline,
subject,
text,
imagePath
});
} else {
sharePluginClass.CallStatic ("share", new object[] {
handline,
subject,
text,
""
});
}
Debug.Log ("share call end");
}
public void ScreenShot ()
{
StartCoroutine (GetCapture ());
}
IEnumerator GetCapture ()
{
yield return new WaitForEndOfFrame ();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
tex.ReadPixels (new Rect (0, 0, width, height), 0, 0, true);
byte[] imagebytes = tex.EncodeToPNG ();//转化为png图
tex.Compress (false);//对屏幕缓存进行压缩
// image.mainTexture = tex;//对屏幕缓存进行显示(缩略图)
File.WriteAllBytes (Application.persistentDataPath + "/HKeyGame.png", imagebytes);//存储png图
Debug.Log (Application.persistentDataPath);
}
}
C# v1.0