您好,登錄后才能下訂單哦!
用途:一個GUI對象在屏幕中飛行,用GameObject.SendMessage告訴GUI對象何時飛行。
使用:把該腳本拖到一個GameObject上,用另一個腳本GameObject.SendMessage發送飛行消息。
unity3d學習GUIFly腳本代碼 :
[javascript] view plaincopy //Attach this to the same as the GUIFly is attached to. // Fly in gameObject.SendMessage(“Fly”, true); // Wait 5 seconds yield new WaitForSeconds(5); // Fly out gameObject.SendMessage(“Fly”, false); [c-sharp] view plaincopy using UnityEngine; using System.Collections; public class GUIFly : MonoBehaviour { public enum InterpolationType { Linear, Sinusoidal, Hermite } public Vector3 m_InPosition; public Vector3 m_OutPosition; public float m_TravelTime = 0.5f; public float m_DelayToStartTravelingAfterMessageReceived = 0.1f; public bool m_StartWithInPosition = false; public InterpolationType m_InterpolationType = InterpolationType.Sinusoidal; void Start () { transform.position = (m_StartWithInPosition) ? m_InPosition : m_OutPosition; } IEnumerator Fly(bool flyIn) { yield return new WaitForSeconds(m_DelayToStartTravelingAfterMessageReceived); Vector3 targetPosition = (flyIn) ? m_InPosition : m_OutPosition; float startTime = Time.time; Vector3 startPosition = transform.position; while (Time.time < startTime + m_TravelTime) { switch (m_InterpolationType) { case InterpolationType.Linear: transform.position = Vector3.Lerp(startPosition, targetPosition, (Time.time - startTime) / m_TravelTime); break; case InterpolationType.Sinusoidal: transform.position = Sinerp(startPosition, targetPosition, (Time.time - startTime) / m_TravelTime); break; case InterpolationType.Hermite: transform.position = Hermite(startPosition, targetPosition, (Time.time - startTime) / m_TravelTime); break; } yield return 0; } transform.position = targetPosition; } void Reset() { m_InPosition = transform.position; } private static Vector3 Sinerp(Vector3 start, Vector3 end, float value) { return new Vector3(Sinerp(start.x, end.x, value), Sinerp(start.y, end.y, value), Sinerp(start.z, end.z, value)); } private static Vector3 Hermite(Vector3 start, Vector3 end, float value) { return new Vector3(Hermite(start.x, end.x, value), Hermite(start.y, end.y, value), Hermite(start.z, end.z, value)); } /* The following functions are also in the Mathfx script on the UnifyWiki, but are included here so the script is self sufficient. */ private static float Sinerp(float start, float end, float value) { return Mathf.Lerp(start, end, Mathf.Sin(value * Mathf.PI * 0.5f)); } private static float Hermite(float start, float end, float value) { return Mathf.Lerp(start, end, value * value * (3.0f - 2.0f * value)); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。