This shows you the differences between two versions of the page.
pjv:laboratoare:en:02 [2019/10/23 14:27] alexandru.gradinaru |
pjv:laboratoare:en:02 [2019/10/23 14:33] (current) alexandru.gradinaru |
||
---|---|---|---|
Line 2: | Line 2: | ||
[[https://www.raywenderlich.com/6570-introduction-to-unity-ui-part-1|Unity UI โ Part 1]] | [[https://www.raywenderlich.com/6570-introduction-to-unity-ui-part-1|Unity UI โ Part 1]] | ||
+ | [[https://www.raywenderlich.com/4180726-introduction-to-unity-scripting-part-1|Unity Scripting โ Part 1]] | ||
+ | |||
+ | === Parameters ==== | ||
+ | |||
+ | Parameters can be controlled in the editor as long as they are defined as public or serializable variables. | ||
+ | |||
+ | You can define both simple variables and lists (array) | ||
+ | |||
+ | <code> | ||
+ | public float speed; | ||
+ | |||
+ | public Sprite[] images; | ||
+ | |||
+ | |||
+ | [SerializeField] | ||
+ | private Text scoreText; | ||
+ | | ||
+ | [SerializeField] | ||
+ | private GameObject[] enemyTypes; | ||
+ | </code> | ||
+ | |||
+ | |||
+ | === Instantiating objects === | ||
+ | |||
+ | Objects can be instantiated using the function | ||
+ | <code> | ||
+ | newobj = Instantiate(objTemplate) as ObjType; | ||
+ | |||
+ | //from pregab - prefab must be in Resources folder | ||
+ | newobj1 = Instantiate(Resources.Load("enemy")); | ||
+ | |||
+ | // Instantiate the projectile at the position and rotation of this transform | ||
+ | Rigidbody projectile; | ||
+ | Rigidbody clone; | ||
+ | clone = Instantiate(projectile, transform.position, transform.rotation); | ||
+ | |||
+ | enemyOrc = Instantiate(Orc) as Enemy; | ||
+ | </code> | ||
+ | |||
+ | === Transforms === | ||
+ | |||
+ | The transformations of an object can be accessed through the attribute `transform` (https://docs.unity3d.com/ScriptReference/Transform.html) | ||
+ | |||
+ | <code> | ||
+ | Vector3 objectPosition = GameObject.transform.position; | ||
+ | |||
+ | GameObject.transform.position = new Vector3(posX, posY, posZ); | ||
+ | |||
+ | transform.Translate(Vector3.up * Time.deltaTime, Space.World); | ||
+ | |||
+ | transform.Rotate(Vector3.up * Time.deltaTime, Space.World); | ||
+ | </code> | ||
+ | |||
+ | === Random === | ||
+ | |||
+ | To generate random values you can use the Random class | ||
+ | |||
+ | <code> | ||
+ | |||
+ | Random.Range(-10.0f, 10.0f) | ||
+ | Random.Range(0, 8); | ||
+ | |||
+ | </code> | ||
+ | |||
+ | === Control of some objects or components === | ||
+ | |||
+ | The display or hiding of an object in the scene can be done through the activation function. | ||
+ | |||
+ | <code> | ||
+ | myObject.SetActive(false); // hide | ||
+ | myObject.SetActive(true); // show | ||
+ | </code> | ||
+ | |||
+ | Similarly, the components of an object can be controlled | ||
+ | |||
+ | <code> | ||
+ | |||
+ | this.GetComponent<BoxCollider>().SetActive(false); // deactivate | ||
+ | this.GetComponent<SpriteRenderer>().sprite = image; //setting an image | ||
+ | |||
+ | myObject.GetComponent<Text>().text = '123' //setting a text | ||
+ | |||
+ | </code> | ||
+ | |||
+ | You can also access child or parent items | ||
+ | <code> | ||
+ | childObject=parentObject.GetChild("child_name"); | ||
+ | |||
+ | //setarea unei componente a elementului copil | ||
+ | parentObject.GetChild("child_name").GetComponent<SpriteRenderer>().sprite = image; | ||
+ | </code> | ||
+ | |||
+ | === Wait === | ||
+ | |||
+ | If you want to introduce expectations when running a script / functions you can use custom keys. | ||
+ | The corutines are used to start asynchronous functions, which can be extended on several frames, and in which you can put the wait (pause). | ||
+ | |||
+ | <code> | ||
+ | void Start() | ||
+ | { | ||
+ | StartCoroutine(Example()); | ||
+ | } | ||
+ | |||
+ | IEnumerator Example() | ||
+ | { | ||
+ | print(Time.time); | ||
+ | yield return new WaitForSeconds(5); | ||
+ | print(Time.time); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | === Input Events === | ||
+ | |||
+ | To be able to detect if an item has been pressed, the OnMouseDown function can be used | ||
+ | <code> | ||
+ | |||
+ | void OnMouseDown() | ||
+ | { | ||
+ | gameObject.SetActive(false); | ||
+ | } | ||
+ | | ||
+ | </code> | ||
+ | |||
+ | <note important>Note! This function is active only if the user clicks on a UI element or a Collider</note> | ||
+ | |||
==== Tasks ==== | ==== Tasks ==== |