// Start is called before the first frame update voidStart()//唯一调用,调用时间在第一次OnEnable之后 { Debug.Log("start"); }
// Update is called once per frame voidUpdate()//每一帧输出一次,不要轻易使用 { } voidLateUpdate()//每次Update执行完都会执行一遍 { } voidFixedUpdate()//每隔固定一段时间调用一次,与帧无关,和性能无关,在编辑中的项目设定中的时间进行修改 {
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassDebugTest : MonoBehaviour { // Start is called before the first frame update voidStart() { Debug.Log("test"); Debug.LogWarning("test2"); Debug.LogError("test3"); }
// Update is called once per frame voidUpdate() { //绘制一条线,起点,终点;只有调试时能看见,在游戏中是看不见的 Debug.DrawLine(new Vector3(1, 0, 0), new Vector3(1, 1, 0), Color.blue);//颜色设置为蓝色 //绘制一条射线 起点,射线 Debug.DrawRay(new Vector3(1, 0, 0), new Vector3(1, 1, 0), Color.red);//前面的两个参数可以用Vector3.zero、Vector3.up等 } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassAudioTest : MonoBehaviour { //AudioClip public AudioClip music;//;音乐1 public AudioClip se;//音效1
//播放器组件 private AudioSource player;
// Start is called before the first frame update voidStart() { player = GetComponent<AudioSource>();//获取到播放器组件 player.clip = music;//设定播放的音频片段 //循环播放 player.loop = true; //设置音量 player.volume = 0.5f; //播放 player.Play(); }
// Update is called once per frame voidUpdate() { //按空格切换声音的播放和暂停 if(Input.GetKeyDown(KeyCode.Space)) { //如果当前正在播放声音 if(player.isPlaying) { //暂停 player.Pause(); //player.Stop();停止播放,下次从头开始播放 } else { //继续播放 player.UnPause(); //player.Play(); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video;
publicclassVideoTest : MonoBehaviour { private VideoPlayer player; // Start is called before the first frame update voidStart() { player = GetComponent<VideoPlayer>(); }
// Update is called once per frame voidUpdate() { if(Input.GetMouseButtonDown(0)) { player.Pause();//操作基本类似音频 } } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassPlayerControl : MonoBehaviour { private CharacterController player; // Start is called before the first frame update voidStart() { player = GetComponent<CharacterController>(); }
// Update is called once per frame voidUpdate() { //水平轴 float horizontal = Input.GetAxis("Horizontal"); //垂直轴 float vertical = Input.GetAxis("Vertical"); //创建成一个方向向量 Vector3 dir = new Vector3(horizontal, 0, vertical); //Debug.DrawRay(transform.position, dir, color.red);
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassTriggerControl : MonoBehaviour { // Start is called before the first frame update voidStart() { }
// Update is called once per frame voidUpdate() { } //注意碰撞传的参数是Collision类型,返回的是物体,要得到碰撞物体的碰撞器还要collision.collider privatevoidOnTriggerEnter(Collider other)//这里是直接给出进入触发的物体的触发器 { //此处实现让一个叫做door的物体消失,最常用的游戏制作用法之一 GameObject door = GameObject.Find("Door"); if(door!=null) { door.SetActive(false); } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassRayTest : MonoBehaviour { // Start is called before the first frame update voidStart() { //方式1 Ray ray = new Ray(Vector3.zero, Vector3.up);//以前一个点作为出发点,后一个点作为发射方向 }
// Update is called once per frame voidUpdate() { //方式2 if(Input.GetMouseButtonDown(0))//按下鼠标左键时候发出射线 { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //声明一个碰撞信息类 RaycastHit hit; //碰撞检测 bool res = Physics.Raycast(ray, out hit); //如果碰撞到的情况下,hit就有内容了 if(res == true) { Debug.Log(hit.point); transform.position = hit.point;//当前物体的位置移动到射线碰撞的位置 } //多检测 //RaycastHit[] hits = Physics.RaycastAll(ray,100,1<<10);//检测射线碰到的所有物体,每碰到一个物体就hits就多上一个,100是检测距离,1<<10代表就检测第10图层,非第十图层不检测 } } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassAnimatorTest : MonoBehaviour { private Animator animator; // Start is called before the first frame update voidStart() { animator = GetComponent<Animator>();//获取动画器组件,只需要一次 }
// Update is called once per frame voidUpdate() { if(Input.GetMouseButtonDown(0)) { animator.Play("right");//播放相应的动画 } } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassPlayerController : MonoBehaviour//基本动画操作 { // Start is called before the first frame update voidStart() { }
// Update is called once per frame voidUpdate() { if(Input.GetKeyDown(KeyCode.F))//按下F键 { //触发pickup参数 GetComponent<Animator>().SetTrigger("pickup");//触发叫做pickup的条件参数 } } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
publicclassPlayerControl1 : MonoBehaviour { //反向动力学(IK)演示对象 public Transform target;
private Animator animator;//设置动画组件成员,人真的跑起来了 // Start is called before the first frame update voidStart() { animator = GetComponent<Animator>();//获取组件 }
// Update is called once per frame voidUpdate() { //水平轴 float horizontal = Input.GetAxis("Horizontal"); //垂直轴 float vertical = Input.GetAxis("Vertical"); //向量 Vector3 dir = new Vector3(horizontal, 0, vertical); //当用户按下了方向键 if(dir != Vector3.zero) { transform.rotation = Quaternion.LookRotation(dir);//面向向量 //播放跑步动画 animator.SetBool("IsRun", true); //朝向前方移动 transform.Translate(Vector3.forward * 2 * Time.deltaTime);//转成每秒移动 } else { //播放站立动画 animator.SetBool("IsRun", false); }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
publicclassPlayerControl2 : MonoBehaviour { private NavMeshAgent.agent;//代理组件 // Start is called before the first frame update voidStart() { //获取代理组件 agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame voidUpdate() { //如果按下鼠标 if(Input.GetMouseButtonDown(0)) { //获取点击位置 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;//判断射线是否碰到物体 if(Physics.Raycast(ray, out hit))//射线检测,如果检测到射线返回true证明检测成功 { //点击位置 Vector3 point = hit.point;//就是鼠标的点击位置 //设置该位置为导航目标点 agent.SetDestination(point); } } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; publicclassDropDownTest : MonoBehaviour { // Start is called before the first frame update voidStart() { //获取下拉组件 Dropdown dropdown = GetComponent<Dropdown>(); //获取组件的选项 List<Dropdown.OptionData> options = dropdown.options; //修改选项 options.Add(new Dropdown.Option("俄罗斯")); dropdown.options = options;//加完后的选项赋值上去 }
// Update is called once per frame voidUpdate() { } }