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 language=”csharp”]
[Serializable]
public class SaveData
{
}
[/sourcecode] -
How do I open an URL in the browser?
[sourcecode language=”csharp”]
Application.OpenURL("http://www.drmop.com");
[/sourcecode] -
How do I load another scene?
[sourcecode language=”csharp”]
SceneManager.LoadScene("AnotherScene");
[/sourcecode] -
How do I check for the back button being pressed on Android?
[sourcecode language=”csharp”]
if (Input.GetKeyDown(KeyCode.Escape))
{
}
[/sourcecode] -
How do I instantiate a prefab?
[sourcecode language=”csharp”]
GameObject obj = Instantiate(my_prefab, position, orientation) as GameObject;
[/sourcecode] -
How do I change the parent of a game object?
[sourcecode language=”csharp”]
my_object.transform.SetParent(parent_object.transform);
[/sourcecode] -
How do I get the main camera in the scene?
[sourcecode language=”csharp”]
Camera main_cam = Camera.main;
[/sourcecode] -
How do I get the first child of a game object?
[sourcecode language=”csharp”]
Transform first_child = game_object.GetChild(0);
[/sourcecode] -
How do I get a named child of a game object?
[sourcecode language=”csharp”]
Transform child = game_object.FindChild("childs_name");
[/sourcecode] -
How do I define and start a coroutine?
[sourcecode language=”csharp”]
// Define the coroutine
private IEnumerator RunTests()
{
for (int t = 0; t < 100; t++)
{
// Run the test at index t then wait for 2 seconds
RunTest(t);
yield return new WaitForSeconds(2);
}
}
// Start the coroutine
StartCoroutine(RunTests());
[/sourcecode] -
How do I call a method after a delay?
[sourcecode language=”csharp”]
// Define a coroutine that cals the method after a delay
private IEnumerator CallMethodAfterDelay(float delay, Action method)
{
yield return new WaitForSeconds(delay);
method();
}
// Start the coroutine
StartCoroutine(CallMethodAfterDelay(10, my_delayed_method));
[/sourcecode]
Collision and hit testing
-
How do I found out which object was touched on the screen?
[sourcecode language=”csharp”]
private HitObject? FindHitObject(Vector3 pos)
{
Ray r = Camera.main.ScreenPointToRay(pos);
RaycastHit hit;if (Physics.Raycast(r, out hit, Mathf.Infinity))
{
GameObject go = hit.transform.gameObject;
}
return null;
}
[/sourcecode]
Maths
-
How do I make an object face the camera or another object?
[sourcecode language=”csharp”]
transform.rotation = Quaternion.LookRotation(cam_or_object.transform.position – transform.position);
[/sourcecode] -
How do I position an object in the direction of another object?
[sourcecode language=”csharp”]
// Set the objects position so that it is distance units in front of the source object in the direction it is facing
Vector3 dir = source_object.transform.rotation * new Vector3(0, 0, distance);
transform.position = source_object.transform.position – dir;
[/sourcecode]
3D
[sourcecode language=”csharp”]
TextMesh tm = go.GetComponent<TextMesh>();
tm.text = "Hello";
tm.color = Color.blue;
[/sourcecode]
Animation
-
How do I make an animation relative?
Unity doesn’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.
Materials and Textures
-
How do I change the material of a mesh?
[sourcecode language=”csharp”]
MeshRenderer mr = my_object.GetComponent<MeshRenderer>();
mr.material = new_material;
[/sourcecode] -
How do I change the texture of a material?
[sourcecode language=”csharp”]
MeshRenderer mr = my_object.GetComponent<MeshRenderer>();
mr.material.texture = new_texture;
[/sourcecode]
Networking
-
How do I download a file from the web?
[sourcecode language=”csharp”]
using UnityEngine.Networking;
IEnumerator DownloadFile()
{
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get("http://www.drmop.com/acoolfile.txt");
yield return www.Send();
if (!www.isError)
{
Debug.Log(www.downloadHandler.text);
}
}
[/sourcecode]
UI
-
How do I get the size of an UI element?
[sourcecode language=”csharp”]
RectTransform rt = GetComponent<RectTransform>();
Rect rect = rt.rect;
[/sourcecode] -
How do I change the size of an UI element?
[sourcecode language=”csharp”]
RectTransform rt = GetComponent<RectTransform>();
rt.sizeDelta += new Vector2(new_size.x – rt.rect.width, new_size.y – rt.rect.height);
[/sourcecode] -
How do I get the parent canvas for an UI element?
[sourcecode language=”csharp”]
Canvas parent = GetComponentInParent<Canvas>();
[/sourcecode] -
How do I hide an UI element?
[sourcecode language=”csharp”]
transform.localScale = new Vector3(0, 0, 0);
[/sourcecode] -
How do I change the sprite of an UI element?
[sourcecode language=”csharp”]
MusicButton.GetComponent<Image>().sprite = new_sprite;
[/sourcecode]