Unity 屏幕旋转

  1. void Update () {
  2.  
  3. //处理横向两个方向旋转
  4.  
  5. if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
  6.  
  7. {
  8.  
  9. if (Screen.orientation != ScreenOrientation.LandscapeLeft) {
  10.  
  11. Screen.orientation = ScreenOrientation.LandscapeLeft;
  12.  
  13. }
  14.  
  15. }else if(Input.deviceOrientation == DeviceOrientation.LandscapeRight)
  16.  
  17. {
  18.  
  19. if (Screen.orientation != ScreenOrientation.LandscapeRight) {
  20.  
  21. Screen.orientation = ScreenOrientation.LandscapeRight;
  22.  
  23. }
  24.  
  25. }else
  26.  
  27. //处理纵向两个方向的旋转
  28.  
  29. if(Input.deviceOrientation == DeviceOrientation.Portrait)
  30.  
  31. {
  32.  
  33. if (Screen.orientation != ScreenOrientation.Portrait) {
  34.  
  35. Screen.orientation = ScreenOrientation.Portrait;
  36.  
  37. }
  38.  
  39. }else if(Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)
  40.  
  41. {
  42.  
  43. if (Screen.orientation != ScreenOrientation.PortraitUpsideDown) {
  44.  
  45. Screen.orientation = ScreenOrientation.PortraitUpsideDown;
  46.  
  47. }
  48.  
  49. }
  50.  
  51. }

Unity 镜头拉近效果(带缓冲)

  1. var target : Transform;
  2.  
  3. var distance : float = 3.0;
  4.  
  5. var height : float = 1.0;
  6.  
  7. var damping : float = 5.0;
  8.  
  9. var smoothRotation : boolean = true;
  10.  
  11. var rotationDamping : float = 10.0;
  12.  
  13. var targetLookAtOffset : Vector3; // allows offsetting of camera lookAt, very useful for low bumper heights
  14.  
  15. var bumperDistanceCheck : float = 2.5; // length of bumper ray
  16.  
  17. var bumperCameraHeight : float = 1.0; // adjust camera height while bumping
  18.  
  19. var bumperRayOffset : Vector3; // allows offset of the bumper ray from target origin
  20.  
  21. function FixedUpdate() {
  22.  
  23. var wantedPosition = target.TransformPoint(, height, -distance);
  24.  
  25. // check to see if there is anything behind the target
  26.  
  27. var hit : RaycastHit;
  28.  
  29. var back = target.transform.TransformDirection(- * Vector3.forward);
  30.  
  31. // cast the bumper ray out from rear and check to see if there is anything behind
  32.  
  33. if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, hit, bumperDistanceCheck)) {
  34.  
  35. // clamp wanted position to hit position
  36.  
  37. wantedPosition.x = hit.point.x;
  38.  
  39. wantedPosition.z = hit.point.z;
  40.  
  41. wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
  42.  
  43. }
  44.  
  45. transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
  46.  
  47. var lookPosition : Vector3 = target.TransformPoint(targetLookAtOffset);
  48.  
  49. if (smoothRotation) {
  50.  
  51. var wantedRotation : Quaternion = Quaternion.LookRotation(lookPosition - transform.position, target.up);
  52.  
  53. transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
  54.  
  55. } else {
  56.  
  57. transform.rotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
  58.  
  59. }
  60.  
  61. }

Untiy 动画加速

  1. for (var state : AnimationState in animation)
  2.  
  3. {
  4.  
  5. state.speed = 0.5;
  6.  
  7. }

Unith3D判断物体是否在视角内

  1. public var isRendering:boolean=false;
  2.  
  3. private var lastTime:float=;
  4.  
  5. private var curtTime:float=;
  6.  
  7. function Update()
  8.  
  9. {
  10.  
  11. isRendering=curtTime!=lastTime?true:false;
  12.  
  13. lastTime=curtTime;
  14.  
  15. }
  16.  
  17. function OnWillRenderObject()
  18.  
  19. {
  20.  
  21. curtTime=Time.time;
  22.  
  23. }

如何判断Unity3D角色动作播放结束

  1. bool AnimationStillPlaying( string animationName )
  2.  
  3.   {
  4.  
  5.   return _animation.IsPlaying(animationName) && _animation[animationName].normalizedTime<1.0f;
  6.  
  7.   }
  8.  
  9.   normalizedTime: 范围0 -- , 0是动作开始,1是动作结束

利用Quaternion.LookRotation API 将旋转矩阵转换成四元数

  1. Quaternion.LookRotation
  2.  
  3.   private static Quaternion QuaternionLookRotation(Vector3 forward, Vector3 up)
  4.  
  5.   {
  6.  
  7.   forward.Normalize();
  8.  
  9.   Vector3 vector = Vector3.Normalize(forward);
  10.  
  11.   Vector3 vector2 = Vector3.Normalize(Vector3.Cross(up, vector));
  12.  
  13.   Vector3 vector3 = Vector3.Cross(vector, vector2);
  14.  
  15.   var m00 = vector2.x;
  16.  
  17.   var m01 = vector2.y;
  18.  
  19.   var m02 = vector2.z;
  20.  
  21.   var m10 = vector3.x;
  22.  
  23.   var m11 = vector3.y;
  24.  
  25.   var m12 = vector3.z;
  26.  
  27.   var m20 = vector.x;
  28.  
  29.   var m21 = vector.y;
  30.  
  31.   var m22 = vector.z;
  32.  
  33.   float num8 = (m00 + m11) + m22;
  34.  
  35.   var quaternion = new Quaternion();
  36.  
  37.   if (num8 > 0f)
  38.  
  39.   {
  40.  
  41.   var num = (float)Math.Sqrt(num8 + 1f);
  42.  
  43.   quaternion.w = num * 0.5f;
  44.  
  45.   num = 0.5f / num;
  46.  
  47.   quaternion.x = (m12 - m21) * num;
  48.  
  49.   quaternion.y = (m20 - m02) * num;
  50.  
  51.   quaternion.z = (m01 - m10) * num;
  52.  
  53.   return quaternion;
  54.  
  55.   }
  56.  
  57.   if ((m00 >= m11) && (m00 >= m22))
  58.  
  59.   {
  60.  
  61.   var num7 = (float)Math.Sqrt(((1f + m00) - m11) - m22);
  62.  
  63.   var num4 = 0.5f / num7;
  64.  
  65.   quaternion.x = 0.5f * num7;
  66.  
  67.   quaternion.y = (m01 + m10) * num4;
  68.  
  69.   quaternion.z = (m02 + m20) * num4;
  70.  
  71.   quaternion.w = (m12 - m21) * num4;
  72.  
  73.   return quaternion;
  74.  
  75.   }
  76.  
  77.   if (m11 > m22)
  78.  
  79.   {
  80.  
  81.   var num6 = (float)Math.Sqrt(((1f + m11) - m00) - m22);
  82.  
  83.   var num3 = 0.5f / num6;
  84.  
  85.   quaternion.x = (m10+ m01) * num3;
  86.  
  87.   quaternion.y = 0.5f * num6;
  88.  
  89.   quaternion.z = (m21 + m12) * num3;
  90.  
  91.   quaternion.w = (m20 - m02) * num3;
  92.  
  93.   return quaternion;
  94.  
  95.   }
  96.  
  97.   var num5 = (float)Math.Sqrt(((1f + m22) - m00) - m11);
  98.  
  99.   var num2 = 0.5f / num5;
  100.  
  101.   quaternion.x = (m20 + m02) * num2;
  102.  
  103.   quaternion.y = (m21 + m12) * num2;
  104.  
  105.   quaternion.z = 0.5f * num5;
  106.  
  107.   quaternion.w = (m01 - m10) * num2;
  108.  
  109.   return quaternion;
  110.  
  111.   }

Unity3D判断场景是否加载完成

  1. 加载场景时要用
  2.  
  3.   public static AsyncOperation async;
  4.  
  5.   async = Application.LoadLevelAsync(“SelectCarModelScreen”);
  6.  
  7.   在加载场景里写
  8.  
  9.   if (MainInterfaceGUIScript.async.isDone == false)
  10.  
  11.   {
  12.  
  13.   //________没 有加载完要做的事情如Logo__________________________
  14.  
  15.   }
  16.  
  17.   else
  18.  
  19.   {
  20.  
  21.   //全部加载完成后显示的东东。。
  22.  
  23.   }
  24.  
  25.   说明一点这种方法消耗性能哦

物体运动到指定位置

  1. int smooth = ;
  2.  
  3.   Quaternion target = Quaternion.Euler(, , );//目标
  4.  
  5.   // Dampen towards the target rotation
  6.  
  7.   transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
  8.  
  9.   《《《《《《《《《《《《《《鼠标左键地形 物体移动至点击位置》》》》》》》》》》》》》
  10.  
  11.   var moveSpeed:int=;//player移动速度
  12.  
  13.   var player:Transform;//定义一个人物的Transform
  14.  
  15.   private var endposition : Vector3;
  16.  
  17.   function Start()
  18.  
  19.   {
  20.  
  21.   endposition = player.transform.position;
  22.  
  23.   }
  24.  
  25.   function Update ()
  26.  
  27.   {
  28.  
  29.   if(Input.GetButtonUp("LeftMouse")){ //LeftMouse是在inputManager中设置的,左键值为mouse 0
  30.  
  31.   PlayerMove();
  32.  
  33.   }
  34.  
  35.   var targetposition=player.TransformPoint(Vector3(,48.8,-));
  36.  
  37.   transform.position=targetposition;//相机的目标位置,这两句代码的作用是让人物一直处于相机的视野下
  38.  
  39.   if(endposition != player.transform.position){
  40.  
  41.   player.position=Vector3.MoveTowards(player.position,endposition,Time.deltaTime*moveSpeed);
  42.  
  43.   }
  44.  
  45.   }
  46.  
  47.   function PlayerMove()
  48.  
  49.   {
  50.  
  51.   var cursorScreenPosition:Vector3=Input.mousePosition;//鼠标在屏幕上的位置
  52.  
  53.   var ray:Ray=Camera.main.ScreenPointToRay(cursorScreenPosition);//在鼠标所在的屏幕位置发出一条射线(暂名该射线为x射线)
  54.  
  55.   var hit:RaycastHit;
  56.  
  57.   if(Physics.Raycast(ray,hit)){
  58.  
  59.   if(hit.collider.gameObject.tag=="Terrain"){//设置地形Tag为Terrain
  60.  
  61.   endposition = hit.point;
  62.  
  63.   }
  64.  
  65.   }
  66.  
  67.   }

射线测试+使用手势判断是否触碰了某物体

  1. using unityEngine;
  2.  
  3.   using System.Collections;
  4.  
  5.   public class Lu : MonoBehaviour
  6.  
  7.   {
  8.  
  9.   public Camera cam;
  10.  
  11.   // We need to actually hit an object
  12.  
  13.   RaycastHit hitt = new RaycastHit();
  14.  
  15.   // Use this for initialization
  16.  
  17.   void Start()
  18.  
  19.   {
  20.  
  21.   }
  22.  
  23.   // Update is called once per frame
  24.  
  25.   void Update()
  26.  
  27.   {
  28.  
  29.   Ray ray = cam.ScreenPointToRay(Input.mousePosition);
  30.  
  31.   Physics.Raycast(ray, out hitt, );
  32.  
  33.   // Debug.DrawLine(cam.transform.position, ray.direction,Color.red);
  34.  
  35.   if (null != hitt.transform)
  36.  
  37.   {
  38.  
  39.   print(hitt.point);//鼠标点击的坐标
  40.  
  41.   }
  42.  
  43.   }
  44.  
  45.   }
  46.  
  47.   ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  48.  
  49.   必须给物体添加碰撞器,比如Box Collider
  50.  
  51.   然后在Update中检测手势,此处假设为手指在屏幕上移动
  52.  
  53.   主相机的视角(Camera.main)从手指移动处(Input.GetTouch().position)发射射线,设置射线发射距离。
  54.  
  55.   当射线碰撞到了带有碰撞器的物体,就会存储该物体的信息。
  56.  
  57.   [csharp] view plaincopy
  58.  
  59.   void Update(){
  60.  
  61.   if(Input.touchCount > && Input.GetTouch().phase == TouchPhase.Moved){
  62.  
  63.   Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch().position);
  64.  
  65.   //hit用来存储碰撞物体的信息
  66.  
  67.   RaycastHit hit;
  68.  
  69.   //ray表示射线,hit存储物体的信息,1000为设定射线发射的距离
  70.  
  71.   if(Physics.Raycast (ray, out hit, )){
  72.  
  73.   print(hit.transform.name.ToString());
  74.  
  75.   }
  76.  
  77.   }
  78.  
  79.   }

实例来创建对象

  1. //Simple Instantiation of a Prefab at Start
  2.  
  3.   var thePrefab : GameObject;
  4.  
  5.   function Start () {
  6.  
  7.   var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
  8.  
  9.   }
  10.  
  11.   建立JAVA,把代码拖入到空GameJect上,然后把Prefab拖入到公共变量上,就可以了

声音播放 坐标转换

  1. var cAudio:AudioClip;
  2.  
  3. function Update(){
  4.  
  5. }
  6.  
  7. function OnMouseDown () {
  8.  
  9. var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
  10.  
  11. var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,transform.position.y, screenSpace.z));
  12.  
  13. while (Input.GetMouseButton()){
  14.  
  15. var curScreenSpace = Vector3(Input.mousePosition.x, transform.position.y, screenSpace.z);
  16.  
  17. var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
  18.  
  19. if(curPosition!=transform.position){
  20.  
  21. audio.Stop();
  22.  
  23. audio.clip = cAudio;
  24.  
  25. audio.Play();
  26.  
  27. }
  28.  
  29. transform.position = curPosition;
  30.  
  31. yield;
  32.  
  33. }
  34.  
  35. }

射线 (之鼠标点击的坐标)

  1. using UnityEngine;
  2.  
  3. using System.Collections;
  4.  
  5. public class Lu : MonoBehaviour
  6.  
  7. {
  8.  
  9. public Camera cam;
  10.  
  11. // We need to actually hit an object
  12.  
  13. RaycastHit hitt = new RaycastHit();
  14.  
  15. // Use this for initialization
  16.  
  17. void Start()
  18.  
  19. {
  20.  
  21. }
  22.  
  23. // Update is called once per frame
  24.  
  25. void Update()
  26.  
  27. {
  28.  
  29. Ray ray = cam.ScreenPointToRay(Input.mousePosition);
  30.  
  31. Physics.Raycast(ray, out hitt, );
  32.  
  33. // Debug.DrawLine(cam.transform.position, ray.direction,Color.red);
  34.  
  35. if (null != hitt.transform)
  36.  
  37. {
  38.  
  39. print(hitt.point);//鼠标点击的坐标
  40.  
  41. }
  42.  
  43. }
  44.  
  45. }

拖拽物体的脚本

  1. function OnMouseDown () {
  2.  
  3.   var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
  4.  
  5.   var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
  6.  
  7.   while (Input.GetMouseButton())
  8.  
  9.   {
  10.  
  11.   var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
  12.  
  13.   var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
  14.  
  15.   transform.position = curPosition;
  16.  
  17.   yield;
  18.  
  19.   }
  20.  
  21.   }

用指定图片替换鼠标指针

  1. //用指定图片替换鼠标指针
  2.  
  3. var mouse:Texture;
  4.  
  5. function Update(){
  6.  
  7. Screen.showCursor=false;//隐藏鼠标指针
  8.  
  9. }
  10.  
  11. function OnGUI{
  12.  
  13. var msPos=Input.mousePosition;//鼠标的位置
  14.  
  15. GUI.DrawTexture(Rect(msPos.x,Screen.height-msPos.y,,),mouse);
  16.  
  17. }

Unity 字体描边

  1. private void MakeStroke(Rect position,string txtString,Color txtColor,Color outlineColor,int outlineWidth )
  2.  
  3.   {
  4.  
  5.   position.y-= outlineWidth;
  6.  
  7.   GUI.color=outlineColor;
  8.  
  9.   GUI.Label(position, txtString);
  10.  
  11.   position.y+=outlineWidth*;
  12.  
  13.   GUI.Label(position, txtString);
  14.  
  15.   position.y-=outlineWidth;
  16.  
  17.   position.x-=outlineWidth;
  18.  
  19.   GUI.Label(position, txtString);
  20.  
  21.   position.x+=outlineWidth*;
  22.  
  23.   GUI.Label(position, txtString);
  24.  
  25.   position.x-=outlineWidth;
  26.  
  27.   GUI.color=txtColor;
  28.  
  29.   GUI.Label(position, txtString);
  30.  
  31.   }

调用外部的EXE和载入关卡

  1. using unityEngine;
  2.  
  3.   using System.Collections;
  4.  
  5.   public class openexe: MonoBehaviour {
  6.  
  7.   // Use this for initialization
  8.  
  9.   void Start () {
  10.  
  11.   System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
  12.  
  13.   Info.FileName = "C:\\Program Files\\Autodesk\\3dsMax8\\3dsmax.exe";
  14.  
  15.   System.Diagnostics.Process.Start(Info);
  16.  
  17.   }
  18.  
  19.   // Update is called once per frame
  20.  
  21.   void Update () {
  22.  
  23.   }
  24.  
  25.   }
  26.  
  27.   -----------------载入关卡--------------
  28.  
  29.   function OnGUI ()
  30.  
  31.   {
  32.  
  33.   if (GUI.Button(new Rect(,,,),"Start"))
  34.  
  35.   {
  36.  
  37.   Application.LoadLevel();
  38.  
  39.   }
  40.  
  41.   }

[Unity] 常用技巧收集的更多相关文章

  1. JavaScript常用技巧总结(持续添加中...)

    在我学习过程中收集的一些常用技巧: typeof x !== undifined 判断x是否已定义: x === Object(x)  判断x是否为对象: Object.keys(x).length ...

  2. AS技巧合集「常用技巧篇」

    转载:http://www.apkbus.com/forum.php?mod=viewthread&tid=254723&extra=page%3D2%26filter%3Dautho ...

  3. C#WebBrowser控件使用教程与技巧收集--苏飞收集

    C#WebBrowser控件使用教程与技巧收集--苏飞收集 先来看看常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System. ...

  4. T-SQL技巧收集——拆分字符串

    原文:T-SQL技巧收集--拆分字符串 在开发中,很多时候都需要处理拆分字符串的操作.下面收集了几种方法供大家分享,其中的逗号可以改为多种有需要的符号,但是不能针对多种符号同时存在的例子.有待各位补充 ...

  5. SHELL脚本编程的常识和VI常用技巧

    来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...

  6. Unity常用常找(二)

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51315050 作者:car ...

  7. [转帖]Linux Shell常用技巧(五)

    Linux Shell常用技巧(五) https://zhuanlan.zhihu.com/p/73451771 1. 变量:在awk中变量无须定义即可使用,变量在赋值时即已经完成了定义.变量的类型可 ...

  8. 【shell 大系】Linux Shell常用技巧

    在最近的日常工作中由于经常会和Linux服务器打交道,如Oracle性能优化.我们数据采集服务器的资源利用率监控,以及Debug服务器代码并解决其效率和稳定性等问题.因此这段时间总结的有关Linux ...

  9. oracle存储过程常用技巧

    我们在进行pl/sql编程时打交道最多的就是存储过程了.存储过程的结构是非常的简单的,我们在这里除了学习存储过程的基本结构外,还会学习编写存储过程时相关的一些实用的知识.如:游标的处理,异常的处理,集 ...

随机推荐

  1. python日常-int和float

    首先先看看下面的代码

  2. 前博客 http://bbs.landingbj.com/mytopic.jsp?action=mytopic&username=57071

    在工作学习的过程中,遇到了亮眼的技术点,或者 学习的心得体会,总想要记录下来,或是方便自己,或是帮助如同自己现在这般的新人.前人种树,后人乘凉.享受了前人留下的阴凉,也会考虑自己给后来者种下几棵树苗. ...

  3. [转]JVM调优总结:一些概念

    JVM调优总结:一些概念 原文出处: pengjiaheng 数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变 ...

  4. 扩展easyUI tab控件,添加加载遮罩效果

    项目里要用HighChart显示图表,如果返回的数量量太多,生成图表是一个很耗时的过程.tab控件又没有显示遮罩的设置(至少本菜是没有找到), Google了一下,根据另一个兄台写的方法,拿来改造了一 ...

  5. JAVA基本类型的转换

    1.String转成Int 例1: String str = "123"; try { int a = Integer.parseInt(str); } catch (Number ...

  6. 【BZOJ-1367】sequence 可并堆+中位数

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 932  Solved: 348[Submit][S ...

  7. 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)

    3589: 动态树 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 405  Solved: 137[Submit][Status][Discuss] ...

  8. 【BZOJ-1857】传送带 三分套三分

    1857: [Scoi2010]传送带 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1077  Solved: 575[Submit][Status][ ...

  9. 解析window.open链接的参数

    ); var arr = new Array(); arr = str.split("&"); ){ ; i<arr.length; i++){ ){ ); if(p ...

  10. Scala implicit

    Scala implicit implicit基本含义 在Scala中有一个关键字是implicit, 之前一直不知道这个货是干什么的,今天整理了一下. 我们先来看一个例子: def display( ...