文章目录
transform
不但控制着组件的旋转、位置、缩放并且还控制着组件间的父子关系
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TransformTest : MonoBehaviour
{
void Start()
{
/*
//获取位置
Debug.Log(transform.position);
Debug.Log(transform.localPosition);
//获取旋转
Debug.Log(transform.rotation);
Debug.Log(transform.localRotation);
Debug.Log(transform.eulerAngles);
Debug.Log(transform.localEulerAngles);
//获取缩放
Debug.Log(transform.localScale);
//向量
Debug.Log(transform.forward);
Debug.Log(transform.right);
Debug.Log(transform.up);
*/
//父子关系
//获取父物体
GameObject parentGameObject = transform.parent.gameObject;
//子物体个数
Debug.Log(transform.childCount);
//解除与子物体的父子关系
transform.DetachChildren();
//获取子物体
Transform trans = transform.Find("Child");
trans = transform.GetChild(0);
//判断一个物体是不是另外一个物体的子物体
bool res = trans.IsChildOf(transform);
Debug.Log(res);
//设置为父物体
trans.SetParent(transform);
}
void Update()
{
//时时刻刻看向000点,敌人看向玩家,或者玩家看向物品
// transform.LookAt(Vector3.zero);
//旋转
//每帧旋转1度
// transform.Rotate(Vector3.up,1);
//绕某个物体旋转
//绕up轴旋转
// transform.RotateAround(Vector3.zero, Vector3.up,1);
//移动
//每一帧移动0.1距离
// transform.Translate(Vector3.forward * 0.1f);
}
}
摄像机camera
透视(3D)
正交(2D)
skybox
需要商城下载一点素材
相机权重(depth)
depth(深度)值越高,越优先显示
Audio source
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioTest : MonoBehaviour
{
//AudioClip
public AudioClip music;
//播放器组件
private AudioSource player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<AudioSource>();
//设定播放的音频片段
player.clip = music;
//循环
player.loop = true;
//音量
player.volume = 0.5f;
//播放
player.Play();
}
// Update is called once per frame
void Update()
{
//按空格切换声音的播放和暂停
if (Input.GetButtonDown("Jump"))
{
if (player.isPlaying)
{
// player.Pause(); //暂停播放
player.Stop();
}
else
{
// player.UnPause() //继续播放
player.Play(); //重头播放
}
}
//按鼠标左键播放声音
if (Input.GetMouseButtonDown(0))
{
// 这个播放是异步操作,非常方便
player.PlayOneShot(music);
}
}
}
video player
做了一个游戏里面看电视的功能,没什么难度,说一点
屏幕用的plane模型,直接贴在TV前面
video直接放在plane上面的video player组件即可,脚本也挂在plane上面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
public class VideoTest : MonoBehaviour
{
private VideoPlayer player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<VideoPlayer>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (player.isPlaying)
{
player.Pause();
Debug.Log("停止播放");
}
else
{
player.Play();
Debug.Log("继续播放");
}
}
}
}
加载网络url
只能加载mp4结尾的url,不能加载html结尾的url,html结尾的是视频流文件,不能读取
测试url
mp4文件url扫描二维码关注公众号,回复: 14901159 查看本文章
character controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private CharacterController player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//水平轴
float horizontal = Input.GetAxis("Horizontal");
//垂直轴
float vertical = Input.GetAxis("Vertical");
//创建一个方向向量 ,水平轴horizontal影响x的位置,y轴是上天入地,不需要受影响(0),垂直轴vertical影响z的位置
Vector3 dir = new Vector3(horizontal,0,vertical);
Debug.DrawRay(transform.position,dir,Color.red);
//朝向该方向移动
player.SimpleMove(dir);
}
}
关节组件
Hinge Joint(铰链关节)
固定某个关节
Spring Joint(弹簧关节)
把一个物体挂载到另一个物体上
Fixed Joint(固定关节)
需要指定一个力,来打破这个关节
Animation、Animator
旧版(Animation)
与pr一样,使用关键帧来制作