+1

Cách chia sẻ facebook, twitter , Google Plus trong Unity sử dụng Utimate plugin

1 . Giới thiệu

Hôm nay tôi sẽ giới thiệu cho các bạn làm game Unity một plugin dùng để làm những công việc mà dùng Unity thuần rất khó làm đó là việc chia sẻ social trong game , đây là chức năng cần thiết trong các game mobile để đưa game của bạn tới nhiều người chơi hơn.Plugin này là Utimate Mobile nó là một asset có trên Unity store .Link tải trên asset store của Unity : https://www.assetstore.unity3d.com/en/#!/content/20152. Chú ý đây là bản mất phí nhưng thật sự đây là một gói rất tiện dụng và làm được rất nhiều việc ngoài share social nó còn làm được rất nhiều chức năng của một app native bạn sẽ không phải phàn nàn gì về chất lượng và hiệu quả của nó .

2 . Hướng dẫn

Trong phần này tôi sẽ hướng dẫn bạn dùng plugin Utimate để tạo nút chia sẻ facebook, twitter và Google plus trong game . Trước tiên bạn hãy tạo mới một project việc chắc ai cũng làm được rồi .Bước tiếp theo bạn sẽ tải gói Ultimate Mobile về và import vào project .

Sau khi import tất cả vào bạn nhìn trong tab Project -> Assets sẽ có những mục như sau

1.png

Bước tiếp theo bạn sẽ tạo một Scene mới đặt tên là SocialScene và tạo ba nút : ShareFacebook và ShareTwitter, ShareGooglePlus trong đó .

6.png

Tiếp theo ta tạo một script tên là MySocial.cs với nội dung như sau

using UnityEngine;
using System.Collections;

public class MySocial : MonoBehaviour {

	public Texture2D shareTexture;

	void Awake() {
		SA_StatusBar.text = "Social Sharing scene is loaded";
	}

	void Start()
	{

	}

    public void GoogleShare() {
		string Caption = "I just play game " + Constant.nameGame+"! Can you beat it? " + Constant.linkApp;
		AndroidSocialGate.StartShareIntent("Hello Share Intent", Caption, "com.google.android.apps.plus");
	}

    public void postTwitter()
	{
		StartCoroutine(PostTwitterScreenshot());
	}

	public void postFace()
	{
		string Caption = "I just play game " + Constant.nameGame+ "! Can you beat it? " + Constant.linkApp;;
		UM_ShareUtility.FacebookShare (Caption);
	}

	private IEnumerator PostTwitterScreenshot() {

		yield return new WaitForEndOfFrame();
		// Create a texture the size of the screen, RGB24 format
		int width = Screen.width;
		int height = Screen.height;
		Texture2D tex = new Texture2D( width, height, TextureFormat.RGB24, false );
		// Read screen contents into the texture
		tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
		tex.Apply();
		string Caption = "I just play game " + Constant.nameGame+ "! Can you beat it? " + Constant.linkApp;
		UM_ShareUtility.TwitterShare (Caption, tex);
		Destroy(tex);

	}

	private IEnumerator PostFBScreenshot() {

		yield return new WaitForEndOfFrame();
		// Create a texture the size of the screen, RGB24 format
		int width = Screen.width;
		int height = Screen.height;
		Texture2D tex = new Texture2D( width, height, TextureFormat.RGB24, false );
		// Read screen contents into the texture
		tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
		tex.Apply();
		string Caption = "I just pass level 9  in game " + Constant.nameGame+ "! Can you beat it? " + Constant.linkApp;
		UM_ShareUtility.FacebookShare (Caption, tex);
		Destroy(tex);
	}
}

Ở đây ta sẽ chia sẻ lên trang facebook , twitter, google plus của chúng ta nội dung Caption gồm một câu giới thiệu về game cùng với đường link để tải game và một ảnh chụp màn hình trong game ngay tại thời điểm đó .

Bước tiếp theo ta sẽ tạo ra một đối tượng rỗng trong scene vừa tạo đặt tên là Social và add script trên vào đối tượng đó .

3.png

Tiếp theo ta add script cho các button share facebook, twitter và google plus ở trên . 4.png 5.png

7.png

Tới đây bạn có thể build và chạy thử .

Plugin này còn làm được nhiều việc khác mà chỉ những app native làm được như chụp ảnh , hiển thị popup , mua bán trong game , notification , gửi email , google analitic ... nói chung là những gì Unity khó làm với một app native thì plugin này sẽ giúp bạn làm việc đó . Tôi sẽ có bài hướng dẫn dùng những chức năng đó ở bài viết sau .


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí