unity代码添加动画,并传参数

测试界面

button一个
sprite一个

 

测试代码

public class BgObject : MonoBehaviour {

    void Start()
    {
        List<string> btnsName = new List<string>();
        btnsName.Add("login");

        foreach (string btnName in btnsName)
        {
            GameObject btnObj = GameObject.Find(btnName);
            Button btn = btnObj.GetComponent<Button>();
            btn.onClick.AddListener(delegate()
            {
                this.OnClick(btnObj);
            });
        }
    }

    public void OnClick(GameObject sender)
    {
        switch (sender.name)
        {
            case "login":
                Dictionary<string,string> dic = new Dictionary<string,string>();
                dic.Add("111","333");
                StartCoroutine(flip(dic));
                break;
            default:
                Debug.Log("none");
                break;
        }
    }

    IEnumerator flip(Dictionary<string, string> dic)
    {
        Text infoText = (Text)GameObject.Find("info").GetComponent<Text>();
        infoText.text = dic["111"];
        Transform obj = GameObject.Find("dizhu").transform;

        bool isDone = false;

        while (!isDone)
        {
            float x = obj.localPosition.x + Time.deltaTime;
            obj.localPosition = new Vector3(x, 0,0);

            if (obj.localPosition.x >= 0)
            {
                isDone = true;
            }
            yield return new WaitForSeconds(1 / 60);
        }
    }
    // Update is called once per frame
    void Update () {
    
    }

代码非常简单,没有什么好说明的

需要注意的一点就是

unity里面的动画,由animation controller去绑定n个animation clip,但是在这里是直接生成的,可以当成是代码实现的animation clip

posted @ 2015-03-31 15:33  自由出土文物  阅读(3142)  评论(0编辑  收藏  举报