--- title: 'loadInstanceObj' disqus: hackmd tags: 'so' --- ## **Read Instancified Objects** How to read an instancified object’s data. Click Item. ![](https://i.imgur.com/ttfX3A2.jpg) Click GetItemInstance to see Item’s ID and attribute’s name and value. ![](https://i.imgur.com/kzhzJzD.jpg) p.s. When an Object is not instancified, the suggestion is to execute the “object-instancifying” example first. And then, learn to read instancified objects. In this example, for an Object that is not instancified, even when the code is 0, it will be returned in CallBack. Please pay extra attention to this. ``` C# using UnityEngine; using System.Collections; public class Sample_GetItemClass : MonoBehaviour { /******************************************************************* * Copyright © 2022 SPKITA Game Cloud. All Rights Reserved * *******************************************************************/ //enter the gguid shown in the developer backstage here private const string gguid = ""; //enter the item iguid shown in the developer backstage here private const string item_iguid = ""; //enter the verification shown in the developer backstage here byte[] certificate = {}; //declare CloudGame and this Class will be in charge of the communication with Cloud Server public CloudGame ag = null; string userAcc; //user’s account string userPwd; //user’s password string _itemid; /the instancified object’s ID void Start() { CloudSystem.UnityEnvironment(); CloudSystem.ServerProvider("sgc-api-us.spkita.com"); // set the server } void OnApplicationQuit() { //When the program is closed, the user’s account will not be logged out. Here we can add a mechanism that allows automatic logging out. if(ag!=null) ag.Dispose(); } public void LoginBtnClick() { userAcc = login_input_acc.text; userPwd = login_input_pwd.text; //the parameters needed when logging in, which are (account, password, gguid, certificate) ag = new CloudGame(userAcc, userPwd, gguid, certificate); // designate the way of management and start the detection on whether the connection is successful ag.onCompletion += CloudLaunch; ag.UnityLaunch(); } /******************************************************************* // After CloudGame finishes its execution, in CallBack there will be a message about whether the login is successful. // When the code is 0, the login succeeds and when the code is not 0, the login fails. // refer the errors in login to Error Code // *******************************************************************/ void CloudLaunch(int code, CloudGame game) { if (code == 0) { Debug.Log("Login Success! My poid : " + ag.poid + " NickName : " + ag.nickname); } else { Debug.LogWarning("Login Fail! Code : " + code); } } /******************************************************************* * Reading an instacified object *******************************************************************/ public void GetItemInstanceBtn() { CloudItem.GetItemInstance(ag, item_iguid, CB_GetItemInstance, null); } /******************************************************************* * When the code is 0, the reading succeeds and when the code is not 0, the reading fails. *******************************************************************/ void CB_GetItemInstance(int code, object obj, object token) { if (code == 0) { List<Hashtable> list = obj as List<Hashtable>; if (list.Count == 0) //the length is 0, which means this Item has not been instancified yet { NewItemInstanceBtn();//if the item has not been instancified yet, let’s instancify it first return; } for (int i = 0; i < list.Count; i++) { //obtain the data according to the usage requirements //Debug.Log("iguid : " + list[i]["iguid"].ToString()); //Debug.Log("id : " + list[i]["id"].ToString()); //Debug.Log("name : " + list[i]["name"].ToString()); //Debug.Log("now : " + list[i]["now"].ToString()); //Debug.Log("expire : " + list[i]["expire"].ToString()); item_MsgText.text += list[i]["id"].ToString() + "\n"; Hashtable btb = list[i]["attr"] as Hashtable; foreach (DictionaryEntry itemm in btb) { Hashtable attr = itemm.Value as Hashtable; item_MsgText.text += attr["name"].ToString() + attr["value"].ToString() + "\n"; } } } else { item_MsgText.text += "GetItemInstance Fail! Code : " + code + "\n"; } } /******************************************************************* * Instancify an object *******************************************************************/ public void NewItemInstanceBtn() { CloudItem.NewItemInstance(ag, item_iguid, CB_NewItemInstance, null); } /******************************************************************* * When the code is 0, the instancification succeeds and when the code is not 0, the instancification fails. *******************************************************************/ void CB_NewItemInstance(int code, object data, object token) { if (code == 0) { _itemid = data.ToString(); // Record my instacified ID to use it when writing in data in the future. item_MsgText.text += "ItemInstance Success! \nItem ID = " + data + "\n"; } else { item_MsgText.text += "ItemInstance Fail! Code : " + code + " \n"; } } } ```