Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pjv:laboratoare:en:02 [2019/10/16 15:26]
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 ====
 +Making a 2D memory game
 +
 +  * Configure a 2D scene for running
 +  * Add to the scene an image that represents a board (table)
 +  * Add two images to the scene:
 +    * one that represents the hidden element
 +    * one that represents the displayed element
 +  * Script the displayed element so that the click will hide and the hidden one will be displayed
 +  * Collect 3 more images for the displayed items
 +  * Make a game controller (in the scene there must be pairs of elements - so there will be 4 pairs positioned randomly at each run)
 +    * dynamic change of the image depending on which to write the following
 +    * dynamic instantiation of a grid of 4 × 4 elements (the first is already instantiated)
 +    * random positioning of type elements for each element in the scene
 +    * maintaining the currently selected elements (max. 1 pair is selected)
 +    * the asynchronous run of the check whether the pair was selected correctly or not
 +  * maintain and display a score. The score increases when you discover two identical elements.
 +  * Add a restart game button
pjv/laboratoare/en/02.1571228765.txt.gz · Last modified: 2019/10/16 15:26 by alexandru.gradinaru
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0