"Unity" sharesdk third-party login

Source: Internet
Author: User
Tags tojson

Use Weibo login as an example
    • Official Document http://wiki.mob.com/unity3d%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E6%8C%87%E5%8D%97/
    • Official demo Https://github.com/MobClub/New-Unity-For-ShareSDK
    • Video tutorial http://www.sikiedu.com/course/140
1, register the account number on the mob.com, add an application, add SHARESDK to the application. 2. Log in to the Weibo open platform and fill in the developer information. Add an app, determine the android package name, download the signature Generator tool (MD5 signature generator apk) provided by Weibo, and install it on your Android phone. Open the tool to enter the name of the Android package and get the signature string. Fill in the package name and signature. Enter the callback page URL in the OAuth2.0 authorization settings. 3. Open Unity to create a new project, the Player settings fill in the above package name, their own keystore and key. 4. Download sharesdk for Unityand import the Sharesdk.unitypackage in the package into the project. 5, a new scene named Init, an empty object (can be named Sharesdkmanager), dedicated to storing all the games in general (can cross the scene long, life cycle) data, start the game to enter the scene first, perform SHARESDK initialization, Then jump to another game scene. The scene will only enter once. 6. Attach the Share SDK script to the Sharesdkmanager object, and fill in the App Key,app secret and callback page path redirect Url (the default is the official debug account information) on the Weibo open platform. 7, open the test ID in the script's dev info Sina Weibo sinaweibo to make changes. Other no-tube, the default is filled with the official debug account information.

8. Create a new Sharesdkmanager script to mount theSharesdkmanager the object.
usingCn.sharesdk.unity3d;usingUnityengine; Public classSharesdkmanager:monobehaviour {Private StaticSharesdkmanager _instance;  Public StaticSharesdkmanager Instance {Get {            return_instance; }} [Hideininspector] PublicSHARESDK ssdk; voidStart () {_instance= This;        Dontdestroyonload (Gameobject); SSDK= gameobject.getcomponent<sharesdk>(); //The sharesdkutils.initsdk (appkey,appsecret) initialization operation has been performed in Sharesdk.awake (), where the write initialization is arbitrary//when initialization is complete, jump to the sceneUnityEngine.SceneManagement.SceneManager.LoadScene (1); }}
9. Create a new scene named Login, which is the login page scene that was entered after the init scene. Create a new script login and hang it on the newly created Loginmanager object. The "Log on with Sina Weibo" button in the scene triggers the corresponding method of the login script in the object. The process reference for the login method. Here is an example of a system that does not have a user system applied.
usingCn.sharesdk.unity3d;usingSystem.Collections;usingUnityengine;usingunityengine.scenemanagement; Public classLogin:monobehaviour {sharesdk ssdk; voidStart () {SSDK=ShareSDKManager.Instance.shareSDK; Ssdk.authhandler= Onauthresulthandler;//callback function for authorization result    }        //sign in with Sina Weibo     Public voidOnsinaloginbuttonclick () {if(SSDK. IsAuthorized (Platformtype.sinaweibo))//detects if the specified platform has been authorized        {            //Get authorization InformationUtility.writefile (Application.persistentdatapath,"AuthInfo.txt", Ssdk.            Getauthinfo (Platformtype.sinaweibo). ToJson ()); //go to the next sceneScenemanager.loadscene (2); }        Else //the specified platform has not been authorized to authorize it{ssdk.        Authorize (Platformtype.sinaweibo); }    }    /// <summary>    ///callback function for authorization result/// </summary>    /// <param name= "ReqID" ></param>    /// <param name= "state" >Authorization Status: Success, failure, cancellation</param>    /// <param name= "type" >License Platform Type</param>    /// <param name= "Data" >the returned data</param>    voidOnauthresulthandler (intReqID, responsestate State, Platformtype type, Hashtable data) {        Switch(state) { CaseResponsestate.begin: Break;  Caseresponsestate.success://data writes to fileUtility.writefile (Application.persistentdatapath,"AuthData.txt", Data.tojson ()); //Get authorization InformationUtility.writefile (Application.persistentdatapath,"AuthInfo.txt", Ssdk.                Getauthinfo (Platformtype.sinaweibo). ToJson ()); //go to the next sceneScenemanager.loadscene (2);  Break;  CaseResponsestate.fail: CaseResponsestate.cancel://failed or canceled authorization information to clear the specified platformSSDK.                Cancelauthorize (type);  Break;  CaseResponsestate.beginupload: Break; default:                 Break; }    }}
Write a tool class that reads and writes files.
usingSystem.IO;/// <summary>///a tool class that reads and writes files. ///the data that is returned when you view the SHARESDK authorization. /// </summary> Public Static classutility{ Public Static voidWriteFile (stringPathstringNamestringinfo)        {StreamWriter writer; FileInfo fi=NewFileInfo (path +"/"+name); Writer=fi.        CreateText (); Writer.        WriteLine (info); Writer.        Close (); Writer.    Dispose (); }         Public Static stringReadFile (stringPathstringname)        {StreamReader reader; FileInfo fi=NewFileInfo (path +"/"+name); Reader=fi.        OpenText (); stringinfo =Reader.        ReadToEnd (); Reader.        Close (); Reader.        Dispose (); returninfo; }}
9. Pack the APK and run it on the Android emulator. Click on the "sign in with Sina Weibo" button to see the effect: Jump to fill in the Micro-blog account and password, entered correctly will jump to the Weibo licensing page, click to determine the completion of authorization, the game into the next scene, complete the operation.Pit Point:
  • If the click button does not respond, go to the Weibo open platform test information-Add a test account, link to your own Weibo, because the developer authentication is not approved when the data cannot be obtained.
  • If you click the button after the page fallback, is unity in the sharesdk script of the microblogging app key and app secret is not filled in. (There is the default debug account data, note check).
  • Error "The site you are visiting has failed in the Weibo authentication errors number 21322 redirect address mismatch". Because the callback URL is not filled in the unity script, it is consistent with the address of the callback page entered in the microblogging open platform!

"Unity" sharesdk third-party login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.