Unity3D数字孪生笔记(二)——Unity篇

  • 一、常用API
    • 1、Component
    • 2、Transform
    • 3、GameObject
    • 4、Object
    • 5、Time
  • 二、预制件(Prefab)
  • 三、动画(Animation)
    • 1.Animation View
    • 2.创建动画片段
    • 3.录制动画片段
    • 4.时间线
    • 5.Animation组件属性
    • 6.常用API函数

一、常用API

1、Component

Component类提供了查找(在当前物体、后代、先辈)组件的功能。

  • 常用属性
    gameObject、transform、collider、renderer…
  • 常用方法
  • GetComponent、GetComponentInChild、GetComponentInParent…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// Component类提供了查找(在当前物体、后代、先辈)组件的功能。
/// </summary>
public class ComponentDemo : MonoBehaviour
{private void OnGUI(){if (GUILayout.Button("transform")){this.transform.position = new Vector3(0, 0, 10);}if (GUILayout.Button("GetComponent")){this.GetComponent<MeshRenderer>().material.color = Color.red;}if(GUILayout.Button("GetComponents")){//获取当前物体所有组件var allComponent = this.GetComponents<Component>();foreach (var item in allComponent){Debug.Log(item.GetType());}}if(GUILayout.Button("GetComponentsInChildren")){//获取后代物体的指定类型组件(从自身开始,从上往下)var allComponent = this.GetComponentsInChildren<MeshRenderer>();foreach (var item in allComponent){item.material.color = Color.red;}}if (GUILayout.Button("GetComponentsInParent")){//获取先辈物体的指定类型组件(从自身开始,从下往上)var allComponent = this.GetComponentsInParent<MeshRenderer>();foreach (var item in allComponent){item.material.color = Color.red;}}}
}

2、Transform

Transform类 提供了 查找(父、根、子(索引、名称))变换组件、改变位置、角度、大小功能。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// Transform类 提供了 查找(父、根、子(索引、名称))变换组件、改变位置、角度、大小功能
/// </summary>
public class TransformDemo : MonoBehaviour
{//***********查找变换组件****************//public Transform tf;private void OnGUI(){if(GUILayout.Button("foreach--transform")){foreach (Transform child in this.transform){//child为 每个子物体的变换组件print(child.name);}}if(GUILayout.Button("root")){//获取根物体变换组件Transform rootTF = this.transform.root;}if (GUILayout.Button("parent")){//获取父物体变换组件Transform parentTF = this.transform.parent;}if (GUILayout.Button("setparent")){//设置父物体,当前物体的位置 视为世界坐标//this.transform.SetParent(tf,true);//设置父物体,当前物体的位置 视为localpositionthis.transform.SetParent(tf,false);this.transform.SetParent(null);}if(GUILayout.Button("find")){//根据名称获取子物体Transform childTF =  this.transform.Find("子物体名称");//Transform childTF = this.transform.Find("子物体名称/子物体名称/子物体名称");}if (GUILayout.Button("getchild")){//根据索引获取子物体int count = this.transform.childCount;for (int i = 0; i < count; i++){Transform childTF = this.transform.GetChild(i);}            }//***********改变位置、角度、大小****************//if (GUILayout.Button("pos/scale")){//物体相对于世界坐标系原点的位置//this.transform.position//物体相对于父物体轴心点的位置//this.transform.localPosition//相对于父物体缩放比例  1  2  1//this.transform.localScale//lossyScale理解为:物体与模型缩放比例(自身缩放比例*父物体缩放比例)//this.transform .lossyScale//如:父物体localScale为3 当前物体localScale为2//    lossyScale则为6}if (GUILayout.Button("translate")){//向自身坐标系Z轴 移动1米this.transform.Translate(0, 0, 1);//向世界坐标系z轴 移动1米//this.transform.Translate(0, 0, 1, Space.World);}if (GUILayout.Button("rotate")){//向自身坐标系Z轴 旋转10°//this.transform.Rotate(0, 0, 10);//向世界坐标系y轴 旋转10°this.transform.Rotate(0, 10, 0, Space.World);}if (GUILayout.RepeatButton("rotatearound")){//围绕旋转:   点  轴  角度this.transform.RotateAround(Vector3.zero, Vector3.up, 1);}//练习:在层级未知情况下查找子物体//递归}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 层级未知情况下寻找依照名称寻找子物体
/// </summary>
public class FindChildrenByName : MonoBehaviour
{public static Transform FindChild(Transform current,string childname){Transform childTF = current.Find(childname);if (childTF != null) return childTF;for (int i = 0; i < current.childCount; i++){childTF = FindChild(current.GetChild(i), childname);if (childTF != null) return childTF;}return null;}
}

3、GameObject

Gameobject类提供了启用、禁用、新建、查找游戏对象的功能。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// gameobject类提供了 启用、禁用、新建、查找游戏对象的功能
/// </summary>
public class GameObjectDemo : MonoBehaviour
{public void OnGUI(){//在场景中物体激活状态(物体实际激活状态)//this.gameObject.activeInHierarchy//物体自身激活状态(物体在Inspector面板中的状态)//this.gameObject.activeSelf//设置物体启用/禁用//this.gameObject.SetActive()//this.gameObject.AddComponent<Light>()if (GUILayout.Button("添加光源")){//Light light = new Light();//创建物体GameObject lightGo = new GameObject();//添加组件Light light = lightGo.AddComponent<Light>();light.color = Color.red;light.type = LightType.Point;}//在场景中根据名称查找物体(慎用)//GameObject.Find("游戏对象名称");//获取所有使用该标签的物体//GameObject[] allEnemy =  GameObject.FindGameObjectsWithTag("enemy");//获取使用该标签的物体(单个)GameObject playGo = GameObject.FindWithTag("player");//练习:查找血量最低的敌人}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 寻找血量最低的敌人
/// </summary>
public class FindBleedLess : MonoBehaviour
{private void OnGUI(){if(GUILayout.Button("查找血量最低的敌人")){//查找场景中所有Ememy类型的引用Enemy[] allEnemy = Object.FindObjectsOfType<Enemy>();//获取血量最低的对象引用Enemy min = FindEnemyByMinHP(allEnemy);//根据Enemy引用获取其他组件类型引用min.GetComponent<MeshRenderer>().material.color = Color.red;}}//自己的方法public static Transform BleedLess(Transform current){float BleedLess = current.GetChild(0).GetComponent<Enemy>().HP;int count=0;for (int i = 1; i < current.childCount; i++){if (current.GetChild(i).GetComponent<Enemy>().HP < BleedLess){BleedLess = current.GetChild(i).GetComponent<Enemy>().HP;count = i;}                }return current.GetChild(count);}//参考答案public Enemy FindEnemyByMinHP(Enemy[] allEnemy){//假设第一个人就是血量最低的敌人Enemy min = allEnemy[0];//依次查找for (int i = 1; i < allEnemy.Length; i++){if (min.HP > allEnemy[i].HP)min = allEnemy[i];}return min;}
}

4、Object

Object类提供了按类型查找对象、销毁对象的功能。

  • 常用属性
    name…
  • 常用方法
    Instantiate、Destroy、FindObjectOfType、FindObjectsOfType…

5、Time

  • 从Unity获取时间信息的接口
  • 常用属性
    time:从游戏开始到现在所用时间。
    timeScale:时间缩放。
    deltaTime:以秒计算,表示每帧的经过时间。
    unscaledDeltaTime:不受缩放影响的每帧经过时间。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
///
/// </summary>
public class TimeDemo : MonoBehaviour
{public float a, b, c; //渲染场景时执行,不受TimeScale影响public void Update(){a = Time.time;//受缩放影响的游戏运行时间b = Time.unscaledTime;//不受缩放影响的游戏运行时间c = Time.realtimeSinceStartup;//实际游戏运行时间//每渲染帧执行1次,旋转1度//1秒旋转?度//帧多   1秒旋转速度快   希望1帧旋转角度小  (Time.deltaTime)//   少                    慢                          大//this.transform.Rotate(0, 100*Time.deltaTime, 0);//旋转速度*每帧消耗时间,可以保证旋转/移动速度不受渲染影响//个别物体不受影响,Time.unscaledDeltaTime 不受缩放影响的每帧间隔this.transform.Rotate(0, 100 * Time.unscaledDeltaTime, 0);}//固定0.02秒执行一次,与渲染无关,执行受TimeScale影响public void  FixedUpdate(){//this.transform.Rotate(0, 1, 0);}//游戏暂停  个别物体不受影响public void OnGUI(){if(GUILayout.Button("暂停游戏")){Time.timeScale = 0;}if (GUILayout.Button("继续游戏")){Time.timeScale = 1;}}
}

练习:倒计时

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;/// <summary>
///
/// </summary>
public class TwoSecondDemo : MonoBehaviour
{//需求:1秒修改一次Text文本内容//1、查找组件引用  UnityEnigine.UI.Text//2、定义变量:秒  int second =120;//3、时间转换:  02:00   01:59//Update中1秒修改一次private Text textTime;public int second = 120;private float i=0;private float nextTime = 1;//下次修改时间public void Start(){textTime = this.GetComponent<Text>();//重复调用(被执行的方法名称,第一次执行时间,每次执行间隔)InvokeRepeating("Timer1", 1, 1);//Invoke("被执行的方法名称",开始调用时间);}public void Update(){//Timer();}//方法一:Time.timepublic void Timer(){#region  上课答案Time.timeif (Time.time >= nextTime){second--;textTime.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60); //格式函数//设置下次修改时间nextTime = Time.time + 1;}#endregion}//方法二:InvokeRepeating()//每隔固定时间,重复执行public void Timer1(){second--;textTime.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60); //格式函数//设置下次修改时间if (second <= 0)CancelInvoke("Timer1");}//方法三:Time.deltaTimepublic void Timer2(){#region 自己的方法 Time.deltaTimei += Time.deltaTime;if (i >= 1){second--;if (second <= 10)textTime.color = Color.red;if (second == 0)Time.timeScale = 0;textTime.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60); //格式函数i = 0;}#endregion }//5、如何每秒修改一次//重点:在Update每帧执行的方法中,个别语句实现指定间隔执行一次。//Time.time
}

二、预制件(Prefab)

  • 一种资源类型,可以多次在场景进行实例。
  • 优点:对预制件的修改,可以同步到所有实例,从而提高开发效率。
  • 如果单独修改实例的属性值,则该值不在随预制件变化。
  • Selcet键:通过预制件实例选择对应的预制件
  • Revert键:放弃实例属性值,还原预制件属性值
  • Apply键:将某一实例的修改应用到所有实例

三、动画(Animation)

1.Animation View

  • 通过动画视图可以直接创建和修改动画片段(Animation Clips)。
  • 显示动画试图:Window——Animation。

2.创建动画片段

  • 为物体添加Animation组件
  • 在动画试图中创建片段

3.录制动画片段

  • 录制步骤:
    1.添加关键帧Add Property,选择组件类型

    2.点击录制按钮,开始录制动画

    3.选择关键帧,调整时间点

    4.在Scene或Inspector画板设置属性

    5.点击录制按钮,结束录制动画
    注意:录制动画时,时间线一定要拖到最后,否则默认为最开始,也就是倒着播放动画
  • 任何组件以及材质的属性都可进行动画处理,即使是自定义脚本组件的公共变量。

4.时间线

  • 可以点击时间线上的任意位置预览或修改动画片段。
  • 数字显示为秒数和帧数。
    例如:1:30表示1秒30帧
  • 使用按钮跳到上一个或下一个关键帧,也可以键入特定数直接跳到该帧。

5.Animation组件属性

  • 动画Animation:当前动画
  • 动画列表Animations:可以从脚本访问的动画列表
  • 自动播放Play Automatically:启动游戏时自动播放的动画

6.常用API函数

  • bool isPlay = animation.isPlaying;
  • bool isPlay = animation.IsPlaying(“动画名”);
  • animation.Play(“动画名”);//立即播放
  • animation.PlayQueued(“动画名”);//播放序列
  • animation.CrossFade(“动画名”);//淡入淡出
  • animation[“动画名”].speed = 5;
  • animation[“动画名”].wrapMode = WrapMode.PingPong;
  • animation[“动画名”].length;
  • animation[“动画名”].time;

Unity3D数字孪生笔记——Unity常用API篇相关推荐

  1. Unity3D数字孪生开发笔记——网络篇

    Unity3D数字孪生开发笔记(一) 一.网络连接的端点:Socket 二.端口 三.Socket通信流程 四.TCP和UDP 一.网络连接的端点:Socket 1.网络上的两个程序通过一个双向的通信 ...

  2. 大爱 unity 数字孪生 老卵了 Unity 数字孪生笔记1 工具介绍

    Unity 数字孪生笔记1 工具介绍 火锅肥牛 2020-05-16 20:19:10   106   收藏 1 展开 前言 工欲善其事必先利其器 流程思考 数字孪生本质上一种基于实际物理数据的可视化 ...

  3. Unity常用API详解--初学必备

    初学Unity3D编程,为了更加熟悉Unity常用API,根据搜集资料整理如下: 1.事件函数执行机制 2.Time类 Time.deltaTime:每一帧的时间. Time.fixedDeltaTi ...

  4. unity基础学习九,Unity常用API

    1.Event Function:事件函数 Reset() :被附加脚本时.在游戏物体的组件上按Reset时会触发该事件函数 Start() :在游戏初始化时会执行一次 Update() :每一帧都会 ...

  5. Unity 数字孪生笔记2.1 PiXYZ Studio 工作流简介

    前言 UnReferenceException 为什么引入Pixyz Studio Pixyz是Unity的模型导入工具之一,但是Pixyz作为业界领先的模型处理工具,其产品不止有Pixyz plug ...

  6. Unity 数字孪生笔记1 工具介绍

    前言 工欲善其事必先利其器 流程思考 数字孪生本质上一种基于实际物理数据的可视化方案,通过对接实际数据,在三维界面中展示孪生结果. 那么这个流程就很简单了. 整个流程分为三块:数据来源->数据分 ...

  7. Unity—常用API(重点)

    利用将近两天的时间学习并整理了常用API的知识点! 每日一句:能打动人的从来不是花言巧语,而是恰到好处的温柔以及真挚的内心 目录 脚本生命周期流程图(续上) 常用API 核心类图 *Component ...

  8. Unity常用API

    一.事件函数执行的先后顺序 FixedUpdate每秒执行固定次数,应该将处理跟物理相关的运动的代码放在FixedUpdate中. Update和LateUpdate跟硬件和代码性能有关,每秒执行次数 ...

  9. 【机器学习】手写数字识别学习笔记(对三篇文件进行分析记录)

    看了许多网站,涉及的知识也非常多(毕竟是机器学习"第一题"),真正能理解并掌握,或者说记住的内容,并不多. 一.分类-MNIST(手写数字识别) 1.数据集:博客的网盘链接中50多 ...

最新文章

  1. pandas使用pad函数向dataframe特定数据列的每个字符串添加后置(后缀)补齐字符或者字符串、向所有字符串的右侧填充、直到宽度达到指定要求(right padding)
  2. dedecms 漏洞_代码审计之二次漏洞审计
  3. oracle启动的服务有哪些,启动/关闭oracle服务有三种方式
  4. 河北科技创新平台年报系统 - 头脑风暴会
  5. 使用Yeoman定制前端脚手架
  6. Git 搭建私有仓库
  7. Thread 中 ThreadLocal 源码解读
  8. 检测字符串包含emoji表情
  9. hdu 2196 computer
  10. 程序员下班回家,路上被拦…
  11. React基础篇(三)之 webpack打包项目配制
  12. MySQL中单行函数concat_MySQL内置函数-单行函数(字符函数)
  13. iOS开发日记24-详解RunLoop
  14. 计算机组成原理 实验报告
  15. uCos中的信号量机制
  16. android 客户端和服务端cookie,如何在Android客户端注入及清除Cookie教程
  17. 【离散数学】二元关系中的自反闭包,对称闭包,传递闭包
  18. [转]现代密码学实践指南
  19. matlab使用矩形窗设计一个具有线性相位的低通数字滤波器,matlab结合矩形窗设计fir滤波器.doc...
  20. SLAM中Bundle Adjustment与图优化

热门文章

  1. OracleP6机场工程进度控制系列17:机场工程建设总进度计划清单
  2. 使用抓包工具下载有下载限制的视频
  3. 弱网工具ATC使用总结
  4. 大过年的生产项目频繁fullgc
  5. 常见咳嗽种类、治疗方法和忌食大全
  6. 果蔬识别轻松帮你区分车厘子与大樱桃
  7. MYQQA 框架开源源码
  8. QGIS一键加载100万基础地理信息数据导出
  9. Java遍历Map集合的第二种方法Entry对象遍历Map集合内元素
  10. 计算机CQ,什么是Cq(Ct)值?