C# 用户界面面板Lerp更新
我有几个UI面板,我希望移动到屏幕区域。 我已经制作了一个脚本来附加到每个面板上,但是当我在更新中进行更改时,它几乎没有什么作用,我现在使用的是LateUpdate,它几乎可以工作。 面板只会非常轻微地移动到屏幕中,就好像只调用了一次LateUpdate一样。 奇怪的是,当我对第二个面板执行相同操作时,它的“HidePanel”方法会让第一个面板完全进入视图。然后第一个面板的“HidePanel”方法可以正常工作C# 用户界面面板Lerp更新,c#,user-interface,unity3d,panel,C#,User Interface,Unity3d,Panel,我有几个UI面板,我希望移动到屏幕区域。 我已经制作了一个脚本来附加到每个面板上,但是当我在更新中进行更改时,它几乎没有什么作用,我现在使用的是LateUpdate,它几乎可以工作。 面板只会非常轻微地移动到屏幕中,就好像只调用了一次LateUpdate一样。 奇怪的是,当我对第二个面板执行相同操作时,它的“HidePanel”方法会让第一个面板完全进入视图。然后第一个面板的“HidePanel”方法可以正常工作 using UnityEngine; using System.Collectio
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DisplayPanel : MonoBehaviour {
private bool isMovingIn = false;
private bool isMovingOut = false;
private Vector2 posOut; //The panel's position hidden just outside the RH screen edge.
private Vector2 posIn; //The panel’s position shown just inside the RH screen edge.
private Vector2 pos; //The panel’s current position.
//Initialization
void Start () {
RectTransform rt = GetComponent<RectTransform>();
posOut = new Vector2 ((Screen.width + rt.rect.width)/2, 0); //To be hidden just outside the RH screen edge.
posIn = new Vector2 (posOut.x - rt.rect.width, 0); //To be visible just inside the RH screen edge.
pos = posOut;
rt.anchoredPosition = pos; //Move panel to the RH screen edge.
}
public void LateUpdate() {
RectTransform rt = GetComponent<RectTransform> ();
rt.anchoredPosition = pos; //Need this for start..
if (isMovingIn == true) {
if ((int)pos.x != (int)posIn.x) { //Casting floats to ints so we can eventually find equality.
pos = Vector2.Lerp (pos, posIn, 2*Time.deltaTime);
rt.anchoredPosition = pos;
}
else {
isMovingIn = false; }
}
else
if (isMovingOut == true) {
if ( (int)pos.x != (int)posOut.x) {
pos = Vector2.Lerp (pos, posOut, 2 * Time.deltaTime);
rt.anchoredPosition = pos; }
else {
isMovingOut = false; }
}
}
public void DisplayPanel(){
isMovingOut = false;
isMovingIn = true;
}
public void HidePanel(){
isMovingIn = false;
isMovingOut = true;
}
}//End class
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共类显示面板:MonoBehavior{
private bool isMovingIn=假;
private bool isMovingOut=假;
private Vector2 posOut;//面板的位置隐藏在右侧屏幕边缘之外。
private Vector2 posIn;//面板的位置显示在右侧屏幕边缘内。
private Vector2 pos;//面板的当前位置。
//初始化
无效开始(){
RectTransform rt=GetComponent();
posOut=newvector2((Screen.width+rt.rect.width)/2,0);//隐藏在右侧屏幕边缘的正外部。
posIn=newvector2(posOut.x-rt.rect.width,0);//仅在右侧屏幕边缘内可见。
pos=posOut;
rt.anchoredPosition=pos;//将面板移到右侧屏幕边缘。
}
公共更新(){
rectcransform rt=GetComponent();
rt.anchoredPosition=pos;//开始时需要这个。。
如果(isMovingIn==true){
如果((int)pos.x!=(int)posIn.x){//Casting浮动到int,那么我们最终可以找到相等。
pos=Vector2.Lerp(pos,posIn,2*Time.deltaTime);
rt.锚定位置=位置;
}
否则{
isMovingIn=false;}
}
其他的
如果(isMovingOut==true){
如果((int)pos.x!=(int)posOut.x){
pos=Vector2.Lerp(pos,posOut,2*Time.deltaTime);
rt.anchoredPosition=pos;}
否则{
isMovingOut=false;}
}
}
公共面板(){
isMovingOut=假;
isMovingIn=真;
}
public void HidePanel(){
isMovingIn=假;
isMovingOut=真;
}
}//末级
您不应该尝试按代码执行这些移动,而更可能考虑动画。然后你可以在空间和时间上直观地定义动作。完整的脚本是什么?看看上面的信息,isMovingOut永远不会是真的。很难从您共享的内容中找到错误的地方。我认为这与我的两个面板共享的画布有关,并且没有调用FixedUpdate来完成屏幕内的面板移动。Everts,我需要在animator中设置当前屏幕大小,我在代码中创建了一个剪辑,但有一个小问题,所以尝试了这个。马克,为了清晰起见,我删除了所有的废话并重命名了内容。你不应该尝试通过代码执行这些移动,但更可能的是想动画。然后你可以在空间和时间上直观地定义动作。完整的脚本是什么?看看上面的信息,isMovingOut永远不会是真的。很难从您共享的内容中找到错误的地方。我认为这与我的两个面板共享的画布有关,并且没有调用FixedUpdate来完成屏幕内的面板移动。Everts,我需要在animator中设置当前屏幕大小,我在代码中创建了一个剪辑,但有一个小问题,所以尝试了这个。马克,为了清晰起见,我删除了所有的废话并重新命名了它。