• 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

  • 相关阅读:
    【题解】 P1373 小a和uim之大逃离
    题解 CF576C 【Points on Plane】
    题解 P4799 【[CEOI2015 Day2]世界冰球锦标赛】
    【题解】[JSOI2008]最大数
    题解 P3389 【【模板】高斯消元法】
    【模板】矩阵加速
    【模板】树状数组上的差分数组
    tarjan求强连通分量(模板)
    我好菜系列——map查找
    trie树的应用;
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/4381070.html
Copyright © 2020-2023  润新知