logo
Khóa học lập trình game server - smartfox - game đa người chơi Học lập trinh game online 3dvietpro Sửa chữa và thiết kế website .net
Welcome Guest! To enable all features please Đăng nhập or Đăng ký.

Thông báo

Icon
Error

Tùy chọn
Xem
Xem bài viết cuối Go to first unread
admin  
#1 Đã gửi : 23/05/2019 lúc 11:16:50(UTC)
admin

Danh hiệu: Administration

Nhóm:
Gia nhập: 19-08-2014(UTC)
Bài viết: 145

Được cảm ơn: 7 lần trong 6 bài viết

Trong code hướng dẫn này mình sẽ hướng dẫn các bạn hiểu được cách share ảnh lên mạng xã hội facebook và google bằng cách can thiệp vào thư viện của android và ios trên unity




Trích dẫn:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System.Runtime.InteropServices;
public class ShareScreenshot : MonoBehaviour
{

private bool isProcessing = false;
public float startX;
public float startY;
public int valueX;
public int valueY;

//Khai báo các thông tin cần share
public string ShareMessage = "Nội dụng share";
public string url= "Đường dẫn share";
public string subject= "Tên chủ đề.";


//Các bạn chỉ cần gọi tới phương thức này là unity sẽ chụp màn hình và share nội dung trên mạng xã hội
public void shareScreenshot()
{
if (!isProcessing)
StartCoroutine(captureScreenshot());
}
/* Hàm này sẽ xử lý chụp màn hình và share, nếu muốn share hình ảnh nào đó khác không phải chụp, các bạn chỉ cần xác định được đường dẫn qua biến destination là được.*/
public IEnumerator captureScreenshot()
{
isProcessing = true;
yield return new WaitForEndOfFrame();
//Xử lý chụp màn hình
Texture2D screenTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
screenTexture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height),0,0);
screenTexture.Apply();

byte[] dataToSave = screenTexture.EncodeToPNG();
string destination = Path.Combine(Application.persistentDataPath, System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");

File.WriteAllBytes(destination, dataToSave);

//Thực hiện share ở trên thiết bị android
#if UNITY_ANDROID
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");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + destination);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
intentObject.Call<AndroidJavaObject>("setType", "image/png");
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), ShareMessage);
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "YO! I Love Who Lurks!");
currentActivity.Call("startActivity", jChooser);
}
//Thực hiện share trên ios
#elif UNITY_IOS
// if(!isProcessing)
StartCoroutine( CallSocialShareRoutine(ShareMessage, subject, url,destination) );
#else
Debug.Log("No sharing set up for this platform.");
#endif

isProcessing = false;

}


#if UNITY_IOS
public struct ConfigStruct
{
public string title;
public string message;
}
[DllImport ("__Internal")] private static extern void showAlertMessage(ref ConfigStruct conf);
public struct SocialSharingStruct
{
public string text;
public string url;
public string image;
public string subject;
}
[DllImport ("__Internal")] private static extern void showSocialSharing(ref SocialSharingStruct conf);
public void CallSocialShare(string title, string message)
{
ConfigStruct conf = new ConfigStruct();
conf.title = title;
conf.message = message;
showAlertMessage(ref conf);
isProcessing = false;
}
public static void CallSocialShareAdvanced(string defaultTxt, string subject, string url, string img)
{
SocialSharingStruct conf = new SocialSharingStruct();
conf.text = defaultTxt;
conf.url = url;
conf.image = img;
conf.subject = subject;
showSocialSharing(ref conf);
}
void CallSocialShareRoutine(string ShareMessage, string subject, string url, string destination)
{
//isProcessing = true;
string screenShotPath =destination;
//Application.CaptureScreenshot(ScreenshotName);
//yield return new WaitForSeconds(1f);
CallSocialShareAdvanced(ShareMessage, subject, url, screenShotPath);
}
#endif
}


Code trên mình đã thực hiện test và chạy được, các bạn chỉ cần check tỉ mỉ là ok nhé



Admin.

Bạn bình luận ngay tại đây
Ai đang xem chủ đề này?
Guest
Di chuyển  
Bạn không thể tạo chủ đề mới trong diễn đàn này.
Bạn không thể trả lời chủ đề trong diễn đàn này.
Bạn không thể xóa bài của bạn trong diễn đàn này.
Bạn không thể sửa bài của bạn trong diễn đàn này.
Bạn không thể tạo bình chọn trong diễn đàn này.
Bạn không thể bỏ phiếu bình chọn trong diễn đàn này.

Powered by YAF 2.1.0 | YAF © 2003-2024, Yet Another Forum.NET
Thời gian xử lý trang này hết 0.089 giây.