Unity基本操作汇总
向量的操作
//创建向量Vector3 v = new Vector3(1, 1, 1);//创建零向量v = Vector3.zero;//创建一向量v = Vector3.one;//创建自带的方向向量v = Vector3.forward;v = Vector3.left;v = Vector3.right;v = Vector3.back;Vector3 v2 = new Vector3(1, 2, 3);//计算两个向量的夹角Vector3.Angle(v1, v2);//计算两个向量的距离Vector3.Distance(v, v2);//向量点乘Vector3.Dot(v, v2);//向量叉乘Vector3.Cross(v, v2);//插值Vector3.Lerp(v, v2, 0.5f);//向量的模float a=v.magnitude;Vector3.Magnitude(v);//向量标准化v = v.normalized;v = Vector3.Normalize(v);//向量模的平方Vector3.SqrMagnitude(v);
旋转
//创建四元数Quaternion quaternion = Quaternion.identity;//欧拉角(向量)转换为四元数Vector3 rotate = new Vector3(1, 1, 1);quaternion = Quaternion.Euler(rotate);//看向一个目标quaternion = Quaternion.LookRotation(rotate);
GameObject
//得到当前脚本所挂载的gameobjectGameObject gameObject = this.gameObject;//名字、标签、层Debug.Log(gameObject.name);Debug.Log(gameObject.tag);Debug.Log(gameObject.layer);//当前真实激活状态Debug.Log(gameObject.activeInHierarchy);//自身激活状态Debug.Log(gameObject.activeSelf);//获取组件Component c = gameObject.GetComponent<Transform>();//添加组件gameObject.AddComponent<BoxCollider>();//获取一个自己和孩子节点的组件gameObject.GetComponentInChildren<Transform>();//获取自己和孩子节点的组件返回一个数组gameObject.GetComponentsInChildren<Transform>();//获取当前物体的父物体身上的某个组件gameObject.GetComponentInParent<Transform>();//通过物体名称来获取物体GameObject gameObject1 = GameObject.Find("name");//通过游戏标签来获取物体GameObject gameObject2 = GameObject.FindWithTag("tag");//激活或关闭物体gameObject.SetActive(true);//实例化预制体GameObject gameObject3 GameObject.Instantiate(prefab,new Vector3(0,0,0));//销毁物体GameObject.Destroy(gameObject3);
时间
//游戏开始到现在所用的时间Debug.Log(Time.time);//时间倍率,控制当前游戏时间的快慢Debug.Log(Time.timeScale);//固定时间间隔,配合FixedUpdate()使用Debug.Log(Time.fixedDeltaTime);//上一帧到这一帧所用的时间,可做计时器,在某时刻开始计时Debug.Log(Time.deltaTime);//当前游戏的帧数Debug.Log(Time.frameCount);
Application
//游戏数据文件路径(Assets)只读并且加密压缩Debug.Log(Application.dataPath);//不同平台下存储游戏数据的路径,持久读写Debug.Log(Application.persistentDataPath);//StreamingAssets文件夹,只读不加密,一般存储配置文件Debug.Log(Application.streamingAssetsPath);//临时文件夹Debug.Log(Application.temporaryCachePath);//控制是否在后台运行Debug.Log(Application.runInBackground);//打开urlApplication.OpenURL("www.baidu.com");//退出游戏Application.Quit();
场景
//跳转场景,第二个参数表示单独加载,如果改成Additive会将场景与当前场景叠加SceneManager.LoadScene("Scene1",LoadSceneMode.Single);//获取当前的场景Scene scene = SceneManager.GetActiveScene();//场景名称Debug.Log(scene.name);//场景是否已经加载Debug.Log(scene.isLoaded);//场景路径Debug.Log(scene.path);//场景索引Debug.Log(scene.buildIndex);//获取场景中所有游戏物体GameObject[] gameObjects = scene.GetRootGameObjects();//场景管理类//场景数量Debug.Log(SceneManager.sceneCount);//创建新场景并获取Scene newScene=SceneManager.CreateScene("newScene");//卸载场景SceneManager.UnloadScene(newScene);
使用协程实现异步加载
//加载对象AsyncOperation operation;void Start(){//开始协程StartCoroutine(loadScene());} //协程方法IEnumerable loadScene(){//加载operation = SceneManager.LoadSceneAsync(1);//返回加载对象yield return operation;}void Update(){//加载进度Debug.Log(operation.progress);}
Transform
//获取位置Debug.Log(transform.position);Debug.Log(transform.localPosition);//获取旋转//四元数Debug.Log(transform.rotation);Debug.Log(transform.localPosition);//欧拉角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();//获取子物体的transformTransform trans = transform.Find("gameObject");//通过索引获取子物体transform.GetChild(0);//判断一个物体是否是另外一个物体的子物体bool res = trans.IsChildOf(transform);//设置父物体trans.SetParent(transform);//看向某一点transform.LookAt(Vector3.zero);//旋转,第一个参数是方向轴,第二个是角度transform.Rotate(Vector3.up, 1);transform.Rotate(Vector3.right, 1);//绕某个物体旋转,第一个为目标点,第二个为方向轴,第三个是角度transform.RotateAround(Vector3.zero, Vector3.up, 1);//移动transform.Translate(Vector3.forward * 0.1f);
控制
键鼠控制
//鼠标控制 0为左键 1为右键 2为滚轮if (Input.GetMouseButtonDown(0)){Debug.Log("按下了鼠标左键");}if (Input.GetMouseButton(0)){Debug.Log("持续按下鼠标左键");}if (Input.GetMouseButtonUp(0)){Debug.Log("抬起了鼠标左键");}//键盘控制 if (Input.GetKeyDown(KeyCode.A)){Debug.Log("按下了A键");}if (Input.GetKey(KeyCode.A)){Debug.Log("持续按下了A键");}if (Input.GetKeyUp(KeyCode.A)){Debug.Log("抬起了A键");}
虚拟轴与按键
//虚拟轴float hori = Input.GetAxis("Horizontal");float vert = Input.GetAxis("Vertical");float mX = Input.GetAxis("Horizontal");float mY = Input.GetAxis("Vertical");//虚拟按键,空格出发虚拟按键jumpif (Input.GetButtonDown("jump")){Debug.Log("空格");}
手机触摸屏
//多点触摸开启Input.multiTouchEnabled = true;//单点触摸if(Input.touchCount==1){//触摸对象Touch touch = Input.touches[0];//触摸位置Debug.Log(touch.position);//触摸阶段switch (touch.phase){//开始触摸case TouchPhase.Began:break;//移动case TouchPhase.Moved:break;//静止case TouchPhase.Stationary:break;//结束case TouchPhase.Ended:break;//因为某些事件而取消case TouchPhase.Canceled:break;}}//多点触摸if (Input.touchCount == 2){Touch[] touch = Input.touches;}
音频
//AudioClippublic AudioClip music;//播放器组件private AudioSource player;player = GetComponent<AudioSource>();//设定播放的音频player.clip = music;//音量调节player.volume = 0.5f;//循环player.loop = true;//播放player.Play();//是否是播放状态Debug.Log(player.isPlaying);//停止播放player.Stop();//暂停和继续player.Pause();player.UnPause();//播放音效,快速可重叠player.PlayOneShot(music);
视频
videoPlayer=GetComponent<VideoPlayer>();//具体操作和音频一样
角色控制器
private CharacterController player;
player = GetComponent<CharacterController>();//拿到虚拟轴float horizontal = Input.GetAxis("Horizontal");float vertical = Input.GetAxis("Vertical");//使用虚拟轴创建移动向量Vector3 move= new Vector3(horizontal, 0, vertical);//使用虚拟轴控制角色移动player.SimpleMove(move*2);
碰撞和触发
//监听发生碰撞 collision是碰撞信息private void OnCollisionEnter(Collision collision){Instantiate(explosionObject, transform.position, Quaternion.identity);Destroy(gameObject);//碰撞到的物体名字Debug.Log(collision.gameObject.name);//碰撞到的碰撞体collision.collider;}//持续碰撞中private void OnCollisionStay(Collision collision){ }//结束碰撞private void OnCollisionExit(Collision collision){ }//触发,other是触发的碰撞器private void OnTriggerEnter(Collider other){}private void OnTriggerStay(Collider other){}private void OnTriggerExit(Collider other){}
物理关节
铰链
Hinge Joint组件
弹簧
Spring Joint组件
固定关节
Fixed Joint
射线检测
//创建一条射线Ray ray = new Ray(Vector3.zero, Vector3.up);if(Input.GetMouseButtonDown(0)){//从主摄像机发射射线Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//碰撞信息类RaycastHit hit;//将碰撞信息返回hitbool res = Physics.Raycast(ray, out hit);if (res == true){transform.position = hit.point;}}//多检测RaycastHit[] hits = Physics.RaycastAll(ray);
线条和拖尾
Line Renderer
Tail Renderer
LineRenderer lineRenderer= gameObject.AddComponent<LineRenderer>();lineRenderer.positionCount = 3;lineRenderer.SetPosition(0, Vector3.zero);lineRenderer.SetPosition(1, Vector3.one);lineRenderer.SetPosition(2, Vector3.forward);
动画器
animator.Play("动画名");//播放动画//设置触发参数if (Input.GetKeyDown(KeyCode.F)) {GetComponent<Animator>().SetTrigger("pickup");/}animitor.GetFloat("fire");//获取参数
动画烘焙,
Root Transform Rotation
将旋转烘焙,执行动画时不影响真实旋转
Y轴和XZ轴烘焙同理
Curves 曲线
在动画运行时,返回当前运行时间的曲线值,动画器中设置同名参数会得到曲线值
Event 事件
在动画的某一时刻执行一个函数,这个函数只要存在于挂载脚本中即可
反向动力学
//IK写到这个方法中private void OnAnimatorIK(int layerIndex){//设置头部IKanimator.SetLookAtWeight(1);animator.SetLookAtPosition(lookatOBJ.transform.position);//设置手部IKanimator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);animator.SetIKPosition(AvatarIKGoal.LeftHand,lookatOBJ.transform.position);animator.SetIKRotation(AvatarIKGoal.LeftHand, lookatOBJ.transform.rotation);}
}
导航
window—>AI—>nav
寻路
添加组件NavMeshAgent
//NavMeshAgent需要命名空间using UnityEngine.AI
//获取组件 agent = gameObject.GetComponent<NavMeshAgent>();if (Input.GetMouseButtonDown(0)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if(Physics.Raycast(ray,out hit)){//点击位置Vector3 point = hit.point;//设置该位置为导航目标点agent.SetDestination(point);}}
动态障碍物设置
添加组件NavMeshObstacle
网格链接
必须将object中的生成网格链接打勾才生效
Generated off Mesh Links
设置掉落高度和跳跃距离
可自定义链接位置
引入组件 off mesh link
设置两个点,可自动更新位置
区域和成本
area设置区域,不同的区域可设置成本,导航时根据成本来选择最优路线
游戏物体在objedct中可设置为不同区域
导航代理中可设置区域遮罩,选择可以走哪些区域
Unity基本操作汇总相关推荐
- 遇见的Unity疑难杂症汇总(个人积累)
遇见的Unity疑难杂症汇总(个人积累) 1.Unity打开VS提示杂项文件 将Unity工程中的XX.sln文件删掉再重新打开工程编译 2.Unity打开VS后部分命名空间丢失 新建一个Unity工 ...
- Unity基础笔记(1)—— Unity基本操作与基本组件介绍
Unity 基本操作与组件 一.Unity 基本操作 1. Unity 界面详解 Hierachy:层级面板,游戏场景中的资源,比如UI.模型: Scene:场景面板,用于管理游戏场景中的各种游戏物体 ...
- Linux学习笔记之基本操作汇总
Linux学习笔记之基本操作汇总 图片放大了再看才清楚!!!! Linux cd ocd / root package ocd /user ocd -/ ocd ~ home ocd - ls ...
- Asset Store上常用的40个Unity插件汇总——进阶开发者必备Unity插件
上篇文章着重介绍了Unity Asset Store(Unity资源商店)上一些超棒的资源与素材. unity老司机的资源推荐与常用插件汇总合集 - 简书 Unity插件资源购买小技巧 - 简书 本篇 ...
- SQL字符串基本操作汇总
2019独角兽企业重金招聘Python工程师标准>>> --===============================字符串使用汇总======================= ...
- Android原生开发modules方式导入Unity问题汇总
1.UNITY到处的android工程导入后,需要设置依赖 2.aapt打包时遇到该问题 A problem occurred evaluating project ':unityLibrary'. ...
- Unity 基本操作
基本操作 物体的组合 1.从需要的组合的物体中选择一个作为父对象,其他的物体作为子对象,即把子对象拖拽到父对象里,操作父对象即可实现整体操作,效果如下. 2.在组合物体中,父对象的坐标是该组合体的坐标 ...
- MS-DOS基本操作汇总
MS-DOS可以利用命令有效进行批量操作,解决众多繁琐操作,因此,根据个人使用需要将,常用的MS-DOS基本命令汇总如下: (1)文件夹内文件重命名 ren *.* *.*.txt ren --- r ...
- Unity基本操作学习
前言: 今天大部分时间花在如何安装unity上面了,装官网版本装了又卸载,卸载又安装,最后在学长的帮助下才完成安装(呜呜呜) 一,物体的创建 物体调整及快捷键 常用快捷键 快捷键Q--Hand(手形) ...
最新文章
- 成功解决OSError: [Errno 28] No space left on device
- Spring Cloud简介
- NYOJ 608 畅通工程 并查集
- hibernate session的load和get方法
- Spring Boot集成测试中@ContextConfiguration和@SpringApplicationConfiguration之间的区别
- 5 MM配置-企业结构-分配-给公司代码分配工厂
- iOS开发——网络使用技术OC篇网络爬虫-使用正则表达式抓取网络数据
- AndroidStudio(7)---导入jar包方法
- 小白入门angular-cli的第一次旅程(学习目标 学习目标 1. 路由基础知识)补充学习...
- centos5编译内核
- 美媒:中国假冒芯片太假 致驻伊美军频繁坠机
- C语言程序设计谭浩强版 五
- std::numeric_limits的一个使用注意事项
- 人机大战简史(第二版)
- PS常用快捷键大全(2020版)
- 杭电ACMSteps中Chapter One——Section 3中所有ac代码及解析
- JavaWeb整合萤石云(一),VUE和小程序也适用
- c语言程序设计入门教程视频教学
- 简单记录一下雨量计的分类
- Python×cmd:教你装X