Resources






Please refer to Github link below for latest update on this

https://github.com/ixabhay/UltimateUnityCheatSheet





What's in there?

- Drawing Board Drag for moving objects while holding them, zooming in using mouse scroll and pinch zoom for touch devices.

- SimpleAES for encrypting and decrypting content.

- Orientation Manager for forcing a certain orientation on Android Devices

- Photon Connect contains a very robust Photon Matchmaking Example using Hashtable for Room Properties and Player Properties.

-SSUpload for taking screenshot of unity screen - flipping the textures and uploading screenshot using REST API.



ULTIMATE UNITY CHEAT SHEET


########################################################
########## for interviewing candidates #################
########################################################

rate your experience with Unity3D out of 10
rate your experience with NodeJS out of 10
rate your experience with SocketIO out of 10

#UNITY {
define PREFABS
unity application life cycle
define inspector tab
in a scene there is a button and a function to be executed and NOTHING ELSE AT ALL
> will it work?
your approach to build a map that follow a particular theme
how would you move an object from position A to B without update, fixed update or late update
teen patti how will you make user look like its sitting at the center of table
photon hash table define
> how will you sync room properties 
> how will you sync player properties
how would you execute a method on Player B's system from Player A's system > photon RPC
}

#NODEJS {
nodejs how would you increase a number each second

define express middle-ware
}

#SOCKETIO {

how to join socket room
> how would you send a message to particular user, given that they have not joined any room explicitly
sending message to particular user

}




########################################################
############  for parsing json from socket #############
########################################################



Debug.Log("Event Name : " + e.name + " Data : " + e.data);

        string recCallBackString = e.data.ToString();

        Debug.Log("recCallback sub string: " + recCallBackString);

        string callBackSubstring = recCallBackString.Substring(1, recCallBackString.Length - 2);

        Debug.Log("Callback sub string: " + callBackSubstring);

        var userListsJSON = sJSON.Parse(callBackSubstring);
foreach(var userName in userListsJSON)
        {
            Debug.Log("Listing user with name: " + userName.ToString());
            Debug.Log("Listing user with name: " + userName.Value);
            shiftLoginManager.GetComponent<ShiftLoginManager>().ListUsersWithName(userName.Value);
        }


########################################################
############  for parsing json from web response ##############
########################################################

// using SimpleJson; JSONNode responseData = sJSON.Parse(www.downloadHandler.text); string loginStatus = responseData["status"].Value; Debug.Log("LoginStatus: " + loginStatus);



########################################################
########## for moving object without Update ############
########################################################



        IEnumerator FakeThrowingCard(Transform target)
        {
            transform.position = dealersDeck.transform.position;

            //  Transform inInitPosition = initPosition.transform;
            //  Transform inTargetPosition = targetPosition.transform;


            //float smoothing = 1.0f;

            while (Vector3.Distance(transform.position, target.position) > 0.05f)
            {

                transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);

                yield return null;

            }


            //transform.SetParent(myParentObject.transform);

            // done

            yield return new WaitForSeconds(wfs);

            //finished

        }
########################################################
############### for input to upper case ################
########################################################


        
public InputField inputField;
this goes to start function:

  inputField.onValidateInput += delegate (string input, int charIndex, char addedChar) {
        return nameValidation(addedChar);
  };
and this is the addedChar function that does the control and modification on per character basis:

 private char nameValidation(char c)
 {
     if (c >= 'A' && c <= 'Z') {
         return (char)((int)c - 'A' + 'a');
     }
     else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == ' ') {
         return c;
     } else {
         return '\0';
     }
 }

no more onChange causing more change!


And here's an even simpler function to force uppercase...

 void Start()
 {
     your_input.onValidateInput += 
         delegate (string s, int i, char c) { return char.ToUpper(c); };
 }
 
And if you want ONLY letters:

 void Start()
 {
     your_input.onValidateInput += delegate (string s, int i, char c) { return char.ToUpper(c); };
 }
 
 char Val(char c)
 {
     c =  char.ToUpper(c);
     return char.IsLetter(c) ? c : '\0';
 }

Here's uppercase letters only, and a length limit:

     your_input.onValidateInput += delegate (string s, int i, char c)
     {
         if (s.Length >= 4) { return '\0'; }
         c = char.ToUpper(c);
         return char.IsLetter(c) ? c : '\0';
     };

(Alternately, don't forget the very handy .CharacterValidation approach. It is often all you need:)

 void Start()
 {
     your_input.characterValidation = InputField.CharacterValidation.Alphanumeric;
 }


So when using .onValidateInput the .Net "char." functions are very handy.




###########################################################
######### for layout bug fixing after adding items at runtime #########
###########################################################

public void InstantReturn()
        {
            transform.SetParent(dealersDeck);

            transform.SetAsLastSibling();

            LayoutRebuilder.ForceRebuildLayoutImmediate(dealersDeck);
        }


//////// UNITY AS A LIBRARY /////////
https://github.com/Unity-Technologies/uaal-example



###########################################################
######### Sending json file to REST API #########
###########################################################

private IEnumerator SavingUserProgressToServer(string fileContents)
{
yield return new WaitForEndOfFrame();

byte[] bArray = Encoding.ASCII.GetBytes(fileContents);


//string enc = Convert.ToBase64String(fileContents);
//string enc = fileContents;


WWWForm form = new WWWForm();



//form.AddField("UserId", PlayerPrefs.GetString("userid"));

//form.AddField("userid", "42");
form.AddField("UserId", 42);

//Debug.Log("Getting file at: " + saveFilePath);

Debug.Log("File contents: " + fileContents);

//form.AddField("file", fileContents);

form.AddBinaryData("files", bArray, "game_manager.json","application/json");


//UnityWebRequest www = UnityWebRequest.Post("localhost:3001/upload", form);
UnityWebRequest www = UnityWebRequest.Post("localhost:3000/SaveJsonFileApi", form);

//www.certificateHandler = new BypassCertificate();

yield return www.SendWebRequest();
print("Upload Files ");
if (www.isHttpError || www.isNetworkError)
{
Debug.Log(www.error);

Debug.Log("Uploaded " + " Failed");
}
else
{
Debug.Log("Uploaded " + " files Successfully");

}


}


public class BypassCertificate : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
return true;
}
}



###########################################################
######### Reading data from REST API using SimpleJSON #########
###########################################################


IEnumerator FetchProductsNow()
    {
        yield return new WaitForEndOfFrame();

        UnityWebRequest www = UnityWebRequest.Get("https://shiftescape.com/api/Offer/GetHomePageOffers?StoreId=3");

        yield return www.SendWebRequest();

        if(www.isHttpError || www.isNetworkError)
        {
            Debug.Log(www.error);
        }
        else
        {
           // Debug.Log(www.downloadHandler.text);
            JSONNode itemsData = sJSON.Parse(www.downloadHandler.text);

            totalProductsCount = itemsData["ResponseMessage"][1].Count;

            Debug.Log("Some item: " + itemsData["ResponseMessage"][1][3]["ItemName"]);

            for(int i = 0; i < totalProductsCount; i++)
            {
                if(i < productPlaceholders.Length)
                {

                    productPlaceholders[i].TextureURL = itemsData["ResponseMessage"][1][i]["ItemPhoto"];
                    productPlaceholders[i].productName = itemsData["ResponseMessage"][1][i]["ItemName"];
                    productPlaceholders[i].productPrice = itemsData["ResponseMessage"][1][i]["SellingPrice"];
                    productPlaceholders[i].RefreshProduct();
                }
            }
        }

    }


Comments