码迷,mamicode.com
首页 > 移动开发 > 详细

Android应用程序中分享图片和文字给好友

时间:2015-06-17 15:23:18      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:unity   分享截图   

插件地址:点此下载插件包【Unitypackage格式】


配置文件中需要添加读写sdcard的权限 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

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


Android应用程序中分享图片和文字给好友

标签:unity   分享截图   

原文地址:http://blog.csdn.net/sinat_20559947/article/details/46533453

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!