|
|
°¡Àå Áøº¸µÈ °ÔÀÓ ÄÚµå´Â ÇϳªÀÇ °³Ã¼¸¦ Á¶ÀÛÇÏÁö ¾Ê´Â´Ù. ÅëÇÕµÈ ½ºÅ©¸³Æ® ÀÎÅÍÆäÀ̽º´Â ´Ù¸¥ °ÔÀÓ °³Ã¼¿Í ±× ¾È¿¡¼ÀÇ ±¸¼º¿ä¼Ò¸¦ ã°Å³ª °Ë»öÇϱâ À§ÇÑ ´Ù¾çÇÑ ¹æ¹ýÀ» °¡Áö°í ÀÖ´Ù. ´ÙÀ½¿¡ ¿ì¸®´Â Àå¸é¿¡¼ °ÔÀÓ °³Ã¼¿¡ ¿¬°áµÈ OtherScript.js¶ó ºÒ¸®´Â ½ºÅ©¸³Æ®°¡ ÀÖ´Ù°í °¡Á¤ÇÑ´Ù. 1. JavaScript function Update () { otherScript = GetComponent(OtherScript); otherScript.DoSomething(); } 2. C# using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Update() { otherScript = GetComponent otherScript.DoSomething(); } } 1. °¨µ¶°üÀ» ÅëÇØ ÇÒ´çÇÒ ¼ö ÀÖ´Â Âü°í . ´ç½ÅÀº °¨µ¶°üÀ» ÅëÇؼ ´Ù¸¥ °³Ã¼ À¯Çü¿¡ ´ëÇØ º¯¼öµéÀ» ÇÒ´çÇÒ ¼ö ÀÖ´Ù: 1. JavaScript // Ÿ°Ù ½½·ÔÀ¸·Î ºÎÅÍ µå·¡±×µÈ ¿ÀºêÁ§Æ®¸¦ ¹ø¿ªÇÕ´Ï´Ù. var target : Transform; function Update () { target.Translate(0, 1, 0); } 2. C# using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Transform target; void Update() { target.Translate(0, 1, 0); } } ´ç½ÅÀº ¶ÇÇÑ °Ë»ç°ü¿¡°Ô ´Ù¸¥ °³Ã¼¿¡ ´ëÇÑ Âü°íÇÒ °ÍÀ» º¸Àϼö ÀÖ´Ù. °¨µ¶°ü¿¡°Ô ´ë»ó ½½·Ô¿¡¼ Otherscipt¸¦ Æ÷ÇÔÇÑ °ÔÀÓ °³Ã¼¸¦ µå·¡±× ÇÒ ¼ö ÀÖ´Ù. 1. JavaScript // °Ë¿°ü¿¡ ÁöÁ¤µÈ Ÿ°Ù º¯¼ö¿¡¼ foo DoSomethingÀ» ¼³Á¤ÇÕ´Ï´Ù. var target : OtherScript; function Update () { // Ÿ°Ù ¿ÀºêÁ§Æ®ÀÇ foo º¯¼ö¸¦ ¼³Á¤ÇÕ´Ï´Ù. target.foo = 2; // Ÿ°Ù¿¡¼ do somethingÀ» È£ÃâÇÕ´Ï´Ù. target.DoSomething("Hello"); } 2. C# using UnityEngine; using System.Collections; public class example : MonoBehaviour { public OtherScript target; void Update() { target.foo = 2; target.DoSomething("Hello"); } } 2. °³Ã¼ °èÃþ ±¸Á¶¸¦ ÅëÇÑ À§Ä¡. ´ç½ÅÀº Á¸ÀçÇÏ´Â °³Ã¼¸¦ ÅëÇØ ¾ÆÀÌ¿Í ºÎ¸ð °³Ã¼¸¦ ãÀ» ¼ö ÀÖ´Ù Transform component of a game object: 1. JavaScript // °ÔÀÓ¿ÀºêÁ§Æ®¿¡¼ ¾î¸°ÀÌÀÇ "Hand"À» ã½À´Ï´Ù. // we attached the script to transform.Find("Hand").Translate(0, 1, 0); 2. C# using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Awake() { transform.Find("Hand").Translate(0, 1, 0); } } ÀÏ´Ü ´ç½ÅÀÌ °ÔÃþ¿¡¼ º¯È¯À» ã´Â´Ù¸é, ´ç½ÅÀº »ç¿ëÇÒ ¼ö ÀÖ´Ù GetComponent to get to other scripts. 1. JavaScript // "Hand"¶ó´Â À̸§ÀÇ ¾î¸°À̸¦ ã½À´Ï´Ù. // °Å±â¿¡ ÷ºÎµÈ OtherScript¿¡¼, foo¸¦ 2±îÁö ¼³Á¤ÇÕ´Ï´Ù. transform.Find("Hand").GetComponent(OtherScript).foo = 2; // "Hand"¶ó´Â À̸§ÀÇ ¾î¸°À̸¦ ã½À´Ï´Ù. // °Å±â¿¡ ÷ºÎµÈ OtherScript¿¡¼ DoSomethingÀ» È£ÃâÇÕ´Ï´Ù. transform.Find("Hand").GetComponent(OtherScript).DoSomething("Hello"); // "Hand"¶ó´Â À̸§ÀÇ ¾î¸°À̸¦ ã½À´Ï´Ù. // ±×·¯¸é ¼Õ¿¡ ºÎÂøµÈ °Ã¼¿¡ ÈûÀ» Àû¿ëÇÕ´Ï´Ù. transform.Find("Hand").rigidbody.AddForce(0, 10, 0); 2. C# using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Awake() { |