提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Unity 搜集问题
前言
一、Unity 简介?
游戏引擎,用C#开发!二、问题合集
问题描述:人物随着鼠标移动
if (Input.GetMouseButton(0))
{
//3D世界坐标转为2D屏幕坐标
Vector3 cubeTo2D = Camera.main.WorldToScreenPoint(this.transform.position);
//屏幕坐标系中立方体位置
Vector3 NewTouch = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeTo2D.z);
//将屏幕坐标转换为实际的世界坐标并且赋值给立方体
this.transform.position = Camera.main.ScreenToWorldPoint(NewTouch);
} //注意相机标签
问题描述:碰撞体事件,tag,加载新的场景
using UnityEngine.SceneManagement;
private void OnTriggerEnter2D(Collider2D collision)
{
Destroy(this.gameObject);
if (collision.tag == "Exit")
{
SceneManager.LoadScene("Win");
return;
}
}
} //注意相机标签
问题描述:按钮事件的处理
第二种方法
//button_start.onClick.AddListener(ButtonDemo);
第一种方法
//button_start.onClick.AddListener(() =>
//{
// Debug.Log("第一种方法,点击Button按钮了!");
//});
public Button button1,button2;
// Update is called once per frame
void Update()
{
button1.onClick.AddListener(button1_text);
button2.onClick.AddListener(button2_text);
}
private void button1_text()
{
SceneManager.LoadScene("Game");
}
private void button2_text()
{
Application.Quit();
}
问题描述:人物上下移动
public float up, dawn, speed=1.5f;
void Update()
{
if (transform.position.y > up)
{
transform.position = new Vector3(transform.position.x, up, 0);
speed = -speed;
}else if (transform.position.y < dawn)
{
transform.position = new Vector3(transform.position.x, dawn, 0);
speed = -speed;
}
transform.Translate(0, speed * Time.deltaTime, 0);
}
//if (updawn && transform.position.y > 5)
//{
// updawn = false;
// transform.localEulerAngles = new Vector3(0, 0, 180);
//}
//if (!updawn && transform.position.y < -5)
//{
// updawn = true;
// transform.localEulerAngles = new Vector3(0, 0, 0);
//}
//float step = 0.8f * Time.deltaTime;
问题描述:基础描述组件
Debug.Log(gameObject.activeSelf);//当前游戏对象的激活状态
gameObject.SetActive(false);//设置当前对象的激活状态
gameObject.SetActive(!gameObject.activeSelf);//设置当前游戏对象的激活状态为当前状态的反向状态
Debug.Log(gameObject.name);//获取当前对象名字
Debug.Log(gameObject.tag);//获取当前对象标签
Debug.Log(gameObject.layer);//获取当前对象层
Light mylight = gameObject.GetComponent(type: "Light") as Light;
mylight = gameObject.GetComponent(typeof(Light)) as Light;
mylight = gameObject.GetComponent<Light>();//获取当前游戏对象的组件
mylight = gameObject.AddComponent<Light>();//添加一个组件到游戏对象身上,并返回这个组件
GameObject lt = GameObject.Find("Directional Light");//通过名字找到单个游戏对象
GameObject com = GameObject.FindWithTag("Player");//通过标签找到单个游戏对象
问题描述:预制体操作1 和键盘输入控制
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyJet : MonoBehaviour
{
public GameObject myPrefab;
float count = 0;
float intrval = 0.4f;
void Update()
{
//相当于一个定时器
count += Time.deltaTime;
if (count >= intrval)
{
count = 0;
Fire();
}
//按键响应
float step = 2.5f * Time.deltaTime;
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(-step, 0, 0);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(step, 0, 0);
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(0, step, 0);
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(0, -step, 0);
}
if (Input.GetMouseButtonUp(0))
{
Time.timeScale = 0;
}
if (Input.GetKey(KeyCode.A))
{
Time.timeScale = 1;
}
}
private void Fire()
{
Vector3 pos = transform.position + new Vector3(0, 1f, 0);
GameObject bullet = Instantiate(myPrefab, pos, transform.rotation);
}
}
问题描述:预制体操作2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monsterctrl : MonoBehaviour
{
public GameObject monsterprefab;
public Sprite[] images;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("CreateMonster", 0.1f, 3f);
}
// Update is called once per frame
void Update()
{
}
void CreateMonster()
{
//随机选择生成怪兽
float x = Random.Range(-2, 2);
float y = 5;
GameObject monster = Instantiate(monsterprefab);
monster.transform.position = new Vector3(x, y, 0);
//随机选择怪兽
int index = Random.Range(0, images.Length);
SpriteRenderer spriteRenderer = monster.GetComponent<SpriteRenderer>();
spriteRenderer.sprite = this.images[index];
//随机分割图片大小一致
Sprite sprite = this.images[index];
float imgWidth = sprite.rect.width;
float scale = 75 / imgWidth;
monster.transform.localScale = new Vector3(scale, scale, scale);
}
}
问题描述:背景图的轮换
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroundCtrl : MonoBehaviour
{
Transform bg1;
Transform bg2;
public float speed = 1f;
// Start is called before the first frame update
void Start()
{
bg1 = GameObject.Find("/背景/bg1").transform;
bg2 = GameObject.Find("/背景/bg2").transform;
bg1.position = new Vector3(0, 0, 0);
bg2.position = new Vector3(0, 10, 0);
}
// Update is called once per frame
void Update()
{
float dy = speed * Time.deltaTime;
bg1.Translate(0, -dy, 0);
bg2.Translate(0, -dy, 0);
if (bg1.position.y <-10)
{
bg1.Translate(0,20,0);
}
if (bg2.position.y < -10)
{
bg2.Translate(0, 20, 0);
}
}
}
问题描述:游戏的开始与结束
Time.timeScale = 0;//暂停
Time.timeScale = 1;//开始
Application.Quit();//程序的退出
Application.targetFrameRate = 60;//设置帧
问题描述:按屏幕坐标移动
float speed = 3f;
void Update()
{
float sp = speed * Time.deltaTime;
transform.Translate(0, -sp, 0);
Vector3 gong = Camera.main.WorldToScreenPoint(transform.position);
if (gong.y < 0)
{
Destroy(this.gameObject);
}
}
//if (gong.y > Screen.height)
//{
// bg1.transform.position = new Vector3(0, 0, 0);
// //bg2.transform.position = new Vector3(0, 0, 0);
//}
问题描述:Unity Editor下使用 Application.Quit()为什么程序没有退出?
因为Editor下使用 UnityEditor.EditorApplication.isPlaying = false 结束退出,
只有当工程打包编译后的程序使用Application.Quit()才奏效,
public void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
总结
以后还有待更新!!