unity 异步加载网络图片_30-Unity入门学习9「Unity的常用类」

#原创#

一、Unity的常用类

1.1、Application类

①获取当前Unity版本【Application.unityVersion】

②获取当前Unity运行的平台【Application.platform】

③场景加载【SceneManager.LoadScene...】

④固定路径获取【Application.persistentDataPath...】

目前最常用到的以上四个内容的获取、用法如下:

/**** Title:"Unity基础" 项目* 主题:* Description:* 功能:* Date:2019* Version:0.1版本* Author:Coffee* Modify Recoder:*/using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;namespace TestFunction{public class Test : MonoBehaviour{//1-当前Unity的版本private string _CurrentUnityVersion = string.Empty;//2-当前Unity运行的平台private string _CurrentUnityPlatform = string.Empty;void Start(){//获取当前Unity的版本_CurrentUnityVersion = Application.unityVersion;//判断当前Unity运行的平台_CurrentUnityPlatform = GetRunPlatform();//打印信息PrintInfo(_CurrentUnityVersion);PrintInfo(_CurrentUnityPlatform);PrintInfo(GetFixedPath(UnityPath.persistentDataPath));PrintInfo(GetFixedPath(UnityPath.streamingAssetsPath));PrintInfo(GetFixedPath(UnityPath.LocaldataPath));//3-加载场景(同时加载两个场景到当前场景)SceneManager.LoadScene("Lighting", LoadSceneMode.Additive);SceneManager.LoadScene("SampleScene", LoadSceneMode.Additive);/*4-跳转场景*///同步加载场景(适合较小场景的加载)SceneManager.LoadScene("SampleScene");//异步加载场景(适合较大场景的加载,显示加载进度)SceneManager.LoadSceneAsync("SampleScene");//退出游戏Application.Quit();}//1-获取当前运行的平台(常用)private string GetRunPlatform(){string curPlatform = string.Empty;switch (Application.platform){//Window编辑器和Window平台case RuntimePlatform.WindowsEditor:case RuntimePlatform.WindowsPlayer:curPlatform = "Windows".ToUpper();break;    case RuntimePlatform.OSXEditor:case RuntimePlatform.OSXPlayer:curPlatform = "OSX".ToUpper();break;case RuntimePlatform.LinuxEditor:case RuntimePlatform.LinuxPlayer:curPlatform = "Andorid".ToUpper();break;case RuntimePlatform.WebGLPlayer:curPlatform = "WebGL".ToUpper();break;case RuntimePlatform.IPhonePlayer:curPlatform = "Iphone".ToUpper();break;case RuntimePlatform.Android:curPlatform = "Andorid".ToUpper();break;case RuntimePlatform.PS4:curPlatform = "PS4".ToUpper();break;case RuntimePlatform.XboxOne:curPlatform = "XboxOne".ToUpper();break;case RuntimePlatform.Switch:curPlatform = "Switch".ToUpper();break;default:break;}return curPlatform;}  //2-获取Unity固定路径private string GetFixedPath(UnityPath unityPath){string path = string.Empty;switch (unityPath){case UnityPath.persistentDataPath://持久化路径(推荐使用,可读可写)path = Application.persistentDataPath;break;case UnityPath.streamingAssetsPath://流资源路径(推荐使用,只读)path = Application.streamingAssetsPath;break;case UnityPath.LocaldataPath://本项目基础路径(即Assets路径)path = Application.dataPath;break;default:break;}return path;}//3-打印信息private void PrintInfo(string info){Debug.Log("当前信息为:"+info);}}//Class_end//Unity常用路径枚举public enum UnityPath{persistentDataPath,streamingAssetsPath,LocaldataPath,}}

1.2、Debug类

①正常调试打印信息【Debug.Log()】

②警告信息打印【Debug.LogWarning()】

③错误信息打印【Debug.LogError()】

以上信息打印的3个方法使用如下:

/" 项目* 主题:* Description:* 功能:* Date:2019* Version:0.1版本* Author:Coffee* Modify Recoder:*/using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;namespace TestFunction{public class Test : MonoBehaviour{void Start(){//1-常用信息打印Debug.Log("这是常用的信息打印测试");Debug.LogFormat("1-{0},2-{1},3-{2}", "测试1", "测试2", "测试3");//2-警告信息打印Debug.LogWarning("这是警告信息打印测试");Debug.LogWarningFormat ("1-{0},2-{1},3-{2}", "警告测试1", "警告测试2", "警告测试3");//3-错误信息打印Debug.LogError("这是警告信息打印测试");Debug.LogErrorFormat("1-{0},2-{1},3-{2}", "错误测试1", "错误测试2", "错误测试3");}}//Class_end}运行结果如下:
6da5b38305c74651aa3469fbeb0600cb

1.3、Input类

常用的为键盘输入和鼠标输入:

①按下键盘【Input.GetKeyDown()】按下鼠标【Input.GetMouseButtonDown()】

②按着键盘【Input.GetKey()】按着鼠标【GetMouseButton()】

③放开键盘【Input.GetKeyUp()】放开鼠标【Input.GetMouseButtonUp()】

具体常用的按下键盘和鼠标的使用方法如下:

/**** Title:"Unity基础" 项目* 主题:* Description:* 功能:* Date:2019* Version:0.1版本* Author:Coffee* Modify Recoder:*/using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;namespace TestFunction{public class Test : MonoBehaviour{private void Update(){/*1-按键操作*///按下按键就执行if (Input.GetKeyDown(KeyCode.A)){Debug.Log("你按下了左键就执行");}//一直按着按键才执行if (Input.GetKey(KeyCode.A)){Debug.Log("你一直按着W键就一直执行");}//抬起按键才执行if (Input.GetKeyUp(KeyCode.A)){Debug.Log("你按下了左键后放开弹起就执行");}/*2-鼠标操作*/if (Input.GetMouseButtonDown(0)){Debug.Log("你按下了鼠标左键");}if (Input.GetMouseButtonDown(2)){Debug.Log("你按下了鼠标中键");}if (Input.GetMouseButtonDown(1)){Debug.Log("你按下了鼠标右键");}if (Input.GetMouseButton(0)){Debug.Log("你一直按着鼠标左键");}if (Input.GetMouseButtonUp(0)){Debug.Log("你放开了鼠标左键");}}}//Class_end}

运行结果如下:

17cd1f7bf3644131930e861518b70606

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值