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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM