zoukankan      html  css  js  c++  java
  • 协程

    如何创建协程并使用它们来实现复杂的行为。

    CoroutinesExample

    using UnityEngine;
    using System.Collections;
    
    public class CoroutinesExample : MonoBehaviour
    {
        public float smoothing = 1f;
        public Transform target;
    
    
        void Start ()
        {
            StartCoroutine(MyCoroutine(target));
        }
    
    
        IEnumerator MyCoroutine (Transform target)
        {
            while(Vector3.Distance(transform.position, target.position) > 0.05f)
            {
                transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);
    
                yield return null;
            }
    
            print("Reached the target.");
    
            yield return new WaitForSeconds(3f);
    
            print("MyCoroutine is now finished.");
        }
    }

    PropertiesAndCoroutines

    using UnityEngine;
    using System.Collections;
    
    public class PropertiesAndCoroutines : MonoBehaviour
    {
        public float smoothing = 7f;
        public Vector3 Target
        {
            get { return target; }
            set
            {
                target = value;
    
                StopCoroutine("Movement");
                StartCoroutine("Movement", target);
            }
        }
    
    
        private Vector3 target;
    
    
        IEnumerator Movement (Vector3 target)
        {
            while(Vector3.Distance(transform.position, target) > 0.05f)
            {
                transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);
    
                yield return null;
            }
        }
    }

    ClickSetPosition

    using UnityEngine;
    using System.Collections;
    
    public class ClickSetPosition : MonoBehaviour
    {
        public PropertiesAndCoroutines coroutineScript;
    
    
        void OnMouseDown ()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
    
            Physics.Raycast(ray, out hit);
    
            if(hit.collider.gameObject == gameObject)
            {
                Vector3 newTarget = hit.point + new Vector3(0, 0.5f, 0);
                coroutineScript.Target = newTarget;
            }
        }
    }
  • 相关阅读:
    BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
    BZOJ 2705: [SDOI2012]Longge的问题 | 数论
    BZOJ 1257[CQOI2007]余数之和sum | 数论
    BZOJ 3781: 小B的询问 | 莫队
    文件切割与合并
    [科普贴]为何Flash被淘汰?附Chrome看视频最完美教程!
    JQ模仿select
    JS正则表达式
    Vuejs——Vue生命周期,数据,手动挂载,指令,过滤器
    Vuejs——v-on
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/14143030.html
Copyright © 2011-2022 走看看