Unity Random Class and Invork function

Unity 学习笔记汇总
Random官方API使用文档

1. Random Class

1.1. Foreground

在这里插入图片描述

1.2. Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject prefab;

    public void Awake()
    {
        //Debug.Log("Awake");
        //Instantiate(prefab, Vector3.zero, Random.rotation); //克隆
        //print(Random.value);
    }
    /*
    Description
    Awake is called when the script instance is being loaded.

    Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using for example GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

     Note: Use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time. Awake is called once, just like the constructor.
     */


    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log("Start");

        //Vector3 position = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
        //Instantiate(prefab, position, Quaternion.identity);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            Vector3 position = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
            Instantiate(prefab, position, Quaternion.identity);
        }
    }
}

1.3. Result

When C
按下C键会新克隆一个Cube,并且新Cube的位置随机
在这里插入图片描述

2. Invork function

2.1. Foreground

在这里插入图片描述

2.2. Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class t2 : MonoBehaviour
{

    public GameObject cubePrefab, cloneObj;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(Time.time);

        Invoke("CloneCubeObj", 1f);
        Invoke("Function2", 2f);

        InvokeRepeating("CloneCubeObj", 2f, 1f);


    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            if (IsInvoking("CloneCubeObj"))
            {
                CancelInvoke("CloneCubeObj");
            }
        }
    }

    void CloneCubeObj()
    {
        Debug.Log(Time.time);
        cloneObj = Instantiate(cubePrefab, new Vector3(Random.Range(-3, 3), Random.Range(-2, 2), 3), cubePrefab.transform.rotation) as GameObject;
    }

    void Function2()
    {
        Debug.Log("Function2");
        Debug.Log(Time.time);

        if (cloneObj != null)
        {
            cloneObj.transform.GetComponent<BoxCollider>().enabled = false;
        }
    }
}

2.3. Result

按下C键会新克隆一个Cube,并且新Cube的位置随机
在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读