用途:一个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));
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。