Unity 5.X 3D游戏开发技术详解与典型案例—— 第三章《Unity脚本程序开发》

本文详细介绍了Unity 5.X中3D游戏开发的脚本程序技术,涵盖Awake、Start、协程、Transform与Rigidbody组件的使用,以及Time、GetComponent、GameObject查找、Vector3、MonoBehaviour类等多个关键知识点,旨在帮助开发者掌握Unity的高效编程技巧。
摘要由CSDN通过智能技术生成

目录

使用 Awake 或 Start 方法初始化

Unity脚本中协同程序有不同的语法规则

尽量避免使用构造函数

print() 和 Debug.Log()、Debug.Log. Warning() 、Debug.LogErroro()、Debug.Break()  操作 ( 73P)

修改 Transform 和 Rigidbody 组件 中的属性来对游戏对象进行操作 (73P)

使用 Time 类 来记录时间 (74P)

使用 GetComponent 方法 获取到游戏对象上的 组件 (75P)

通过 Transform.Find 来 需找 子对象 或 父对象 (76P)

通过 Transform.Find 来 获取 子对象 上的组件 (76P)

如何获取当前脚本自身所在的GameObject?

使用 GameObject.FindWithTag 和 GameObject.Find 方法 来 通过 游戏物体的名字 或 标签 来 查找对象 (77P)

使用 gameObject.SetActive 方法设置物体是否为 active

如何用代码创建空物体?

使用 GameObject.CreatePrimitive 创建内置几何体

使用 OnTriggerStay 函数 为 刚体 施加力 (77P)

使用 FindObjectOfType 和 FindObjectsOfType  函数 来查找到挂载特定类型组件的游戏对象 (78P)

使用不同的方式 初始化 Vector3 向量类型 ( 78P )

Vector3 类 中的常量、以及可调用的方法 (78P)

成员变量 和 static 成员变量 、(79P)

Destroy、DestroyImmediate 、Instantiate 方法 ( 80P)

使用 StartCoroutine 方法 来启动一个协同程序  (80P)

MonoBehaviour 类 中可调用的函数 (82P)

MonoBehaviour 类 中可调用的成员变量 (82P)

使用 transform.Rotate 使一个物体顺时针旋转

使用 GameObject.Instantiate 实例化Prefab

使用 Vector3.MoveTowards 向前移动

通过 GameObject. FindGameObjectWithTag 通过标签来查找游戏对象


 


使用 Awake 或 Start 方法初始化


必须使用 Awake 或 Start 方法 来初始化脚本的代码

两者的不同之处在于

  •  Awake方法是在加载场景时运行;
  • Start 方法是在第一次调用 Update 或 FixedUpdate 方法之前被调用;
  • Awake 方法运行在所有 Start 方法之前。

Unity脚本中协同程序有不同的语法规则


  • Unity脚本中协同程序(Coroutines)必须是 IEnumerator返回类型, 并且 yield 被yield return替代

尽量避免使用构造函数


  • 不要在构造函数中初始化任何变量, 要用 Awake 或 Start 方法来实现。

print() 和 Debug.Log()、Debug.Log. Warning() 、Debug.LogErroro()、Debug.Break()  操作 ( 73P)


在Unity中,可以使用 print() 和 Debug.Log() 打印调试信息。

print() 和 Debug.Log()  不同在于?

  • print() 只能在  继承了 Monobehaviour 的类中使用 
  • 但是 Debug.Log()  可以在任何地方使用。所有一般情况下最好使用Debug.Log(),它和print()效果一样。
  • 使用 Debug.Log. Warning() 可以收集警告
  • 使用 Debug.LogErroro()可以收集错误信息。
  • 使用 Debug.Break() 可以设置断点。如果想查看特定情况发生时 对象属性 的变化时, 用断点可以快速地完成。

修改 Transform 和 Rigidbody 组件 中的属性来对游戏对象进行操作 (73P)


使一个游戏物体旋转: 

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

public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(20, 0, 0); // 让该脚本所在的游戏物体 绕 x 轴旋转 20 度
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(0, 0, 1); // 让该脚本所在的游戏物体 每帧 向前移动1个单位长度
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        // 第四个参数是可选的,默认情况下是 Space.Self
        this.transform.Rotate(5, 0, 0, Space.World); // 该物体 相当于绕世界坐标系进行旋转
        this.transform.Translate(5, 0, 0, Space.Self); // 相当于饶该物体的自身坐标系进行旋转
    }
}

使用 Time 类 来记录时间 (74P)


Time 类中有一个 名为 deltaTime (只读) 的变量, 该变量指的是 从最近一次调用 Update  或者 FixedUpdate  方法到现在所经过的时间。

如果想均匀地旋转一个物体,不考虑帧速率的情况下, 可以乘以 Time.deltaTime .

public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(10 * Time.deltaTime, 0, 0); // 绕X 轴 均匀的旋转
    }
}

public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(0, 0, 1*Time.deltaTime); // 绕Z 轴 均匀的平移
    }
}
public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        // this.transform.Translate(0,  5*Time.deltaTime,0);
        Vector3 te = gameObject.transform.position; // 获取游戏对象的位置坐标
        te.y += 5 * Time.deltaTime; // 该物体 沿 y 轴 每秒 上升 5 个单位
        gameObject.transform.position = te; // 设置游戏对象的位置坐标
    }
}

public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        // this.transform.Translate(0,  5*Time.deltaTime,0);
        //gameObject.GetComponent: 如果游戏对象有一个附加组件,则返回Type类型的组件,如果没有,则返回null。
        Vector3 te = gameObject.GetComponent<Rigidbody>().transform.position;
        te.y += 5 * Time.fixedDeltaTime; // 刚体 沿 y 轴 每秒 上升 5 个单位
      gameObject.GetComponent<Rigidbody>().transform.position = te; // 设置刚体的位置坐标
    }
}

使用 GetComponent 方法 获取到游戏对象上的 组件 (75P)


public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    { // 下面的两段代码是等价的,都 沿着 x 轴 移动一个单位
        transform.Translate(1, 0, 0); // 这句代码是 通过 Transform 组件的 transform 属性 调用 Translate 向 x 轴移动一个单位
        GetComponent<Transform>().Translate(1, 0, 0); // 首先获取到 Transform 组件,然后移动
    }
}

通过 Transform.Find 来 需找 子对象 或 父对象 (76P)


public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        transform.Find("Sphere").Translate(0, 0, 1); // 首先找到该子对象,然后 向 Z 轴每帧移动一个单位
        transform.parent.Translate(0, 0, 1); // 找到父对象,然后将其 沿z 轴每帧移动一个单位
    }
}

以上是资源连接 ! Unity 5.X 3D游戏开发技术详解典型案例Unity 3D集成开发环境界面、脚本的编写和众多高级特效的实现进行了详细介绍,内容深入浅出,是一本适合不同需求、不同开发水平读者的技术宝典。 全书共分16章。第1章主要介绍了Unity 3D的诞生、特点、开发环境的搭建及运行机制;第2章对Unity 3D集成开发环境进行了详细介绍;第3章介绍了Unity 3D脚本的编写;第4章主要对Unity 3D开发过程中经常使用的组件及对象进行了详细介绍;第5章介绍了Unity游戏开发中非常流行的第三方UI界面开发组件库—NGUI的基础知识;第6章介绍了Unity开发平台的完整的物理引擎体系;第7章介绍了Unity 3D中的着色器和着色器语言—ShaderLab;第8章介绍了天空盒、虚拟按钮与摇杆、声音、水特效、3D拾取、重力加速度传感器及雾特效等开发常用的技术;第9章介绍了Unity中经常使用的光影效果,主要包括各种光源、光照烘焙、法线贴图、镜面特效、波动水面真实效果等技术;第10章介绍了Unity中模型的网格概念及新旧动画系统;第11章介绍了Unity自带的地形引擎、拖尾渲染及导航网格和寻路系统等知识;第12章介绍了AssetBundle更新资源包的使用;第13章介绍了Unity中的多线程技术与网络开发;第14章介绍了Unity 2D游戏开发工具;第15章介绍了Unity 3D提供的Profiler工具的使用方法,及断点调试的两种方式;第16章介绍了完整的大型3D游戏案例—指间足球。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值