### OS X: Example Project install with installer command Source: https://docs.unity3d.com/2018.1/Documentation/Manual/InstallingUnity.html Installs the Example Project into /Users/Shared/Unity/Standard-Assets on the specified volume. ```bash sudo installer [-dumplog] -package Examples.pkg -target / ``` -------------------------------- ### Windows: Silent Example Project install Source: https://docs.unity3d.com/2018.1/Documentation/Manual/InstallingUnity.html Silently installs the Example Project. The default folder is C:\Users\Public\Documentation\Unity Projects\Standard Assets Example Project. ```bash UnityExampleProjectSetup.exe /S /D=E:\Development\Unity ``` -------------------------------- ### Build Player with Postprocess (C#) Source: https://docs.unity3d.com/2018.1/Documentation/Manual/BuildPlayerPipeline.html C# example that builds a Windows player, copies a Readme file, and starts the built game. Uses BuildPipeline.BuildPlayer with BuildTarget.StandaloneWindows and BuildOptions.None. ```C# // C# example. using UnityEditor; using System.Diagnostics; public class ScriptBatch { [MenuItem("MyTools/Windows Build With Postprocess")] public static void BuildGame () { // Get filename. string path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", ""); string[] levels = new string[] {"Assets/Scene1.unity", "Assets/Scene2.unity"}; // Build player. BuildPipeline.BuildPlayer(levels, path + "/BuiltGame.exe", BuildTarget.StandaloneWindows, BuildOptions.None); // Copy a file from the project folder to the build folder, alongside the built game. FileUtil.CopyFileOrDirectory("Assets/Templates/Readme.txt", path + "Readme.txt"); // Run the game (Process class from System.Diagnostics). Process proc = new Process(); proc.StartInfo.FileName = path + "/BuiltGame.exe"; proc.Start(); } } ``` -------------------------------- ### Start server and register host (C#) Source: https://docs.unity3d.com/2018.1/Documentation/Manual/net-MasterServer.html C# example that starts a server and registers the host with the Master Server. It uses Network.InitializeServer with NAT punchthrough enabled if no public IP is present, then calls MasterServer.RegisterHost. ```csharp using UnityEngine; using UnityEngine.Network; using System.Collections; public class ExampleScript : MonoBehaviour { void OnGUI() { if (GUILayout.Button ("Start Server")) { // Use NAT punchthrough if no public IP present Network.InitializeServer(32, 25002, !Network.HavePublicAddress()); MasterServer.RegisterHost("MyUniqueGameType", "JohnDoes game", "l33t game for all"); } } } ``` -------------------------------- ### Start server and register host (JavaScript) Source: https://docs.unity3d.com/2018.1/Documentation/Manual/net-MasterServer.html JavaScript example that starts a server and registers the host with the Master Server. It uses Network.InitializeServer with NAT punchthrough enabled if no public IP is present, then calls MasterServer.RegisterHost. ```javascript function OnGUI() { if (GUILayout.Button ("Start Server")) { // Use NAT punchthrough if no public IP present Network.InitializeServer(32, 25002, !Network.HavePublicAddress()); MasterServer.RegisterHost("MyUniqueGameType", "JohnDoes game", "l33t game for all"); } } ``` -------------------------------- ### Build Player with Postprocess (JavaScript) Source: https://docs.unity3d.com/2018.1/Documentation/Manual/BuildPlayerPipeline.html UnityScript example that builds a Windows player, copies a Readme file, and starts the built game. Uses BuildPipeline.BuildPlayer with BuildTarget.StandaloneWindows and BuildOptions.None. ```JavaScript // JS example. import System.Diagnostics; class ScriptBatch { @MenuItem("MyTools/Windows Build With Postprocess") static function BuildGame() { // Get filename. var path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", ""); var levels : String[] = ["Assets/Scene1.unity", "Assets/Scene2.unity"]; // Build player. BuildPipeline.BuildPlayer(levels, path + "/BuiltGame.exe", BuildTarget.StandaloneWindows, BuildOptions.None); // Copy a file from the project folder to the build folder, alongside the built game. FileUtil.CopyFileOrDirectory("Assets/Templates/Readme.txt", path + "Readme.txt"); // Run the game (Process class from System.Diagnostics). var proc = new Process(); proc.StartInfo.FileName = path + "/BuiltGame.exe"; proc.Start(); } } ``` -------------------------------- ### GetComponent in C# Source: https://docs.unity3d.com/2018.1/Documentation/Manual/ControllingGameObjectsComponents.html C# example showing how to get a Rigidbody component reference using GetComponent in the Start method. ```csharp void Start () { Rigidbody rb = GetComponent(); } ``` -------------------------------- ### WaitWhile coroutine example in C# Source: https://docs.unity3d.com/2018.1/Documentation/Manual/CLIBatchmodeCoroutines.html Use this coroutine to wait until a condition becomes false. The example logs 'Start of WaitWhile Example', yields until Time.time is at least 5.0f, then logs 'End of WaitWhile Example'. Requires the System.Collections and UnityEngine namespaces. ```csharp using System.Collections; using UnityEngine; [ExecuteInEditMode] public class ExampleClass : MonoBehaviour { public void Start() { StartCoroutine(Example_WaitWhile_Coroutine()); } public IEnumerator Example_WaitWhile_Coroutine() { Debug.Log("Start of WaitWhile Example"); yield return new WaitWhile(() => Time.time < 5.0f); Debug.Log("End of WaitWhile Example"); } } ``` -------------------------------- ### Partial native stack trace example Source: https://docs.unity3d.com/2018.1/Documentation/Manual/TroubleShootingIPhone.html Example of a partial stack trace seen when native symbols are stripped during a Release build. Only the start() function is shown. ```text Thread 1 (thread 11523): 1. 0 0x0062564c in start () ``` -------------------------------- ### Complete UnityWebRequest GET example Source: https://docs.unity3d.com/2018.1/Documentation/Manual/UnityWebRequest-RetrievingTextBinaryData.html A MonoBehaviour that sends a GET request to a server, checks for errors, and retrieves the response as text or binary data. Uses StartCoroutine and SendWebRequest. ```csharp using UnityEngine; using System.Collections; using UnityEngine.Networking; public class MyBehaviour : MonoBehaviour { void Start() { StartCoroutine(GetText()); } IEnumerator GetText() { UnityWebRequest www = UnityWebRequest.Get("http://www.my-server.com"); yield return www.SendWebRequest(); if(www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { // Show results as text Debug.Log(www.downloadHandler.text); // Or retrieve results as binary data byte[] results = www.downloadHandler.data; } } } ``` -------------------------------- ### JavaScript (UnityScript) example: Request and display host list, connect to host Source: https://docs.unity3d.com/2018.1/Documentation/Manual/net-MasterServer.html JavaScript (UnityScript) version of the same example, using the same MasterServer and Network APIs. ```javascript function Awake() { MasterServer.RequestHostList("MadBubbleSmashGame"); } function OnGUI() { var data : HostData[] = MasterServer.PollHostList(); // Go through all the hosts in the host list for (var element in data) { GUILayout.BeginHorizontal(); var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit; GUILayout.Label(name); GUILayout.Space(5); var hostInfo; hostInfo = "["; for (var host in element.ip) hostInfo = hostInfo + host + ":" + element.port + " "; hostInfo = hostInfo + "]"; GUILayout.Label(hostInfo); GUILayout.Space(5); GUILayout.Label(element.comment); GUILayout.Space(5); GUILayout.FlexibleSpace(); if (GUILayout.Button("Connect")) { // Connect to HostData struct, internally the correct method is used (GUID when using NAT). Network.Connect(element); } GUILayout.EndHorizontal(); } } ``` -------------------------------- ### Implement allocRequestForHTTPMethod (Objective-C) Source: https://docs.unity3d.com/2018.1/Documentation/Manual/iosCustomWWWRequest.html Starts by calling the same method on the superclass to get the default request, then customizes it. ```objectivec @implementation UnityWWWCustomRequestProvider + (NSMutableURLRequest*)allocRequestForHTTPMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers { NSMutableURLRequest* request = [super allocRequestForHTTPMethod:method url:url headers:headers]; ``` -------------------------------- ### UnityWebRequest creation and configuration example Source: https://docs.unity3d.com/2018.1/Documentation/Manual/UnityWebRequest-CreatingUnityWebRequests.html Shows how to instantiate UnityWebRequest with both constructors and configure required properties like url and method, plus optional settings such as useHttpContinue, chunkedTransfer, redirectLimit, and timeout. ```C# UnityWebRequest wr = new UnityWebRequest(); // Completely blank UnityWebRequest wr2 = new UnityWebRequest("http://www.mysite.com"); // Target URL is set // the following two are required to web requests to work wr.url = "http://www.mysite.com"; wr.method = UnityWebRequest.kHttpVerbGET; // can be set to any custom method, common constants privided wr.useHttpContinue = false; wr.chunkedTransfer = false; wr.redirectLimit = 0; // disable redirects wr.timeout = 60; // don't make this small, web requests do take some time ``` -------------------------------- ### Setup functions for server, remote client, and local client Source: https://docs.unity3d.com/2018.1/Documentation/Manual/UNetClientServer.html These functions create a server, a remote client, or a local client. The remote client connects to 127.0.0.1:4444, assuming the server is on the same machine; for a finished game, use an internet address or the Matchmaking system. ```csharp // Create a server and listen on a port public void SetupServer() { NetworkServer.Listen(4444); isAtStartup = false; } // Create a client and connect to the server port public void SetupClient() { myClient = new NetworkClient(); myClient.RegisterHandler(MsgType.Connect, OnConnected); myClient.Connect("127.0.0.1", 4444); isAtStartup = false; } // Create a local client and connect to the local server public void SetupLocalClient() { myClient = ClientScene.ConnectLocalServer(); myClient.RegisterHandler(MsgType.Connect, OnConnected); isAtStartup = false; } ``` -------------------------------- ### Find GameObject by name with GameObject.Find Source: https://docs.unity3d.com/2018.1/Documentation/Manual/ControllingGameObjectsComponents.html Use GameObject.Find to locate a single GameObject by its name. The example assigns the result to a player variable in Start(). ```C# GameObject player; void Start() { player = GameObject.Find("MainHeroCharacter"); } ```