溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

unity3d學(xué)習(xí)GUIFly腳本

發(fā)布時間:2020-02-27 04:00:33 來源:網(wǎng)絡(luò) 閱讀:427 作者:xiaoxuanyunmeng 欄目:游戲開發(fā)

用途:一個GUI對象在屏幕中飛行,用GameObject.SendMessage告訴GUI對象何時飛行。

使用:把該腳本拖到一個GameObject上,用另一個腳本GameObject.SendMessage發(fā)送飛行消息。

unity3d學(xué)習(xí)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));
             
  }
             
  }


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI