{"id":3904,"date":"2016-10-01T22:18:18","date_gmt":"2016-10-01T22:18:18","guid":{"rendered":"http:\/\/www.drmop.com\/?page_id=3904"},"modified":"2016-10-14T20:49:46","modified_gmt":"2016-10-14T20:49:46","slug":"unity-3d-tips","status":"publish","type":"page","link":"http:\/\/www.drmop.com\/index.php\/unity-3d-tips\/","title":{"rendered":"Unity 3D Tips"},"content":{"rendered":"<p>Because I have a bit of a Goldfish memory at times, this page contains a list of the various Unity tips that I have picked up on my travels, I share them not only to enlighten others, but also so I do not forget them :).<\/p>\n<h1>General<\/h1>\n<ul>\n<li>\nHow do I make a class serialisable?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\n[Serializable]<br \/>\npublic class SaveData<br \/>\n{<br \/>\n}<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I open an URL in the browser?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nApplication.OpenURL(&quot;http:\/\/www.drmop.com&quot;);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I load another scene?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nSceneManager.LoadScene(&quot;AnotherScene&quot;);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I check for the back button being pressed on Android?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nif (Input.GetKeyDown(KeyCode.Escape))<br \/>\n{<br \/>\n}<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I instantiate a prefab?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nGameObject obj = Instantiate(my_prefab, position, orientation) as GameObject;<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I change the parent of a game object?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nmy_object.transform.SetParent(parent_object.transform);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I get the main camera in the scene?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nCamera main_cam = Camera.main;<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I get the first child of a game object?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nTransform first_child = game_object.GetChild(0);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I get a named child of a game object?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nTransform child = game_object.FindChild(&quot;childs_name&quot;);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I define and start a coroutine?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\n\/\/ Define the coroutine<br \/>\nprivate IEnumerator RunTests()<br \/>\n{<br \/>\n    for (int t = 0; t &lt; 100; t++)<br \/>\n    {<br \/>\n        \/\/ Run the test at index t then wait for 2 seconds<br \/>\n        RunTest(t);<br \/>\n        yield return new WaitForSeconds(2);<br \/>\n    }<br \/>\n}<br \/>\n\/\/ Start the coroutine<br \/>\nStartCoroutine(RunTests());<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I call a method after a delay?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\n\/\/ Define a coroutine that cals the method after a delay<br \/>\nprivate IEnumerator CallMethodAfterDelay(float delay, Action method)<br \/>\n{<br \/>\n    yield return new WaitForSeconds(delay);<br \/>\n    method();<br \/>\n}<br \/>\n\/\/ Start the coroutine<br \/>\nStartCoroutine(CallMethodAfterDelay(10, my_delayed_method));<br \/>\n[\/sourcecode]\n<\/li>\n<\/ul>\n<h1>Collision and hit testing<\/h1>\n<ul>\n<li>\nHow do I found out which object was touched on the screen?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nprivate HitObject? FindHitObject(Vector3 pos)<br \/>\n{<br \/>\n    Ray r = Camera.main.ScreenPointToRay(pos);<br \/>\n    RaycastHit hit;<\/p>\n<p>    if (Physics.Raycast(r, out hit, Mathf.Infinity))<br \/>\n    {<br \/>\n        GameObject go = hit.transform.gameObject;<br \/>\n    }<br \/>\n    return null;<br \/>\n}<br \/>\n[\/sourcecode]\n<\/li>\n<\/ul>\n<h1>Maths<\/h1>\n<ul>\n<li>\nHow do I make an object face the camera or another object?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\ntransform.rotation = Quaternion.LookRotation(cam_or_object.transform.position &#8211; transform.position);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I position an object in the direction of another object?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\n\/\/ Set the objects position so that it is distance units in front of the source object in the direction it is facing<br \/>\nVector3 dir = source_object.transform.rotation * new Vector3(0, 0, distance);<br \/>\ntransform.position = source_object.transform.position &#8211; dir;<br \/>\n[\/sourcecode]\n<\/li>\n<\/ul>\n<h1>3D<\/h1>\n<li>\nHow do I change the text and colour of 3D Text?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nTextMesh tm = go.GetComponent&lt;TextMesh&gt;();<br \/>\ntm.text = &quot;Hello&quot;;<br \/>\ntm.color = Color.blue;<br \/>\n[\/sourcecode]\n<\/li>\n<h1>Animation<\/h1>\n<ul>\n<li>\nHow do I make an animation relative?<br \/>\nUnity doesn&#8217;t appear to support relative animation key frames so instead you have to create an empty game object then place the object with its animation inside it. You can then position the parent game object to wherever you would like the object to start its animation.\n<\/li>\n<\/ul>\n<h1>Materials and Textures<\/h1>\n<ul>\n<li>\nHow do I change the material of a mesh?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nMeshRenderer mr = my_object.GetComponent&lt;MeshRenderer&gt;();<br \/>\nmr.material = new_material;<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I change the texture of a material?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nMeshRenderer mr = my_object.GetComponent&lt;MeshRenderer&gt;();<br \/>\nmr.material.texture = new_texture;<br \/>\n[\/sourcecode]\n<\/li>\n<\/ul>\n<h1>Networking<\/h1>\n<ul>\n<li>\nHow do I download a file from the web?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nusing UnityEngine.Networking;<br \/>\nIEnumerator DownloadFile()<br \/>\n{<br \/>\n    UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(&quot;http:\/\/www.drmop.com\/acoolfile.txt&quot;);<br \/>\n    yield return www.Send();<br \/>\n    if (!www.isError)<br \/>\n    {<br \/>\n        Debug.Log(www.downloadHandler.text);<br \/>\n    }<br \/>\n}<br \/>\n[\/sourcecode]\n<\/li>\n<\/ul>\n<h1>UI<\/h1>\n<ul>\n<li>\nHow do I get the size of an UI element?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nRectTransform rt = GetComponent&lt;RectTransform&gt;();<br \/>\nRect rect = rt.rect;<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I change the size of an UI element?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nRectTransform rt = GetComponent&lt;RectTransform&gt;();<br \/>\nrt.sizeDelta += new Vector2(new_size.x &#8211; rt.rect.width, new_size.y &#8211; rt.rect.height);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I get the parent canvas for an UI element?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nCanvas parent = GetComponentInParent&lt;Canvas&gt;();<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I hide an UI element?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\ntransform.localScale = new Vector3(0, 0, 0);<br \/>\n[\/sourcecode]\n<\/li>\n<li>\nHow do I change the sprite of an UI element?<br \/>\n[sourcecode language=&#8221;csharp&#8221;]<br \/>\nMusicButton.GetComponent&lt;Image&gt;().sprite = new_sprite;<br \/>\n[\/sourcecode]\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Because I have a bit of a Goldfish memory at times, this page contains a list of the various Unity tips that I have picked up on my travels, I share them not only to enlighten others, but also so I do not forget them :). General How do I make a class serialisable? [sourcecode [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-3904","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/pages\/3904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/comments?post=3904"}],"version-history":[{"count":24,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/pages\/3904\/revisions"}],"predecessor-version":[{"id":3911,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/pages\/3904\/revisions\/3911"}],"wp:attachment":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/media?parent=3904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}