Unity最常见的面试题-04

本文涉及的产品
交互式建模 PAI-DSW,5000CU*H 3个月
简介: unity最常见的面试题,总有一题是你急需的!

前言:小弟收集了网上各大面试题并按照它们出现的频率进行了整理,如果对你有所帮助,欢迎评论和点赞,感谢~

图片.png


一.Animation.IsPlaying() 与 Animation.isPlaying 的区别

animation.IsPlaying() 与 animation.isPlaying 的区别,animation.IsPlaying()判断当前是否在播放某段动画,animation.isPlaying 判断当前是否正在播放动画(任何)

详细来说就是,animation.IsPlaying("Idle_1") 判断当时是否正在播放 Idle_1 这段动画,animation.isPlaying判断当前Animation这个组件是否有在播放任意动画。


二.跳跃(只控制上下,未设置前后方向的移动)

在跳跃中,分成上升和下降两段过程

首先需要设置几个变量值

private Transform player;//要跳跃的物体

   publicbool isJumping = false;//当前是否正处于跳跃状态

   privatebool isUp=false;//当前是否正在上升(在跳跃状态中,上升时为true,下落时为false)

   publicfloat jumpHeight = 20f;//最终要达到的跳跃高度

   publicfloat jumpSpeed = 10f;//跳跃的升降速度

   float haveJumpHeight=0;//当前已经跳跃的高度

设置完了以后要先获取到需要跳跃的物体,然后在触发跳跃时更改开关参数

if (isJumping == false)//判断当前是否正处于跳跃状态

   {

         isJumping = true;

         isUp = true;

         haveJumpHeight = 0f;

   }

根据开关参数来控制当前物体的跳跃

void Update(){

    if (isJumping)

       {

           float yMove = jumpSpeed * Time.deltaTime;

           if (isUp)//上升过程

           {

               player.position = new Vector3(player.position.x, player.position.y+yMove, player.position.z);

               haveJumpHeight += yMove;

               if(Mathf.Abs(jumpHeight - haveJumpHeight) <= 0.5f)

               {

                   player.position = new Vector3(player.position.x, player.position.y + (jumpHeight - haveJumpHeight), player.position.z);

                   isUp = false;

                   haveJumpHeight = jumpHeight;

               }

           }

           else//下降过程

           {

               player.position = new Vector3(player.position.x, player.position.y - yMove, player.position.z);

               haveJumpHeight -= yMove;

               if (Mathf.Abs(haveJumpHeight) <= 0.5f)

               {

                   player.position = new Vector3(player.position.x, player.position.y -  haveJumpHeight, player.position.z);

                   isJumping = false;

                   haveJumpHeight = 0f;

               }

           }

       }

}


三. 冒泡排序 + 普通顺序查找 方法:(此方法在数据规模小的时候适用,在大部分情况下适用)

public class BubbleSort

{

   public Tree[] trees = new Tree[100];   // 假设已经有了100颗已经实例的树。


   List<Tree> trees_pos = new List<Tree>();


   //获取高度为 hight 的所有树木

   public List<Tree> GetAppointHight(float hight)

   {


       StartSort();  //首先先排序

       trees_pos.Clear();


   // 顺序查找

       for(int i  = 0; i < trees.Length; i++)

       {

           if(trees[i].m_hight > hight)

           {

               trees_pos.Add(trees[i]);

           }

       }

       return trees_pos;

   }


   //正宗冒泡排序

   void StartSort()

   {

       for (int i = 0; i < trees.Length - 1; i++)

       {

           for (int j = 0; j < trees.Length - 1 - i; j++)

           {

               if (trees[j].m_hight > trees[j + 1].m_hight)

               {

                   Tree tree = trees[j];

                   trees[j] = trees[j + 1];

                   trees[j + 1] = tree;

               }

           }

       }

   }

}


四. 归并排序 + 二分查找查找 方法:(此方法在数据规模大的时候适用,在大部分情况下适用)

public class MergeSort

{

   public Tree[] trees = new Tree[1000000];  // 假设已经有一百万颗已经实例的树

   public Tree[] new_trees;   //储存用于归并排序之后数据的并入


   //通过这个函数获取 树中 高度 大于 hight 的所有树木

   public void GetAppointHight(float hight)

   {

       new_trees = new Tree[trees.Length];  


       StartMergeSort(0, trees.Length - 1);  // 对数组进行 归并排序


       int index = TwoPointSearch(hight);   //排序之后。二分查找出需要高度所在的下标


       for(int i = index; i < new_trees.Length; i++)    //输出高度大于 hight 的所有树木

       {

           Debug.Log(" All hight : index :" + i + " : " + new_trees[i].m_hight);

       }

   }



   //二分查找 算法

   int TwoPointSearch(float hight)

   {

       int low = 0;

       int high = new_trees.Length;


       while (low <= high)

       {

           int mid = (low + high) / 2;


           if (hight > new_trees[mid].m_hight)   // 如果要找的数在后半部分

           {

               low = mid + 1;

           }

           else if(hight < new_trees[mid].m_hight)  // 负责在前半部分

           {

               high = mid - 1;

           }

           else{


               return mid;

           }

       }


       return -1;

   }



   //二路归并(递归实现)

   void StartMergeSort(int low, int high)

   {

       if (low < high)

       {

           int mid = (low + high) / 2;

           StartMergeSort(low, mid);      //左边有序

           StartMergeSort(mid + 1, high);   //右边有序


           Merge(low, mid, high);     //再将两个有序序列合并

       }

   }


   //排序

   public void Merge(int low, int mid, int high)

   {

       int i, j, k = 0;

       i = low;

       j = mid + 1;//避免重复比较a[mid]


       while (i <= mid && j <= high)//数组a[low,mid]与数组(mid,high]均没有全部归入数组temp中去

       {

           if (trees[i].m_hight <= trees[j].m_hight)        //如果a[i]小于等于a[j]

               new_trees[k++] = trees[i++]; //则将a[i]的值赋给temp[k],之后i,k各加一,表示后移一位

           else

               new_trees[k++] = trees[j++]; //否则,将a[j]的值赋给temp[k],j,k各加一

       }



       while (i <= mid)             //表示数组a(mid,high]已经全部归入temp数组中去了,而数组a[low,mid]还有剩余

           new_trees[k++] = trees[i++];     //将数组a[low,mid]剩下的值,逐一归入数组temp


       while (j <= high)           //表示数组a[low,mid]已经全部归入到temp数组中去了,而数组(mid,high]还有剩余

           new_trees[k++] = trees[j++];     //将数组a(mid,high]剩下的值,逐一归入数组temp


       for (i = 0; i < k; i++)     //将归并后的数组的值逐一赋给数组a[low,high]

           trees[low + i] = new_trees[i];     //注意,应从a[low+i]开始赋值

   }

}


图片.png


相关文章
|
7月前
|
存储 网络协议 安全
Unity最常见的面试题-02
unity最常见的面试题,总有一题是你急需的!
|
7月前
|
存储 Java C#
Unity最常见的面试题-03
unity最常见的面试题,总有一题是你急需的!
|
7月前
|
缓存 Java 调度
Unity最常见的面试题-01
unity最常见的面试题,总有一题是你急需的!
|
图形学
Unity面试题——逻辑题
Unity面试题——逻辑题
114 0
|
存储 编解码 小程序
Unity面试题——图形学相关
Unity面试题——图形学相关
389 0
|
设计模式 开发框架 算法
Unity面试题——设计模式相关
Unity面试题——设计模式相关
142 0
|
存储 人工智能 算法
Unity面试题——数据结构算法相关
Unity面试题——数据结构算法相关
170 1
|
网络协议 安全 关系型数据库
Unity面试题——服务器数据库等杂项
Unity面试题——服务器数据库等杂项
111 0
|
存储 设计模式 缓存
Unity面试题——Unity性能优化
Unity面试题——Unity性能优化
328 0
|
4月前
|
C# 图形学
【Unity 3D】元宇宙案例之虚拟地球信息射线实战(附源码、演示视频和步骤 超详细)
【Unity 3D】元宇宙案例之虚拟地球信息射线实战(附源码、演示视频和步骤 超详细)
50 0