Unity重要脚本函数

Unity重要脚本函数

一:Unity事件函数:

Unity中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制函数的回调,这就是事件函数。

对于初学者而言:先介绍最常用的两个:

Start:在Update函数之前进行调用,本函数仅调用一次。

Update:每帧执行一次,这是最常用的事件函数,大约一秒钟执行30-60次,依据个人计算机的性能而不同。

二:GameObject类:

GameObject类是所有Unity场景中的基类,但不是最终根类,它继承Object类,GameObject类最常用的属性与方法如表:

 1.下面的实例展示对象的创建 克隆 销毁对象实例脚本:

/**
*    Title:"":项目
*          主题 :Unity脚本程序基础
*    Description:
*          功能:创建 克隆 销毁对象实例脚本
*    Date:2017.11.06
*    Version:Unity5.5.4
*    Modify Recoder:
*    Operator:HaoYuHang
*
**/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Demo6_3DMaths : MonoBehaviour
{

    #region 字段和属性定义

    private GameObject _goCloneObj;

    #endregion

    void Start()
    {
        //创建游戏对象
        GameObject goCreatObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //材质添加
        goCreatObj.GetComponent<Renderer>().material.color = Color.red;
        //添加名称
        goCreatObj.name = "CubeName";
        //对象的克隆
        _goCloneObj = GameObject.Instantiate(goCreatObj);
    }
    void Update()
    {
        //对象的销毁
        if (Input.GetKey(KeyCode.D))
        {
            //2秒后销毁物体
            GameObject.Destroy(_goCloneObj, 2f);
        }
    }

    #region 公共方法的定义

    #endregion

    #region 私有方法的定义

    #endregion

}//class_end

2.添加组件脚本:

/**
*    Title:"":项目
*          主题 :Unity脚本程序基础
*    Description:
*          功能:动态的添加脚本
*    Date:2017.11.06
*    Version:Unity5.5.4
*    Modify Recoder:
*    Operator:HaoYuHang
*
**/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Demo7_3DMaths : MonoBehaviour
{

    #region 字段和属性定义
    

    #endregion

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            //动态的添加一个脚本
            this.gameObject.AddComponent<Demo7_3DMaths_2>();
        }
    }

    #region 公共方法的定义

    #endregion

    #region 私有方法的定义

    #endregion

}//class_end
/**
*    Title:"":项目
*          主题 :Unity脚本程序基础
*    Description:
*          功能:物体的旋转
*    Date:2017.11.06
*    Version:Unity5.5.4
*    Modify Recoder:
*    Operator:HaoYuHang
*
**/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Demo7_3DMaths_2 : MonoBehaviour
{

    #region 字段和属性定义

    #endregion

    void Update()
    {
        //物体旋转的代码
        this.transform.Rotate(Vector3.up, Space.World);
    }

    #region 公共方法的定义

    #endregion

    #region 私有方法的定义

    #endregion

}//class_end

3.获取组件脚本:

/**
*    Title:"":项目
*          主题 :Unity脚本程序基础
*    Description:
*          功能:获取当前游戏对象的脚本组件
*    Date:2017.11.06
*    Version:Unity5.5.4
*    Modify Recoder:
*    Operator:HaoYuHang
*
**/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Demo8_3DMaths : MonoBehaviour
{

    #region 字段和属性定义

    #endregion

    void Start()
    {
        //使用GetComponent方法,做类之间的数值传递
        int result = this.gameObject.GetComponent<Demo8_3DMaths_2>().GetValues();
        string strRes = this.gameObject.GetComponent<Demo8_3DMaths_2>()._str;
        print(string.Format("得到数值:{0}", result));
        print(string.Format("得到字符串:{0}", strRes));
    }

    #region 公共方法的定义

    #endregion

    #region 私有方法的定义

    #endregion

}//class_end
/**
*    Title:"":项目
*          主题 :Unity脚本程序基础
*    Description:
*          功能:被获取的脚本
*    Date:2017.11.06
*    Version:Unity5.5.4
*    Modify Recoder:
*    Operator:HaoYuHang
*
**/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Demo8_3DMaths_2 : MonoBehaviour
{

    #region 字段和属性定义

    [HideInInspector]
    public string _str = "大家好";

    #endregion

    #region 公共方法的定义

    /// <summary>
    /// 得到数值
    /// </summary>
    /// <returns>返回结果</returns>
    public int GetValues()
    {
        return 10;
    }

    #endregion

}//class_end

4:游戏对象的查找:

/**
*    Title:"":项目
*          主题 :Unity脚本程序基础
*    Description:
*          功能:游戏对象的查找的脚本
*    Date:2017.11.06
*    Version:Unity5.5.4
*    Modify Recoder:
*    Operator:HaoYuHang
*
**/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Demo9_3DMaths : MonoBehaviour
{

    #region 字段和属性定义


    #endregion

    void Start()
    {
        //按照“名称”进行全局的查找(不推荐使用)
        GameObject goObj1 = GameObject.Find("Cube");
        //颜色的改变
        goObj1.GetComponent<Renderer>().material.color = Color.red;
        //按照“标签”进行查找
        GameObject goObj2 = GameObject.FindWithTag("Sphere");
        goObj2.GetComponent<Renderer>().material.color = Color.blue;
        //按照“标签”查找返回对象数组
        GameObject[] goObj3 = GameObject.FindGameObjectsWithTag("Capsule");
        //使用循环遍历改变颜色
        foreach (var goObj in goObj3)
        {
            goObj.GetComponent<Renderer>().material.color = Color.green;
        }
    }

}//class_end

 MonoBehaviour类:

在这里我们只是列举比较常用的方法和方法。

(1):实例方法:Invoke():调用函数。

(2):实例方法:InvokeRepeating():重复调用函数

(3):静态方法:Print():后台打印输出消息

请看下面的实例代码:

public class Demo12_Invoke : MonoBehaviour
{

    void Start()
    {
        //调用函数:3f表示调用的时间
        Invoke("DisplayInfo", 3f);

        //重复调用函数
        //参数的含义:
        //5f:表示延迟调用的时间
        //1f:表示重复调用函数的间隔的时间
        InvokeRepeating("DisplayInfo_2", 5f, 1f);
    }

    private void DisplayInfo()
    {
        print("Display()方法被执行了");
    }

    private void DisplayInfo_2()
    {
        print("Display_2()被执行了");
    }

}//class_end

Transform类:

Transform类继承了Component类,在场景中的每个对象,其属性窗口中都有一个变换,用来存储和处理游戏对象的位置、旋转和缩放的信息,如下图所示:

实例代码如下:

    void Start()
    {
        //调整当前的游戏对象的位置、旋转、缩放
        this.transform.position = new Vector3(2f, 3f, 4f);
        this.transform.eulerAngles = new Vector3(45f, 45f, 45f);
        this.transform.localScale = new Vector3(3f, 3f, 3f);
    }

Time类:

Time为Unity提供的时间类,用以记录、控制游戏项目中和时间有关系的操作。我们在这里只给出一个属性的详细的介绍:Time.deltaTime。

deltaTime:按照秒来计数,完成上一帧的时间(只读),常用使用这个函数产生于游戏帧速率无关的效果(使得游戏帧率独立)。如果我们加或者减一个值的时候,那么你就应该乘以时间,但是当我们乘以Time.deltaTime表示:我想移动这个物体10米每秒,而不是10米每帧。

public class Demo14_Time : MonoBehaviour
{

    /// <summary>
    /// Update事件函数:单位时间内执行的次数是不一样的,与计算机的性能和具体的分配的资源有很大的关系
    /// </summary>
    void Update()
    {
        print(string.Format("Time.DelteTime:{0}", Time.deltaTime));

        if (Input.GetKey(KeyCode.W))
        {
            this.transform.Translate(Vector3.forward * Time.deltaTime, Space.World);
        }
        else if (Input.GetKey(KeyCode.S))
        {
            this.transform.Translate(Vector3.back * Time.deltaTime, Space.World);
        }
    }
}//class_end
原文地址:https://www.cnblogs.com/MoRanQianXiao/p/7791861.html