Unity 杂知识集合(小白级别)
程序员文章站
2022-03-26 15:55:49
...
自学Unity又找不到系统的Unity学习,只有各种零碎的知识点,自己做的时候很多需求不知道怎么搞,就记录一下,完全是为了自己方便使用,可能会非常初级,而且不全面,但是没办法,一点一点的积累吧,争取慢慢的补全。
单例模式:
public class GameController : MonoBehaviour
{
private static GameController _instance;
public static GameController Instance
{
get
{
return _instance;
}
}
void Awake()
{
_instance = this;
}
void DoSomething(){
}
}
调用:
GameController.Instance().DoSomething();
本地化存储数据:
PlayerPrefs.GetInt("gold", gold); //其他类型类似
PlayerPrefs.SetInt("gold", 9999);
PlayerPrefs.DeleteAll();// 清除所有数据
加载新的场景:
using UnityEngine.SceneManagement;
SceneManager.LoadScene(0);
SceneManager.LoadScene("Main");
退出程序:
Application.Quit();
设置是否可用:
public Text text;
text.gameObject.SetActive(true);
text.gameObject.SetActive(false);
获取鼠标点击事件:
Input.GetMouseButtonDown(0);//鼠标左键点击
Input.GetAxis("Mouse ScrollWheel");//鼠标滑轮事件,大于小于零判断上下滑动
开启协程(协同程序):
StartCoroutine(DoSmoething());//调用
//方法体本身
IEnumerator DoSmoething(){
...
yield return new WaitForSeconds(0.5f);
...
}
创建物体并设置位置角度:
public GameObject disGameObject;
public Transform transform;
public Prefab disPrefab;
public int angOffset;
GameObject gameObject = Instantiate(disPrefab);
gameObject.transform.SetParent(transform, false);
gameObject.transform.localPosition = disGameObject.localPosition;
gameObject.transform.localRotation = disGameObject.localRotation;
gameObject.transform.Rotate(0, 0, angOffset);
Instantiate(gameObject, new Vector3(0, 0, -0.5f), Quaternion.identity);
获取物体身上挂载代码组件的属性:
public class BeanEntity : MonoBehaviour{
public int maxNum;
public int maxSpeed;
}
public GameObject gameObject;
int maxNum = gameObject.GetComponent<BeanEntity>().maxNum;
int maxSpeed = gameObject.GetComponent<BeanEntity>().maxSpeed;
类的书写:
[System.Serializable]
public class BeanEntity{
public int num { get; set; }
public bool isFirst { get; set; }
public BeanEntity(int number,bool IsFirst){
this.num = number;
this.isFirst = IsFirst;
}
}
碰撞检测:
public class Item : MonoBehaviour{
private void OnCollisionEnter2D(Collision2D collision){
if(collision.collider.tag == "Player"){
//在物体的位置播放一遍动画
Instantiate(animatorPrefab, transform.position, transform.rotation);//transform是该物体的
//销毁物体
Destroy(gameObject);//gameObject是该物体的
}
}
}
设置面板显示内容:
[Header("菜单页面")]
public GameObject menuView;
设置透明度:
public Image image;
//Image身上必须挂载GanvasGroup组件
image.GetComponent<CanvasGroup>().alpha = 0;
image.GetComponent<CanvasGroup>().alpha = 1;
控制图片显示隐藏:
public Image image;
//Image需要挂载CanvasGroup组件
image.GetComponent<CanvasGroup>().alpha = 1;
image.GetComponent<CanvasGroup>().interactable = true;
image.GetComponent<CanvasGroup>().blocksRaycasts = true;
image.GetComponent<CanvasGroup>().alpha = 1;
image.GetComponent<CanvasGroup>().interactable = true;
image.GetComponent<CanvasGroup>().blocksRaycasts = true;
图片设置宽高:
public Image image;
image.GetComponent<RectTransform>().sizeDelta = new Vector2(100f, 190f);
获取点击事件:
using UnityEngine.EventSystems;
public class Click : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler{
public void OnPointerDown(PointerEventData eventData){
}
public void OnPointerUp(PointerEventData eventData){
}
public void OnPointerClick(PointerEventData eventData){
}
}
移动:
public GameObject gameObject;
gameObject.transform.Translate(Vector3.up * speed * Time.fixedDeltaTime, Space.World);
放置在某个位置:
public GameObject gameObject;
gameObject.transform.localPosition = new Vector3((float)x, (float)y, (float)z);
代码获取预制体:
//预制体放在Resources下的Prefabs文件夹中,只要是在Resources文件夹下都可以用这个路径获取;
(GameObject)Resources.Load("Prefabs/Item");
数据本地存储:
//写入
FileInfo file = new FileInfo(Config.Path);
StreamWriter sw = file.CreateText();
string json = JsonConvert.SerializeObject(bean);
sw.WriteLine(json);
sw.Close();
sw.Dispose();
//读取
FileInfo fi = new FileInfo(Config.JsonHeroPath);
StreamReader Stream_reader = fi.OpenText();
string str = Stream_reader.ReadToEnd();
Stream_reader.Close();
Stream_reader.Dispose();
return str;
JSON数据转换成类
需要导包:JSON工具
Bean bean = JsonConvert.DeserializeObject<Bean>(str);
随机数:
int num = Random.Range(0, 100);
上一篇: Unity-控制物体的移动
下一篇: 有关JAVA中的ServerSocket
推荐阅读