- Input.GetAxis() , “Mouse X” "Mouse Y" ,"Horizontal" ,"vertical" ,得到鼠标/键盘造成的一个偏移量,是float类型
- Ray ray = ... toray(point) 中 ray是一种代码对象,往往和Gameobject混谈
- 可以利用携程进行暂停,更重要的是可以分帧加载某种资源
- 暂停代码——被射线集中的某个东西在1S后留下一个球(子弹印)
void Update () {
if (Input.GetMouseButtonDown(0))
{
//Vector3 point = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
Vector3 point= Input.mousePosition;
Ray ray = _camera.ScreenPointToRay(point);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
StartCoroutine(CreateBall(hit.collider.transform.position));
}
}
}
private IEnumerator CreateBall(Vector3 point)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = point;
yield return new WaitForSeconds(1);
Destroy(sphere);
}
- 分步加载资源,可以让玩家不用加载太多东西
private int num = 0;
private const int total=30;
void Start()
{
StartCoroutine(coRoutine());
}
IEnumerator coRoutine()
{
while (num<total)
{
num++;
Debug.Log(num);
yield return null;
}
}
void Update () {
Debug.Log("update!");
}
void LateUpdate()
{
Debug.Log("lateUpdate!");
}
- 可视化准心
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class show : MonoBehaviour {
private Camera _camera;
// Use this for initialization
void Start () {
_camera = GetComponent<Camera>();
Cursor.lockState = CursorLockMode.Locked; // 隐藏鼠标光标
Cursor.visible = false;
}
// Update is called once per frame
private void OnGUI()
{
int sz = 100;
float posX = _camera.pixelWidth / 2 ;
float posY = _camera.pixelHeight / 2 ;
GUI.Label(new Rect(posX, posY, sz, sz), "*");
}
}
- Destory(this.gameobject) 不能写成Destory(this),this只是指向这个脚本组件,而this.gameobject指向脚本所指向的对象
- [SerializeField] 建议所有的私有变量可以加这个,这样使得外界不能使用,但可以实现软编码
- CharacterController.move的问题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class doSth : MonoBehaviour
{
private CharacterController _c;
// Use this for initialization
void Start()
{
_c = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float X = Input.GetAxis("Horizontal");
float Z = Input.GetAxis("Vertical");
Vector3 move = new Vector3(X, 0, Z);
move = move * Time.deltaTime;
//到上面为止的move,自己理想中的方向,比如W,肯定是朝着自己的Z轴走。但实际输入的世界坐标,如果没把
//这种实际输入的坐标经过transform.TransformDirection(move);这句话的转化:将move变成相对于this.transform走的世界坐标,再去移动就对了
move = transform.TransformDirection(move);
_c.Move(move); //而对于transfrom.Translate是不需要TransformDirection的,因为Transfrom默认使用Space.Self
}
}
- 一般开发会使用白盒开发:方便,不用花过多的时间实现一些基础的操作,总是从见到到复杂的改变
- 每种Material是着色器的一个实例,它概要描述了用于绘制表面的指令
- 新建材质球->shader改变成skybox后可以自定义一个天空盒
- 实现小怪的简单来回AI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackAndForth : MonoBehaviour {
private int _dir = 1;
private float Speed = 2.0f;
private float Zup=10.0f;
private float Zlow = -10.0f;
//private bool change_dir = false;
private Transform m_transform;
// Use this for initialization
void Start () {
m_transform = transform;
}
// Update is called once per frame
void Update () {
m_transform.Translate(0, 0, _dir * Speed * Time.deltaTime);
if(m_transform.position.z >= Zup || m_transform.position.z <= Zlow)
{
//change_dir = true;
_dir = -_dir;
}
}
}
- 粒子系统可以创建火焰/烟雾等效果
- 对于img,2D叫精灵,3D叫贴图
- 通常会将UI和Scene分离处理
- 监听InputFields的内容改变和最终编辑结果(回车)
using UnityEngine;
using UnityEngine.UI;
public class inputfielddemo : MonoBehaviour
{
private InputField m_inputField;
void Start()
{
m_inputField = gameObject.GetComponent<InputField>();
m_inputField.onValueChanged.AddListener(InputFieldV);//不知道为什么直接在编辑界面加监听不行
m_inputField.onEndEdit.AddListener(inputFieldEnd);
}
void InputFieldV(string t)
{
print("当值发生改变ing" + t);
}
void inputFieldEnd(string e)
{
print("当编辑结束时:" + e);
}
}
- 通过PlayerPrefs.SetFloat保存一个想一直在游戏中保存的数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class record : MonoBehaviour {
private float delta = 0.5f;
// Use this for initialization
void Start () {
PlayerPrefs.SetFloat("go",1.0f);
}
// Update is called once per frame
void Update () {
PlayerPrefs.SetFloat("go", PlayerPrefs.GetFloat("go")+1.0f);
Debug.Log(PlayerPrefs.GetFloat("go"));
}
}
-