简体   繁体   English

统一3d动画

[英]Unity 3d Animation

I ma newbie to Unity3d and was just playing with unity animation, i was trying to implement 2 Main UI Button such as when either of them is pressed it should check the condition whether a animation is already played before by 2nd button available if yes then remove those gameobject from scene using reverse animation else play the default animation attach to the particular button. 我是Unity3d的新手,只是玩团结动画,我试图实现2个主UI按钮,例如当他们中的任何一个被按下时它应该检查条件是否已经播放动画之前第二个按钮可用如果是,则删除那些来自场景的游戏对象使用反向动画否则播放默认动画附加到特定按钮。

Problem is out of 6 case only 4 are getting operational 2 of them are not executing (marked in the code as Case Not Operational ) 问题是6个案例中只有4个正在运行2其中没有执行(在代码中标记为Case Not Operational

Animation anim;
private bool isOpen = false; 
private bool open = false;
// Start is called before the first frame update
void Start()
{
    anim = GetComponent<Animation>();
    //isOpen = false;
    //open = false;

}

// Update is called once per frame
void Update()
{

}

void OnGUI()
{
    isOpen = GUI.Toggle(new Rect(50, 50, 200, 100), isOpen, "IsOpen");
    open = GUI.Toggle(new Rect(65, 65, 200, 100), open, "Open");
}
public void ButtonControl()
{
    string name = EventSystem.current.currentSelectedGameObject.name;
    if (name == "Main Button 1")
    {
        if (isOpen == false && open == false)
        {   Debug.Log("False False 1");
            Anim1();
            isOpen = true;
        }
        else if (isOpen == false && open == true) // case not operational
        {   Debug.Log("False True 2");
            ReverseAnim_2();
            open = false;
            Anim1();
            isOpen = true;
        }
        else if(isOpen == true)
        {
            Debug.Log("True 3");
            ReverseAnim_1();
            isOpen = false;
        }
    }


    else if (name == "Main Button 2")
    {
        if (open == false && isOpen == false)
        {   Debug.Log("False False 4");
            Anim2();
            open = true;
        }
        else if(open == false && isOpen == true) // case not operational
        {   Debug.Log("False True 5");
            ReverseAnim_1();
            isOpen = false;
            Anim2();
            open = true;
        }
        else if(open == true)
        {
            Debug.Log("True 6");
            ReverseAnim_2(); 
            open = false;
        }
    }
}

void ReverseAnim_1()
{
    anim["1"].speed = -1;
    anim["1"].time = anim["1"].length;
    anim.Play();
}

void Anim1()
{
    anim["1"].speed = 1;
    anim.Play();
}

void Anim2()
{
    anim["2"].speed = 1;
    anim.Play();
}

void ReverseAnim_2()
{
    anim["2"].speed = -1;
    anim["2"].time = anim["2"].length;
    anim.Play();
}

The problem is that you have two Main_B2 scripts and they are each tracking their own values for the isOpen and open fields. 问题是你有两个Main_B2脚本,每个脚本都跟踪isOpenopen字段的值。

One solution for this could be to make the fields static so that they are in common across all instances of Main_B2 . 对此的一个解决方案可以是使字段static以便它们在Main_B2所有实例中是Main_B2

Animation anim;
private static bool isOpen = false; 
private static bool open = false;

If you do this, your code should work as intended - each instance of Main_B2 would then be referring to the same fields whenever they reference isOpen or open . 如果你这样做,你的代码应该按预期工作 - 每当Main_B2引用isOpenopen时,它们将引用相同的字段。


With that said, static fields can get kind of sloppy if you ever want to try and re-use code so a better option might be to have only one instance of Main_B2 somewhere else such as the Canvas , instead of one on each button. 如上所述,如果您想尝试重新使用代码, static字段可能会变得Main_B2因此更好的选择可能是在其他地方只有一个 Main_B2 实例,例如Canvas ,而不是每个按钮上有一个。

Then you could have in it public GameObject fields that you can drag the button objects into, and private Animation fields for the animations.: 然后你可以在其中有public GameObject字段,你可以将按钮对象拖入, private Animation字段:

public GameObject button1
public GameObject button2

private Animation anim1
private Animation anim2

Then in Start : 然后在Start

anim1 = button1.GetComponent<Animation>();
anim2 = button2.GetComponent<Animation>();

And then wherever you referred to anim you would refer to anim1 or anim2 instead: 然后,无论你提到anim你都会引用anim1anim2代替:

void ReverseAnim_1()
{
    anim1.["1"].speed = -1;
    anim1.["1"].time = anim1["1"].length;
    anim1.Play();
}

void Anim1()
{
    anim1["1"].speed = 1;
    anim1.Play();
}

void Anim2()
{
    anim2["2"].speed = 1;
    anim2.Play();
}

void ReverseAnim_2()
{
    anim2["2"].speed = -1;
    anim2["2"].time = anim2["2"].length;
    anim2.Play();
}

Your scripts 'anim' variable is an Animation. 你的脚本'anim'变量是一个动画。 Normally to adjust animation parameters you set them on an Animator, not Animation. 通常,要调整动画参数,请在Animator上设置它们,而不是动画。 But perhaps your solution can work. 但也许你的解决方案可行。

For clarity. 为清楚起见。 An animator organizes how animations play out, when, for how long and how they transition. 动画师可以组织动画的播放方式,时间,转换时间,转换方式。

I would personally advise you to use an animator with behaviour patterns and transitionings between animations. 我个人建议你使用动画制作人员的行为模式和动画之间的过渡。 Instead of the current approach. 而不是目前的方法。

Either that or use one of the many Tweening asset packs from the asset store. 或者使用资产商店中的许多补间资产包中的一个。 They are specifically made to create UI effects like what you describe. 它们专门用于创建您所描述的UI效果。

At least consider these solutions if you keep being stuck :-) 如果你一直被卡住,至少要考虑这些解决方案:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM