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;
            }
        }
    }
  • 相关阅读:
    A very good site containing a lot of wonderful videos from Microsoft, List_of_GUI_testing_tools,
    java试用(3)awt,UI
    deletion of pointer to incomplete type 'A'; no destructor called
    windbg
    semaphore与Mutex
    Display a Web Page in a Plain C Win32 Applicatio
    java试用(1)hello world
    Linux opensshserver,
    Toggle hardware data/read/execute breakpoints programmatically
    RTThread RTOS
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/14143030.html
Copyright © 2011-2022 走看看