文章目录
- transform
- 摄像机camera
- skybox
- 相机权重(depth)
- Audio source
- video player
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("继续播放");
}
}
}
}