unity - Android Share ScreenShot Image 유니티 안드로이드 스크린샷 이미지 공유



java..

C# - Plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class PluginTest : MonoBehaviour {
 
    const string pluginName = "com.cwgtech.unity.MyPlugin";
 
    class AlertViewCallback : AndroidJavaProxy
    {
        private System.Action<int> alertHandler;
 
        public AlertViewCallback(System.Action<int>alertHandlerIn) : base (pluginName + "$AlertViewCallback")
        {
            alertHandler = alertHandlerIn;
        }
        public void onButtonTapped(int index)
        {
            Debug.Log("Button tapped: " + index);
            if (alertHandler != null)
                alertHandler(index);
        }
    }
 
    class ShareImageCallback: AndroidJavaProxy
    {
        private System.Action<int> shareHandler;
        public ShareImageCallback(System.Action<int>shareHandlerIn) : base (pluginName + "$ShareImageCallback")
        {
            shareHandler = shareHandlerIn;
        }
        public void onShareComplete(int result)
        {
            Debug.Log("ShareComplete:" + result);
            isSharingScreenShot = false;
            if (shareHandler != null)
                shareHandler(result);
        }
    }
 
    static AndroidJavaClass _pluginClass;
    static AndroidJavaObject _pluginInstance;
 
    public Button shareButton;
    public Text timeStamp;
    static bool isSharingScreenShot;
 
    public static AndroidJavaClass PluginClass
    {
        get {
            if (_pluginClass==null)
            {
                _pluginClass = new AndroidJavaClass(pluginName);
                AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity");
                _pluginClass.SetStatic<AndroidJavaObject>("mainActivity",activity);
            }
            return _pluginClass;
        }
    }
 
    public static AndroidJavaObject PluginInstance
    {
        get {
            if (_pluginInstance==null)
            {
                _pluginInstance = PluginClass.CallStatic<AndroidJavaObject>("getInstance");
            }
            return _pluginInstance;
        }
    }
 
    // Use this for initialization
    void Start () {
 
        Debug.Log("Elapsed Time: " + getElapsedTime());
        if (timeStamp != null)
            timeStamp.gameObject.SetActive(false);
        
    }
 
 
    double getElapsedTime()
    {
        if (Application.platform == RuntimePlatform.Android)
            return PluginInstance.Call<double>("getElapsedTime");
        Debug.LogWarning("Wrong platform");
        return 0;
    }
 
    void showAlertDialog(string[] strings, System.Action<int>handler = null)
    {
        if (strings.Length<3)
        {
            Debug.LogError("AlertView requires at least 3 strings");
            return;
        }
 
        if (Application.platform == RuntimePlatform.Android)
            PluginInstance.Call("showAlertView"new object[] { strings, new AlertViewCallback(handler) });
        else
            Debug.LogWarning("AlertView not supported on this platform");
    }
 
    public void ShareButtonTapped()
    {
        if (shareButton != null)
            shareButton.gameObject.SetActive(false);
        if (timeStamp!=null)
        {
            timeStamp.text = System.DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
            timeStamp.gameObject.SetActive(true);
        }
        ShareScreenShot(Application.productName + " screenshot", (int result) => {
            Debug.Log("Share complete with: " + result);
            showAlertDialog(new string[] { "Share Complete""Share completed with: " + result, "OK" });
            if (shareButton != null)
                shareButton.gameObject.SetActive(true);
            if (timeStamp != null)
                timeStamp.gameObject.SetActive(false);
        });
    }
 
    public void ShareScreenShot(string caption, System.Action<int> shareComplete)
    {
        if (isSharingScreenShot)
        {
            Debug.LogError("Already sharing screenshot - aborting");
            return;
        }
        isSharingScreenShot = true;
        StartCoroutine(waitForEndOfFrame(caption, shareComplete));
    }
 
    IEnumerator waitForEndOfFrame(string caption, System.Action<int> shareComplete)
    {
        yield return new WaitForEndOfFrame();
        Texture2D image = ScreenCapture.CaptureScreenshotAsTexture();
        Debug.Log("Image size: " + image.width + " x " + image.height);
        byte[] imagePNG = image.EncodeToPNG();
        Debug.Log("PNG size: " + imagePNG.Length);
        if (Application.platform == RuntimePlatform.Android)
        {
            PluginInstance.Call("shareImage"new object[] { imagePNG, caption, new ShareImageCallback(shareComplete) });
        }
        Object.Destroy(image);
    }
 
}
 
cs


원본 : https://www.youtube.com/watch?v=EkF1pB1Ut-M

https://github.com/cwgtech/AndroidUnityShareScreenShot

댓글